-
Notifications
You must be signed in to change notification settings - Fork 13.4k
refactor: replace format! with concat! for string literals #134212
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @SparrowLii (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt The Miri subtree was changed cc @rust-lang/miri |
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.
I don't find this change to be more readable, and I don't think there will be any observable change in performance to justify the change on perf grounds as a tradeoff.
|
The perf difference rounds to zero when the code in question already takes up almost no time. Outside of the couple of cases I indicated where this change is not valid, basically all places you made the change are either tests or in code that only runs when you request a help message. The performance of tests doesn't matter and nobody is going to notice a help message taking like 10μs longer to render due to an extra allocation. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
What about this change? diff --git a/src/tools/clippy/lintcheck/src/main.rs b/src/tools/clippy/lintcheck/src/main.rs
index 8c62dd3ed38..bf3f8ca9802 100644
--- a/src/tools/clippy/lintcheck/src/main.rs
+++ b/src/tools/clippy/lintcheck/src/main.rs
@@ -88,15 +88,13 @@ fn run_clippy_lints(
);
}
- let cargo_home = env!("CARGO_HOME");
-
// `src/lib.rs` -> `target/lintcheck/sources/crate-1.2.3/src/lib.rs`
let remap_relative = format!("={}", self.path.display());
// Fallback for other sources, `~/.cargo/...` -> `$CARGO_HOME/...`
- let remap_cargo_home = format!("{cargo_home}=$CARGO_HOME");
+ let remap_cargo_home = concat!(env!("CARGO_HOME"), "=$CARGO_HOME");
// `~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/crate-2.3.4/src/lib.rs`
// -> `crate-2.3.4/src/lib.rs`
- let remap_crates_io = format!("{cargo_home}/registry/src/index.crates.io-6f17d22bba15001f/=");
+ let remap_crates_io = concat!(env!("CARGO_HOME"), "/registry/src/index.crates.io-6f17d22bba15001f/=");
let mut clippy_args = vec![
"--remap-path-prefix",
|
Lintcheck is a tool that is only used during development of Clippy: https://github.com/rust-lang/rust-clippy/blob/master/lintcheck/README.md |
I think this is a case of premature optimization. While you are correct about the unnecessary allocations, all the use cases are not going to benefit us (too small, mostly in error, code) Thus I'm closing this PR. Please don't hesitate to look for more avenues for optimizations. If you want to get opinions on ideas before doing work, feel free to start a thread on Zulip in the t-compiler stream |
Although
format!
macro is very powerful, it introduces overhead of memory allocation on the heap compared to&str
. Therefore, whenformat!
macro is unnecessary,concat!
is a better choice in terms of performance.