Skip to content

Commit 4aab786

Browse files
committed
Remove compilation bottleneck
Benchmarks on Xcode 12.4 showed compiling / type checking StrideCollection.offsetBackward was the bottleneck for building the package. Replacing the ternary operator with the longer form, removes the bottleneck.
1 parent 2327673 commit 4aab786

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Sources/Algorithms/Stride.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,14 @@ 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 that stresses out the compiler.
230+
// To avoid slow build times for consumers, we use the longhand below.
231+
let distance: Int
232+
if i == endIndex {
233+
distance = -((base.count - 1) % stride + 1) + (n - 1) * -stride
234+
} else {
235+
distance = n * -stride
236+
}
232237
return base.index(
233238
i.base,
234239
offsetBy: distance,

0 commit comments

Comments
 (0)