From 23fdb5520757e70b051dd621e6367ba68d3c436c Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 10 Aug 2020 09:41:57 +0000 Subject: [PATCH] Minimize the cost of write_fmt without arguments --- library/core/src/fmt/mod.rs | 5 ++++- library/std/src/io/mod.rs | 22 +++++++++++++--------- library/std/src/lib.rs | 1 + 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 638e83c3b939d..9a5210c6a5c1a 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -190,7 +190,10 @@ pub trait Write { /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result { - write(&mut self, args) + match args.as_str() { + Some(s) => self.write_str(s), + None => write(&mut self, args), + } } } diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 9eb54c2cc0044..1fc51ecc566e8 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1506,15 +1506,19 @@ pub trait Write { } } - let mut output = Adaptor { inner: self, error: Ok(()) }; - match fmt::write(&mut output, fmt) { - Ok(()) => Ok(()), - Err(..) => { - // check if the error came from the underlying `Write` or not - if output.error.is_err() { - output.error - } else { - Err(Error::new(ErrorKind::Other, "formatter error")) + if let Some(s) = fmt.as_str() { + self.write_all(s.as_bytes()) + } else { + let mut output = Adaptor { inner: self, error: Ok(()) }; + match fmt::write(&mut output, fmt) { + Ok(()) => Ok(()), + Err(..) => { + // check if the error came from the underlying `Write` or not + if output.error.is_err() { + output.error + } else { + Err(Error::new(ErrorKind::Other, "formatter error")) + } } } } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 0569e46241a80..bdf6c3f010bdb 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -265,6 +265,7 @@ #![feature(exhaustive_patterns)] #![feature(extend_one)] #![feature(external_doc)] +#![feature(fmt_as_str)] #![feature(fn_traits)] #![feature(format_args_nl)] #![feature(future_readiness_fns)]