[stdlib] ClosedRange.contains: Leave this force-inlined, not transparent

Making it transparent evidently induces new retain/release traffic in Array’s subscript, even though I can find no indication that `ClosedRange.contains` is ever called by that code path. Oh well.
This commit is contained in:
Karoy Lorentey
2025-06-05 15:05:25 -07:00
parent 487bacabc0
commit ff3cf98a2d
3 changed files with 14 additions and 11 deletions

View File

@@ -128,7 +128,8 @@ extension ClosedRange: RangeExpression {
/// - Parameter element: The element to check for containment.
/// - Returns: `true` if `element` is contained in the range; otherwise,
/// `false`.
@_transparent
@inlinable
@inline(__always)
public func contains(_ element: Bound) -> Bool {
return element >= self.lowerBound && element <= self.upperBound
}

View File

@@ -1080,7 +1080,7 @@ extension Range {
other.isEmpty ||
(lowerBound <= other.lowerBound && upperBound >= other.upperBound)
}
/// Returns a Boolean value indicating whether the given closed range is
/// contained within this range.
///

View File

@@ -2,10 +2,9 @@
// RUN: %target-swift-emit-ir -primary-file %s -O 2>&1 | %FileCheck --check-prefixes CHECK,CHECK-OPTIMIZED %s
// RUN: %target-swift-emit-ir -primary-file %s -Osize 2>&1 | %FileCheck --check-prefixes CHECK,CHECK-OPTIMIZED %s
// We expect calls to `Range.contains`, `ClosedRange.contains` over a
// `Range` instance to result in direct bound comparisons in all compilation
// modes, including unoptimized builds. (These are often used to implement
// bounds checking.)
// We expect calls to `Range.contains` to result in direct bound comparisons in
// all compilation modes, including unoptimized builds. (These are often used to
// implement bounds checking.)
//
// The sample functions below use bounds of different integer types to avoid
// them tail-calling each other.
@@ -19,11 +18,14 @@ public func halfOpenContains(_ r: Range<Int8>, _ i: Int8) -> Bool {
r.contains(i)
}
// CHECK-LABEL: define {{.*}} i1 @"$s21RangeContainsInlining06closedB0ySbSNys5Int16VG_ADtF"
// CHECK-NOT: call swiftcc
// CHECK: icmp
// CHECK-NOT: call swiftcc
// CHECK-LABEL: {{^}}}
// `ClosedRange.contains is only marked `@inline(__always)`; unfortunately it
// doesn't get inlined in deug builds.
// CHECK-OPTIMIZED-LABEL: define {{.*}} i1 @"$s21RangeContainsInlining06closedB0ySbSNys5Int16VG_ADtF"
// CHECK-OPTIMIZED-NOT: call swiftcc
// CHECK-OPTIMIZED: icmp
// CHECK-OPTIMIZED-NOT: call swiftcc
// CHECK-OPTIMIZED-LABEL: {{^}}}
public func closedContains(_ r: ClosedRange<Int16>, _ i: Int16) -> Bool {
r.contains(i)
}