Skip to content

Commit 70e9b96

Browse files
authored
[stdlib] Make Range.init(_: ClosedRange<Bound>) inlinable
Currently, a simple function such as: ```swift func makeSingleElementRange(n: Int) -> Range<Int> { return Range(n...n) } ``` will result in the following assembly under optimisation: ``` output.makeSingleElementRange(n: Swift.Int) -> Swift.Range<Swift.Int>: sub rsp, 40 mov qword ptr [rsp + 8], rdi mov qword ptr [rsp + 16], rdi call (lazy protocol witness table accessor for type Swift.Int and conformance Swift.Int : Swift.SignedInteger in Swift) mov rcx, rax mov rsi, qword ptr [rip + ($sSiN)@GOTPCREL] mov rdx, qword ptr [rip + ($sSiSxsWP)@GOTPCREL] lea rax, [rsp + 24] lea rdi, [rsp + 8] call ($sSnsSxRzSZ6StrideRpzrlEySnyxGSNyxGcfC)@plt mov rax, qword ptr [rsp + 24] mov rdx, qword ptr [rsp + 32] add rsp, 40 ret ``` Mark the `init` as inlinable so these functions can be properly optimised. Also add a missing line of documentation.
1 parent 847b6c0 commit 70e9b96

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

stdlib/public/core/Range.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ extension Range where Bound: Strideable, Bound.Stride: SignedInteger {
315315
/// For example, passing a closed range with an upper bound of `Int.max`
316316
/// triggers a runtime error, because the resulting half-open range would
317317
/// require an upper bound of `Int.max + 1`, which is not representable as
318+
/// an `Int`.
319+
@inlinable // trivial-implementation
318320
public init(_ other: ClosedRange<Bound>) {
319321
let upperBound = other.upperBound.advanced(by: 1)
320322
self.init(_uncheckedBounds: (lower: other.lowerBound, upper: upperBound))

0 commit comments

Comments
 (0)