Skip to content

Commit a847713

Browse files
committed
Faster processor resets
Make processor reset faster by tracking dirty registers and only clearing Arrays that are non-empty.
1 parent 2c836cd commit a847713

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ extension Processor {
139139

140140
self.registers.reset(sentinel: searchBounds.upperBound)
141141

142-
self.savePoints.removeAll(keepingCapacity: true)
143-
self.callStack.removeAll(keepingCapacity: true)
142+
if !self.savePoints.isEmpty {
143+
self.savePoints.removeAll(keepingCapacity: true)
144+
}
145+
if !self.callStack.isEmpty {
146+
self.callStack.removeAll(keepingCapacity: true)
147+
}
144148

145149
for idx in storedCaptures.indices {
146150
storedCaptures[idx] = .init()

Sources/_StringProcessing/Engine/Registers.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ extension Processor {
4141

4242
// MARK: writeable, resettable
4343

44+
var isDirty = false
45+
4446
// currently, useful for range-based quantification
4547
var ints: [Int]
4648

@@ -58,17 +60,22 @@ extension Processor.Registers {
5860
}
5961
subscript(_ i: IntRegister) -> Int {
6062
get { ints[i.rawValue] }
61-
set { ints[i.rawValue] = newValue }
63+
set {
64+
isDirty = true
65+
ints[i.rawValue] = newValue
66+
}
6267
}
6368
subscript(_ i: ValueRegister) -> Any {
6469
get { values[i.rawValue] }
6570
set {
71+
isDirty = true
6672
values[i.rawValue] = newValue
6773
}
6874
}
6975
subscript(_ i: PositionRegister) -> Input.Index {
7076
get { positions[i.rawValue] }
7177
set {
78+
isDirty = true
7279
positions[i.rawValue] = newValue
7380
}
7481
}
@@ -128,6 +135,9 @@ extension Processor.Registers {
128135
}
129136

130137
mutating func reset(sentinel: Input.Index) {
138+
guard isDirty else {
139+
return
140+
}
131141
self.ints._setAll(to: 0)
132142
self.values._setAll(to: SentinelValue())
133143
self.positions._setAll(to: Processor.Registers.sentinelIndex)

0 commit comments

Comments
 (0)