Optimize performance for string literals in Display::fmt#286
Merged
dtolnay merged 2 commits intodtolnay:masterfrom Feb 11, 2024
Merged
Optimize performance for string literals in Display::fmt#286dtolnay merged 2 commits intodtolnay:masterfrom
dtolnay merged 2 commits intodtolnay:masterfrom
Conversation
cb80183 to
9c712ab
Compare
Compiler is unable to generate as efficient code for `write!(f, "text")` as it does for `f.write_str("text")`. This PR checks if the `#[error("text")]` uses a simple string literal without the `{` and `}` characters, and without arguments, and uses `write_str` if so.
Owner
|
Published in 1.0.57. |
Contributor
Author
|
thanks for merging and releasing it so fast! Do you know if it would be possible to detect a more advanced case when the formatting string is by itself, and the only argument is a fn fmt(...) {
let s = "string";
write!("{0}", s)
}P.S. Obviously all these things should be eventually done in the compiler... 🤞 |
Owner
|
I do not know of a way to optimize that. |
asomers
added a commit
to asomers/bfffs
that referenced
this pull request
May 27, 2024
It would probably be worthwhile to create some new custom errors, but this commit doesn't do it yet. Also, update thiserror to take advantage of dtolnay/thiserror#286
asomers
added a commit
to asomers/bfffs
that referenced
this pull request
May 27, 2024
It would probably be worthwhile to create some new custom errors, but this commit doesn't do it yet. Also, update thiserror to take advantage of dtolnay/thiserror#286
takumi-earth
pushed a commit
to earthlings-dev/thiserror
that referenced
this pull request
Jan 27, 2026
Optimize performance for string literals in Display::fmt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Compiler is unable to generate as efficient code for
write!(f, "text")as it does forf.write_str("text"). This PR checks if the#[error("text")]uses a simple string literal without the{and}characters, and without arguments, and useswrite_strif so.Fixes #285