From 80ac15f667c32b1e441cffaa3237cae2990cc152 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 16 May 2021 21:31:18 -0400 Subject: Optimize default ToString impl This avoids a zero-length write_str call, which boils down to a zero-length memmove and ultimately costs quite a few instructions on some workloads. This is approximately a 0.33% instruction count win on diesel-check. --- library/alloc/src/string.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'library/alloc/src') diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index e6252452470..ec09595e357 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2323,9 +2323,10 @@ impl ToString for T { // to try to remove it. #[inline] default fn to_string(&self) -> String { - use fmt::Write; let mut buf = String::new(); - buf.write_fmt(format_args!("{}", self)) + let mut formatter = core::fmt::Formatter::new(&mut buf); + // Bypass format_args!() to avoid write_str with zero-length strs + fmt::Display::fmt(self, &mut formatter) .expect("a Display implementation returned an error unexpectedly"); buf } -- cgit 1.4.1-3-g733a5