-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Evaluate std::fmt::Arguments::new_const()
during Compile Time
#131663
base: master
Are you sure you want to change the base?
Evaluate std::fmt::Arguments::new_const()
during Compile Time
#131663
Conversation
r? @chenyukang rustbot has assigned @chenyukang. Use |
@@ -333,7 +333,7 @@ pub struct Arguments<'a> { | |||
#[unstable(feature = "fmt_internals", issue = "none")] | |||
impl<'a> Arguments<'a> { | |||
#[inline] | |||
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")] | |||
#[rustc_const_stable(feature = "const_fmt_arguments_new", since = "CURRENT_RUSTC_VERSION")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this warrants a separate stabilization PR because it's an internal only API behind [feature(fmt_internals)]
.
@bors try @rust-timer queue |
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
…<try> Evaluate `std::fmt::Arguments::new_const()` during Compile Time Fixes rust-lang#128709 This PR aims to optimize calls to string formating macros without any arguments by evaluating `std::fmt::Arguments::new_const()` in a const context. Currently, `println!("hola")` compiles to `std::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))`. With this PR, `println!("hola")` compiles to `std::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })`. This is accomplished in two steps: 1. Const stabilize `std::fmt::Arguments::new_const()`. 2. Wrap calls to `std::fmt::Arguments::new_const()` in an inline const block when lowering the AST to HIR. This reduces the generated code to a `memcpy` instead of multiple `getelementptr` and `store` instructions even with `-C no-prepopulate-passes -C opt-level=0`. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6c This is a safe and sound transformation because `std::fmt::Arguments::new_const()` is a trivial constructor function taking a slice containing a `'static` string literal as input. CC rust-lang#99012
This comment has been minimized.
This comment has been minimized.
7638733
to
d235b47
Compare
This comment has been minimized.
This comment has been minimized.
d235b47
to
96bdf88
Compare
This comment has been minimized.
This comment has been minimized.
96bdf88
to
ff11342
Compare
Some changes occurred in coverage tests. cc @Zalathar |
This comment has been minimized.
This comment has been minimized.
ff11342
to
558d241
Compare
This comment has been minimized.
This comment has been minimized.
558d241
to
f361a33
Compare
This comment has been minimized.
This comment has been minimized.
f361a33
to
95b1d81
Compare
This PR modifies cc @jieyouxu |
Fixes #128709
This PR aims to optimize calls to string formating macros without any arguments by evaluating
std::fmt::Arguments::new_const()
in a const context.Currently,
println!("hola")
compiles tostd::io::_print(std::fmt::Arguments::new_const(&["hola\n"]))
.With this PR,
println!("hola")
compiles tostd::io::_print(const { std::fmt::Arguments::new_const(&["hola\n"]) })
.This is accomplished in two steps:
std::fmt::Arguments::new_const()
.std::fmt::Arguments::new_const()
in an inline const block when lowering the AST to HIR.This reduces the generated code to a
memcpy
instead of multiplegetelementptr
andstore
instructions even with-C no-prepopulate-passes -C opt-level=0
. Godbolt for code comparison: https://rust.godbolt.org/z/P7Px7de6cThis is a safe and sound transformation because
std::fmt::Arguments::new_const()
is a trivial constructor function taking a slice containing a'static
string literal as input.CC #99012