-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Vec::reserve(n) followed by n calls to push should only check capacity once #105156
Copy link
Copy link
Open
Labels
A-codegenArea: Code generationArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-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-codegenArea: Code generationArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-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 in compiler explorer (https://rust.godbolt.org/z/W35vT4osM):
I expected the generated assembly to only check the capacity once, and reallocate if needed, and then write the value. Instead, for each
pushthere is another check of remaining capacity and potential call toreserve_for_push.Adding explicit
assumecalls leads to much shorter code with only a single call todo_reserve_and_handle.This seems similar to #82801 but that issue seems specific to the initial vector capacity and code for that case looks ok now.
Meta
rustc --version --verbose: