diff options
| author | Yuri Astrakhan <YuriAstrakhan@gmail.com> | 2024-02-14 20:44:46 -0500 |
|---|---|---|
| committer | Yuri Astrakhan <YuriAstrakhan@gmail.com> | 2024-03-05 22:39:44 -0500 |
| commit | 3d0d0ce7401eca08d4f1420b609a78c2965639bb (patch) | |
| tree | 564462043be4afbc9a989e34d2d0aecae63cc956 | |
| parent | 62415e2a95a3b0f137636f86f6a904b8ed85798e (diff) | |
| download | rust-3d0d0ce7401eca08d4f1420b609a78c2965639bb.tar.gz rust-3d0d0ce7401eca08d4f1420b609a78c2965639bb.zip | |
Optimize write with as_const_str for shorter code
| -rw-r--r-- | library/core/src/fmt/mod.rs | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 444d4fee92d..5ea9ff527b8 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -201,14 +201,22 @@ pub trait Write { impl<W: Write + ?Sized> SpecWriteFmt for &mut W { #[inline] default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result { - write(&mut self, args) + if let Some(s) = args.as_const_str() { + self.write_str(s) + } else { + write(&mut self, args) + } } } impl<W: Write> SpecWriteFmt for &mut W { #[inline] fn spec_write_fmt(self, args: Arguments<'_>) -> Result { - write(self, args) + if let Some(s) = args.as_const_str() { + self.write_str(s) + } else { + write(self, args) + } } } @@ -430,6 +438,14 @@ impl<'a> Arguments<'a> { _ => None, } } + + /// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time. + #[must_use] + #[inline] + fn as_const_str(&self) -> Option<&'static str> { + let s = self.as_str(); + if core::intrinsics::is_val_statically_known(s.is_some()) { s } else { None } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1102,14 +1118,8 @@ pub trait UpperExp { /// ``` /// /// [`write!`]: crate::write! -#[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { - if let Some(s) = args.as_str() { output.write_str(s) } else { write_internal(output, args) } -} - -/// Actual implementation of the [`write()`], but without the simple string optimization. -fn write_internal(output: &mut dyn Write, args: Arguments<'_>) -> Result { let mut formatter = Formatter::new(output); let mut idx = 0; @@ -1588,8 +1598,9 @@ impl<'a> Formatter<'a> { /// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[inline] pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result { - write(self.buf, fmt) + if let Some(s) = fmt.as_const_str() { self.buf.write_str(s) } else { write(self.buf, fmt) } } /// Flags for formatting @@ -2278,8 +2289,13 @@ impl Write for Formatter<'_> { self.buf.write_char(c) } + #[inline] fn write_fmt(&mut self, args: Arguments<'_>) -> Result { - write(self.buf, args) + if let Some(s) = args.as_const_str() { + self.buf.write_str(s) + } else { + write(self.buf, args) + } } } |
