-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Special-case format!("{}", string_like) for increased performance #52804
Copy link
Copy link
Open
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Changes like #52767 and existence of the useless_format clippy lint show that people end up writing
format!("{}", a_string)way more often than they should (i.e., >0 times).Since
formatis a proc macro/builtin in the compiler, and since this specific case should not have any side effects other than producing aString, I want to propose adding this special case:When expanding a call of the
formatmacro (and before usingformat_args!), check if the formatting string is literally"{}", and, if the only other parameterxis either of type&str,String, orCow<str>, expand the macro toToString::to_string(x).This is quite a conservative and concrete refinement. It could easily be adjusted to support more string-like types.
And additional special case to consider is
format!("foo")(with only a literal formatting string). It should expand toString::from("foo").