Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize write with as_const_str for shorter code #122059

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
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,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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"const_str" is a very confusing name since const has a meaning in Rust and it's not this. We have very deliberately called the intrinsic is_val_statically_known, not is_val_const or anything like that. Would be nice if the functions using that intrinsic would follow the same pattern. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... as_statically_known_str?

Copy link
Member

@RalfJung RalfJung Mar 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be better IMO. (I'm also open to suggestions for a shorter term that captures this, but "constant" is too misleading IMO. In Rust, whether something is a "constant" is a language guarantee, and we have things like const fn that relate to that. In contrast, "statically known" is a very unstable notion depending on tons of factors that impact how good the compiler is able to analyze some piece of code. Also, const fn is not the same meaning of "const" as in "constant propagation" -- a common misconception that the standard library should not perpetuate. :)

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")]
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
Loading