Simplify slice::Iter::next enough that it inlines - #136771
Conversation
commented
Feb 9, 2025
|
Let's see whether it actually improves things: |
This comment has been minimized.
This comment has been minimized.
commented
Feb 9, 2025
This comment has been minimized.
This comment has been minimized.
commented
Feb 9, 2025
|
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
commented
Feb 9, 2025
|
Finished benchmarking commit (30df00c): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking 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. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -2.2%, secondary -2.5%)This 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.
CyclesResults (primary -1.0%, secondary 1.7%)This 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 sizeResults (primary 0.1%, secondary -0.1%)This 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: 780.488s -> 778.559s (-0.25%) |
f7970b3 to
85deb4d
Compare
| pub fn slice_iter_next<'a>(it: &mut std::slice::Iter<'a, u32>) -> Option<&'a u32> { | ||
| // CHECK: %[[ENDP:.+]] = getelementptr inbounds{{( nuw)?}} i8, ptr %it, {{i32 4|i64 8}} | ||
| // CHECK: %[[END:.+]] = load ptr, ptr %[[ENDP]] | ||
| // CHECK: %[[START:.+]] = load ptr, ptr %it, |
There was a problem hiding this comment.
Rebased atop the transmute-gives-asserts change; codegen tests should be passing now with just this trivial change that it's loading the start pointer first instead of the end pointer first.
| let mut _0: (); | ||
| let mut _11: std::slice::Iter<'_, T>; | ||
| let mut _12: std::iter::Enumerate<std::slice::Iter<'_, T>>; | ||
| let mut _13: std::iter::Enumerate<std::slice::Iter<'_, T>>; |
There was a problem hiding this comment.
Nice to see that the Enumerate iterators get completely SRoAed even just in MIR, with this!
commented
Feb 11, 2025
|
Just to check that having the assumes in the LLVM-IR doesn't somehow lose all of the gains: |
This comment has been minimized.
This comment has been minimized.
commented
Feb 11, 2025
commented
Feb 11, 2025
|
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
commented
Feb 11, 2025
|
Finished benchmarking commit (eaf73cd): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking 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. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -1.8%, secondary -3.2%)This 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.
CyclesResults (primary -1.4%, secondary 2.1%)This 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 sizeResults (primary 0.0%, secondary -0.2%)This 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: 785.339s -> 785.217s (-0.02%) |
| // safe since we check if the iterator is empty first. | ||
| let ptr = self.ptr; | ||
| let end_or_len = self.end_or_len; | ||
| // SAFETY: Type invariants. |
There was a problem hiding this comment.
The safety comment is a bit too sloppy imo. At least it should say something like "same as above" if you want to avoid repetition. Or maybe split it into two unsafe blocks, one for each arm.
There was a problem hiding this comment.
Yeah, true.
Weirdly when I added tighter-scoped unsafe blocks it stopped inlining (and I even rebuilt to check because that's so strange), but I added more specific comments inside a bigger block.
…ffset` Probably reasonable anyway since it more obviously drops provenance.
This adds a few more statements to `next`, but optimizes better in the loops (saving 2 blocks in `forward_loop`, for example)
4ddbbbd to
7add358
Compare
commented
Feb 20, 2025
|
@bors r=joboet |
commented
Feb 20, 2025
commented
Feb 20, 2025
commented
Feb 20, 2025
|
☀️ Test successful - checks-actions |
commented
Feb 20, 2025
|
Finished benchmarking commit (f04bbc6): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowOur benchmarks found a performance regression caused by this PR. Next Steps:
@rustbot label: +perf-regression Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -2.1%)This 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.
CyclesResults (primary -1.2%, secondary 0.9%)This 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 sizeResults (primary -0.1%, secondary -0.3%)This 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: 774.026s -> 774.477s (0.06%) |
commented
Feb 25, 2025
|
Perf improvements vastly outweigh the regressions @rustbot label: +perf-regression-triaged |

Inspired by this zulip conversation: https://rust-lang.zulipchat.com/#narrow/channel/189540-t-compiler.2Fwg-mir-opt/topic/Feedback.20on.20a.20MIR.20optimization.20idea/near/498579990
Draft for now because it needs #136735 to get the codegen tests to pass.