-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Indicate that multiplication in Layout::array cannot overflow #118228
Conversation
This allows LLVM to optimize comparisons to zero before & after the multiplication into one, saving on code size and eliminating an (always true) branch from most Vec allocations.
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Indicate that multiplication in Layout::array cannot overflow Since rust-lang#113113, we have added a check that skips calling into the allocator at all if `capacity == 0`. The global, default allocator will not actually try to allocate though; it returns a dangling pointer explicitly. However, these two checks are not merged/deduplicated by LLVM and so we're comparing to zero twice whenever vectors are allocated/grown. Probably cheap, but also potentially expensive in code size and seems like an unfortunate miss. This removes that extra check by telling LLVM that the multiplication as part of Layout::array can't overflow, turning the original non-zero value into a zero value afterwards. In my checks locally this successfully drops the duplicate comparisons. See https://rust.godbolt.org/z/b6nPP9dcK for a code example. ```rust pub fn foo(elements: usize) -> Vec<u32> { Vec::with_capacity(elements) } ``` r? `@scottmcm` since you touched this in a32305a - curious if you have thoughts on doing this / can confirm my model of this being correct.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (bc314d2): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 675.686s -> 674.254s (-0.21%) |
Hmm, interesting, I would have expected LLVM could just do this. *goes off to investigate* Looks like phase ordering strikes again. SimplifyCFG (https://rust.godbolt.org/z/e8Y9ohfro) optimizes it into a form where it's no longer legal to make it Sounds like this is worth doing, then. This makes me want to try other options for how to write the checks again (like #100866), so I'd kinda like to have a codegen test for this, but I can't think of one that's more than "does this have |
…=<try> Use `usize::checked_mul` for `Layout::array` checks again I found a little trick that allows us to check the `isize::MAX` limit using `usize::checked_mul`. No idea whether that's a good idea, but let's see what perf has to say... Inspired by rust-lang#118228 r? `@ghost`
☀️ Test successful - checks-actions |
Finished benchmarking commit (b06258c): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 678.134s -> 674.301s (-0.57%) |
Since #113113, we have added a check that skips calling into the allocator at all if
capacity == 0
. The global, default allocator will not actually try to allocate though; it returns a dangling pointer explicitly. However, these two checks are not merged/deduplicated by LLVM and so we're comparing to zero twice whenever vectors are allocated/grown. Probably cheap, but also potentially expensive in code size and seems like an unfortunate miss.This removes that extra check by telling LLVM that the multiplication as part of Layout::array can't overflow, turning the original non-zero value into a zero value afterwards. In my checks locally this successfully drops the duplicate comparisons.
See https://rust.godbolt.org/z/b6nPP9dcK for a code example.
r? @scottmcm since you touched this in a32305a - curious if you have thoughts on doing this / can confirm my model of this being correct.