-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
String::new() is slower than "".to_string() #86106
Copy link
Copy link
Closed
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.Category: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
I expected to see this happen: both functions contain identical code.
Instead, this happened: https://rust.godbolt.org/z/P53MMG4W6.
For
String::new()it does one more instruction:mov rcx, qword ptr [rip + .L__unnamed_1]. Thisrcxis then read further below atmov qword ptr [rdi], rcx.The
"".to_string()version however does not do this extra step and immediately reads from a constant:mov qword ptr [rdi], 1.This means that
String::new()does slightly more work than"".to_string()but I expect them to be identical.Before 1.52.0 the functions generated identical code: https://rust.godbolt.org/z/8647E6YhY.
oli noted the following:
Meta
This currently happens on nightly.