Skip to content

Commit d175e89

Browse files
committed
Remove linear factor from Engine's consume
20x perf speedup in the "BasicBacktrack" benchmarks.
1 parent 604bef8 commit d175e89

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ extension Processor {
108108
// Returns whether the advance succeeded. On failure, our
109109
// save point was restored
110110
mutating func consume(_ n: Distance) -> Bool {
111-
// Want Collection to provide this behavior...
112-
if input.distance(from: currentPosition, to: end) < n.rawValue {
111+
guard let idx = input.index(
112+
currentPosition, offsetBy: n.rawValue, limitedBy: end
113+
) else {
113114
signalFailure()
114115
return false
115116
}
116-
currentPosition = input.index(currentPosition, offsetBy: n.rawValue)
117+
currentPosition = idx
117118
return true
118119
}
119120

0 commit comments

Comments
 (0)