Skip to content

[stdlib] Mark contains methods on Range/ClosedRange transparent #81458

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lorentey
Copy link
Member

These are really popular shorthands for implementing bounds checks, but currently they aren’t even marked @inline(__always), so in larger functions they tend to remain outlined, defeating optimizations that would otherwise happen.

The corresponding variants on the partial range types are @_transparent; that helps unoptimized performance, so let’s apply the same attribute here.

rdar://151177326

These are really popular shorthands for implementing bounds checks, but currently they aren’t even marked `@inline(__always)`, so in larger functions they tend to remain outlined, defeating optimizations that would otherwise happen.

The corresponding variants on the partial range types are `@_transparent`; that helps unoptimized performance, so let’s apply the same attribute here.

rdar://151177326
@lorentey lorentey requested a review from a team as a code owner May 12, 2025 22:15
@lorentey
Copy link
Member Author

@swift-ci test

@lorentey
Copy link
Member Author

I'm not expecting any benchmark impact, but.

@swift-ci benchmark

@benrimmington
Copy link
Contributor

Should you also update:

  • contains(_:) in the PartialRangeFrom extension?

  • ~=(_:_:) in the RangeExpression extension?

@glessard
Copy link
Contributor

@swift-ci please benchmark

@glessard
Copy link
Contributor

Performance (x86_64): -O

Regression OLD NEW DELTA RATIO
ArrayAppendGenericStructs 1390.0 1765.0 +27.0% 0.79x (?)

Code size: -O

Performance (x86_64): -Osize

Regression OLD NEW DELTA RATIO
StringDistance.utf16.ascii 115.15 127.056 +10.3% 0.91x (?)

Code size: -Osize

Performance (x86_64): -Onone

Improvement OLD NEW DELTA RATIO
ClosedRangeContainsRange 15858.0 10739.0 -32.3% 1.48x
RangeContainsClosedRange 12057.0 10318.0 -14.4% 1.17x (?)
ArrayAppendReserved 2786.0 2530.0 -9.2% 1.10x (?)
Data.init.Sequence.809B.Count.RE.I 19137.0 17850.0 -6.7% 1.07x (?)

Code size: -swiftlibs

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.

4 participants