-
Notifications
You must be signed in to change notification settings - Fork 10.5k
FlattenSequence/distance(from:to:) untrapping Int.min #71912
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
FlattenSequence/distance(from:to:) untrapping Int.min #71912
Conversation
This patch makes it so FlattenSequence/distance(from:to:) can return Int.min without trapping.
A better, albeit a bit more verbose, method.
1. I added a file with some tests [test/stdlib/FlattenDistanceFromTo.swift]. 2. I used [test/stdlib/StaticBigInt.swift] as the template for the new file.
I've checked some more and not all let range: Range<UInt> = 0 ..< UInt.max/2 + 1
print(range.distance(from: range.endIndex, to: range.startIndex + 1)) // Int.min + 1
print(range.distance(from: range.endIndex, to: range.startIndex + 0)) // Int.min + 0 💥 But, it also looks like that might be fixed by (#71387): |
1. I removed some command-like comments. 2. I added a test for Int.min and Int.max distances.
@glessard can you please test and benchmark this? 🙏 I've untrapped |
@swift-ci please test |
@swift-ci please benchmark |
The |
Hm. I'm looking for FlattenDistanceFromToTests in the logs, but I can't find it. I also can't find any other |
1. FlattenSequence/distance(from:to:) cleanup. 2. FlattenDistanceFromToTests cleanup. 3. FlattenDistanceFromToTests availability checks.
I believe I have added appropriate availability checks in the most recent commit. |
@swift-ci please benchmark |
@swift-ci please test |
Performance (x86_64): -O
Code size: -O
Performance (x86_64): -Osize
Code size: -OsizePerformance (x86_64): -Onone
Code size: -swiftlibsHow to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
@swift-ci please smoke test macOS platform |
I think I can fix the I see at least two improvement opportunities:
I've performed some preliminary measurements, and it's ~10% faster on my machine. Edit: you may also perform a single negation at the end, but that is slower somehow... Edit: I have updated this PR with the above changes. |
1. Put the first region inside the if-else (same branch condition). 2. Use the forward distance in the highest region (as done in the middle).
@swift-ci please benchmark |
@swift-ci please test |
Performance (x86_64): -O
Code size: -O
Performance (x86_64): -Osize
Code size: -Osize
Performance (x86_64): -Onone
Code size: -swiftlibsHow to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
It appears the last commit closed the performance gap. It also added some code size, but perhaps that's OK? In saying this, I have ignored "FlattenListLoop" and "FlattenListFlatMap" because they never call |
Apologies if this is a silly question but (with the exception of code size) what units or by what factors is the performance being measured here? |
I don't remember the context for this pull request - other than wanting to un-trap the |
Thanks! |
@oscbyspro Thanks for this! Sorry for the long delay. |
This patch makes it so FlattenSequence/distance(from:to:) can return
Int.min
without trapping. The (#71648) version trapsInt.min
because0...Int.max ~= count
. I have double-checked, however, and Range/distance(from:to:) (etc.) can returnInt.min
without trapping:I don't believe there are stdlib tests for this, as those would have been too slow before, but perhaps I could add some. But, I haven't yet had time to look at
StdlibUnittest
.