Skip to content

Commit 7a5dbef

Browse files
committed
[stdlib] Mark contains methods on Range/ClosedRange transparent
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
1 parent ecad1d9 commit 7a5dbef

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

stdlib/public/core/ClosedRange.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension ClosedRange: RangeExpression {
128128
/// - Parameter element: The element to check for containment.
129129
/// - Returns: `true` if `element` is contained in the range; otherwise,
130130
/// `false`.
131-
@inlinable
131+
@_transparent
132132
public func contains(_ element: Bound) -> Bool {
133133
return element >= self.lowerBound && element <= self.upperBound
134134
}
@@ -357,6 +357,7 @@ where Bound: Strideable, Bound.Stride: SignedInteger
357357
///
358358
/// - Complexity: O(1)
359359
@_alwaysEmitIntoClient
360+
@_transparent
360361
public func contains(_ other: Range<Bound>) -> Bool {
361362
if other.isEmpty { return true }
362363
let otherInclusiveUpper = other.upperBound.advanced(by: -1)
@@ -383,6 +384,7 @@ extension ClosedRange {
383384
///
384385
/// - Complexity: O(1)
385386
@_alwaysEmitIntoClient
387+
@_transparent
386388
public func contains(_ other: ClosedRange<Bound>) -> Bool {
387389
lowerBound <= other.lowerBound && upperBound >= other.upperBound
388390
}

stdlib/public/core/Range.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct Range<Bound: Comparable> {
194194
/// - Parameter element: The element to check for containment.
195195
/// - Returns: `true` if `element` is contained in the range; otherwise,
196196
/// `false`.
197-
@inlinable
197+
@_transparent
198198
public func contains(_ element: Bound) -> Bool {
199199
return lowerBound <= element && element < upperBound
200200
}
@@ -1074,6 +1074,7 @@ extension Range {
10741074
///
10751075
/// - Complexity: O(1)
10761076
@_alwaysEmitIntoClient
1077+
@_transparent
10771078
public func contains(_ other: Range<Bound>) -> Bool {
10781079
other.isEmpty ||
10791080
(lowerBound <= other.lowerBound && upperBound >= other.upperBound)
@@ -1101,6 +1102,7 @@ extension Range {
11011102
///
11021103
/// - Complexity: O(1)
11031104
@_alwaysEmitIntoClient
1105+
@_transparent
11041106
public func contains(_ other: ClosedRange<Bound>) -> Bool {
11051107
lowerBound <= other.lowerBound && upperBound > other.upperBound
11061108
}

0 commit comments

Comments
 (0)