Skip to content

Commit

Permalink
Remove compilation bottleneck for Swift 5.3.2 builds (#147)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iainsmith committed Jun 21, 2021
1 parent 2327673 commit 50be2c8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Sources/Algorithms/Stride.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,15 @@ extension StrideCollection: Collection {
offsetBy n: Int,
limitedBy limit: Index
) -> Index? {
let distance = i == endIndex
? -((base.count - 1) % stride + 1) + (n - 1) * -stride
: n * -stride
// We typically use the ternary operator but this significantly increases
// compile times when using Swift 5.3.2
// https://github.com/apple/swift-algorithms/issues/146
let distance: Int
if i == endIndex {
distance = -((base.count - 1) % stride + 1) + (n - 1) * -stride
} else {
distance = n * -stride
}
return base.index(
i.base,
offsetBy: distance,
Expand Down

0 comments on commit 50be2c8

Please sign in to comment.