Skip to content

Commit

Permalink
Auto merge of rust-lang#121001 - nyurik:optimize-core-fmt, r=<try>
Browse files Browse the repository at this point in the history
perf: improve write_fmt to handle simple strings

Per `@dtolnay` suggestion in serde-rs/serde#2697 (comment) - attempt to speed up performance in the cases of a simple string format without arguments:

```rust
write!(f, "text")  ->  f.write_str("text")
```

```diff
+ #[inline]
  pub fn write_fmt(&mut self, f: fmt::Arguments) -> fmt::Result {
+     if let Some(s) = f.as_str() {
+         self.buf.write_str(s)
+     } else {
          write(self.buf, f)
+     }
  }
```

* Hopefully it will improve the simple case for the rust-lang#99012
* Another related (original?) issues rust-lang#10761
* Previous similar attempt to fix it by by `@Kobzol` rust-lang#100700

CC: `@m-ou-se` as probably the biggest expert in everything `format!`
  • Loading branch information
bors committed Feb 15, 2024
2 parents ee9c7c9 + e65e940 commit f8069ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
31 changes: 27 additions & 4 deletions library/core/src/fmt/mod.rs
Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -430,6 +438,15 @@ 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]
const fn as_const_str(&self) -> Option<&'static str> {
let s = self.as_str();
// SAFETY: both cases are valid as the result
if unsafe { core::intrinsics::is_val_statically_known(s.is_some()) } { s } else { None }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -1582,8 +1599,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
Expand Down Expand Up @@ -2272,8 +2290,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)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Expand Up @@ -176,6 +176,7 @@
#![feature(ip)]
#![feature(ip_bits)]
#![feature(is_ascii_octdigit)]
#![feature(is_val_statically_known)]
#![feature(isqrt)]
#![feature(maybe_uninit_uninit_array)]
#![feature(non_null_convenience)]
Expand Down

0 comments on commit f8069ba

Please sign in to comment.