Skip to content

Conversation

jmschonfeld
Copy link
Contributor

This is a tiny optimization in conversion from Data to a RawSpan that elides unnecessary overflow checks when performing arithmetic on the bounds/offsets of the bytes pointer. Given the constraints that the Range type guarantees (lowerBound <= upperBound) and the constraint that all Data indices are non-negative, we can avoid performing overflow checking on these operations since the possible inputs can never overflow. Runtime-wise the other parts of this conversion (copying the representation, switching over the cases, etc.) outweigh these overflow checks. However, I did find that removing these checks resulted in a 15% reduction in code size in the caller and as we remove the other heavy pieces these will become more prominent. As we heavily promote using Spans in API this conversion will become much more common and I think is worthy of a micro optimization like this to ensure our best performance.

@jmschonfeld jmschonfeld merged commit 835789d into swiftlang:future Oct 15, 2025
16 checks passed
@jmschonfeld jmschonfeld deleted the data-span-overflow branch October 15, 2025 21:15
Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants