Skip to content

Commit f8069ba

Browse files
committed
Auto merge of rust-lang#121001 - nyurik:optimize-core-fmt, r=<try>
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!`
2 parents ee9c7c9 + e65e940 commit f8069ba

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

library/core/src/fmt/mod.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,22 @@ pub trait Write {
201201
impl<W: Write + ?Sized> SpecWriteFmt for &mut W {
202202
#[inline]
203203
default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result {
204-
write(&mut self, args)
204+
if let Some(s) = args.as_const_str() {
205+
self.write_str(s)
206+
} else {
207+
write(&mut self, args)
208+
}
205209
}
206210
}
207211

208212
impl<W: Write> SpecWriteFmt for &mut W {
209213
#[inline]
210214
fn spec_write_fmt(self, args: Arguments<'_>) -> Result {
211-
write(self, args)
215+
if let Some(s) = args.as_const_str() {
216+
self.write_str(s)
217+
} else {
218+
write(self, args)
219+
}
212220
}
213221
}
214222

@@ -430,6 +438,15 @@ impl<'a> Arguments<'a> {
430438
_ => None,
431439
}
432440
}
441+
442+
/// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time.
443+
#[must_use]
444+
#[inline]
445+
const fn as_const_str(&self) -> Option<&'static str> {
446+
let s = self.as_str();
447+
// SAFETY: both cases are valid as the result
448+
if unsafe { core::intrinsics::is_val_statically_known(s.is_some()) } { s } else { None }
449+
}
433450
}
434451

435452
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1582,8 +1599,9 @@ impl<'a> Formatter<'a> {
15821599
/// assert_eq!(format!("{:0>8}", Foo(2)), "Foo 2");
15831600
/// ```
15841601
#[stable(feature = "rust1", since = "1.0.0")]
1602+
#[inline]
15851603
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result {
1586-
write(self.buf, fmt)
1604+
if let Some(s) = fmt.as_const_str() { self.buf.write_str(s) } else { write(self.buf, fmt) }
15871605
}
15881606

15891607
/// Flags for formatting
@@ -2272,8 +2290,13 @@ impl Write for Formatter<'_> {
22722290
self.buf.write_char(c)
22732291
}
22742292

2293+
#[inline]
22752294
fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
2276-
write(self.buf, args)
2295+
if let Some(s) = args.as_const_str() {
2296+
self.buf.write_str(s)
2297+
} else {
2298+
write(self.buf, args)
2299+
}
22772300
}
22782301
}
22792302

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
#![feature(ip)]
177177
#![feature(ip_bits)]
178178
#![feature(is_ascii_octdigit)]
179+
#![feature(is_val_statically_known)]
179180
#![feature(isqrt)]
180181
#![feature(maybe_uninit_uninit_array)]
181182
#![feature(non_null_convenience)]

0 commit comments

Comments
 (0)