You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Vec::reserve(n) followed by n calls to push should only check capacity once #105156
A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: 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.
I expected the generated assembly to only check the capacity once, and reallocate if needed, and then write the value. Instead, for each push there is another check of remaining capacity and potential call to reserve_for_push.
Adding explicit assume calls leads to much shorter code with only a single call to do_reserve_and_handle.
Just ran into this too. Here's the generalization:
pubfninsert(v:&mutVec<usize>,n:usize){
v.reserve(n);for i in0..n {
v.push(i);}}pubfncreate(n:usize) -> Vec<usize>{letmut v = Vec::with_capacity(n);for i in0..n {
v.push(i);}
v
}
tl;dr: A single assume in Vec's reserve method should've been enough to actually optimize out all further bound checks. But the big caveat here is that llvm isn't actually able to apply these optimizations with its default pipeline of passes. The constraint elimination pass must be run several times interleaved with other optimizations. This could be a limitation in the constraint elimination pass itself
A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: 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.
Activity
SUPERCILEX commentedon Aug 15, 2023
Just ran into this too. Here's the generalization:
SUPERCILEX commentedon Aug 15, 2023
Fixed with this:
So should be handled by #82801
Auto merge of rust-lang#121300 - c410-f3r:perf-try, r=<try>
c410-f3r commentedon Feb 20, 2024
An example that eliminates branches: https://godbolt.org/z/TTEv73o44
Change
v.allocate(4);to any number less than 4 to see the compiler insert guards accordingly.For whatever reason, the trick is in the use of
unchecked_addinallocateas hinted at #121300 (comment).cynecx commentedon Feb 20, 2024
I'd like to leave some more observations I've noticed while playing around with this: https://gist.github.com/cynecx/24e3a4ae8756fdf9257a6cbf50005e08
tl;dr: A single
assumein Vec'sreservemethod should've been enough to actually optimize out all further bound checks. But the big caveat here is that llvm isn't actually able to apply these optimizations with its default pipeline of passes. The constraint elimination pass must be run several times interleaved with other optimizations. This could be a limitation in the constraint elimination pass itself