Skip to content

Commit 50be2c8

Browse files
authored
Remove compilation bottleneck for Swift 5.3.2 builds (#147)
Benchmarks on Xcode 12.4 showed compiling / type checking StrideCollection.offsetBackward was the bottleneck for building the package. Replacing the ternary operator in that function with an explicit if/else removes the bottleneck.
1 parent 2327673 commit 50be2c8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/Algorithms/Stride.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,15 @@ extension StrideCollection: Collection {
226226
offsetBy n: Int,
227227
limitedBy limit: Index
228228
) -> Index? {
229-
let distance = i == endIndex
230-
? -((base.count - 1) % stride + 1) + (n - 1) * -stride
231-
: n * -stride
229+
// We typically use the ternary operator but this significantly increases
230+
// compile times when using Swift 5.3.2
231+
// https://github.com/apple/swift-algorithms/issues/146
232+
let distance: Int
233+
if i == endIndex {
234+
distance = -((base.count - 1) % stride + 1) + (n - 1) * -stride
235+
} else {
236+
distance = n * -stride
237+
}
232238
return base.index(
233239
i.base,
234240
offsetBy: distance,

0 commit comments

Comments
 (0)