Add Step::forward/backward_overflowing to enable RangeInclusive loop optimizations#155114
Add Step::forward/backward_overflowing to enable RangeInclusive loop optimizations#155114pitaj wants to merge 2 commits into
Step::forward/backward_overflowing to enable RangeInclusive loop optimizations#155114Conversation
This comment has been minimized.
This comment has been minimized.
f26532b to
d0c1f8d
Compare
This comment has been minimized.
This comment has been minimized.
d0c1f8d to
39e74a3
Compare
|
We discussed this in the @rust-lang/libs-api meeting. The If the |
|
Right, I forgot this implements Eq even though I implemented it manually 🤦♂️. @Amanieu I'm not sure it gains much over just going back to the derive, would that be acceptable? |
|
The derive is also acceptable. We explicitly document that |
It implements DoubleEndedIterator but regardless yeah I'll change it back to the derive |
|
@rustbot ready |
|
I found a bug with this PR. fn main() {
let mut range = 255_u8..=255_u8;
let _ = range.next();
println!("{range:?}");
println!("{}", range.contains(&100_u8));
}Output of above code on nightly: Output of above code with this PR: (The desired behavior of the |
|
And here's some strange behavior that's possibly a bug: fn main() {
let mut range = 0_usize..=0_usize;
let _ = range.next_back();
println!("{range:?}");
let data = [0; 1000];
println!("{:?}", &data[range]);
}Output of above code on nightly: Output of above code with this PR: |
|
|
||
| let (n, o) = Step::forward_overflowing(self.start.clone(), 1); | ||
|
|
||
| self.exhausted |= o; |
There was a problem hiding this comment.
| self.exhausted |= o; | |
| self.exhausted = o; |
self.exhausted is always false before this line.
| } | ||
| } | ||
| self.start = plus_1; | ||
| self.exhausted |= on | o1; |
There was a problem hiding this comment.
| self.exhausted |= on | o1; | |
| self.exhausted = on | o1; |
Same thing here
Good find on
This is expected behavior from the new overflowing behavior. Given that the values of
I think the I also changed Nominating for libs-api approval of those changes |
|
This was discussed the @rust-lang/libs-api meeting. We're happy with the @bors try |
|
FCP specifically for the changes to @rfcbot merge libs-api |
|
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Add `Step::forward/backward_overflowing` to enable RangeInclusive loop optimizations
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (1c26bd2): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -2.3%, secondary -0.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -3.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary 0.0%, secondary 0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 510.527s -> 508.082s (-0.48%) |
This comment has been minimized.
This comment has been minimized.
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
but RangeInclusive loops do not
and optimize the RangeInclusive iterator implementation with them This changes the `exhausted` field to represent an overflow flag for the bounds, essentially acting as an extra bit for `Idx`. This was found to enable optimizations previously only applicable to the exclusive-ended Range type. - change end_bound to return Excluded(start) when exhausted - add contains to tests - make into_bounds panic when exhausted matches From<legacy::RangeInclusive<T>> for RangeInclusive<T>
805bd01 to
c83fca5
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
View all comments
ACP: rust-lang/libs-team#767
This adds new required methods to the Step trait:
It was found that using these to implement RangeInclusive's Iterator impl enabled optimizations previously only applicable to the exclusive-ended
Range.This required changing how "exhaustion" works for
RangeInclusive. I've nominated this for libs-api discussion because of one insta-stable change:The new implementations now only set
exhaustedwhen overflow occurs, andstartis now advanced pastendotherwise. I doubt anyone depends on the prior behavior, but it's probably worth a crater run.The exhaustion changes also affect
Debugbut my understanding is that debug formatting is never guaranteed stable.I have now changed the
nthimpls to use the new functions as well.r? libs