Skip to content

Commit 58626cc

Browse files
authored
Processor cleanup (#655)
Clean up and refactor the processor * Simplify instruction fetching * Refactor metrics out, and void their storage in release builds *Put operations onto String
1 parent 9ea9936 commit 58626cc

File tree

9 files changed

+210
-76
lines changed

9 files changed

+210
-76
lines changed

Sources/_StringProcessing/Engine/MEBuiltins.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extension Processor {
3030
}
3131

3232
func isAtStartOfLine(_ payload: AssertionPayload) -> Bool {
33+
// TODO: needs benchmark coverage
3334
if currentPosition == subjectBounds.lowerBound { return true }
3435
switch payload.semanticLevel {
3536
case .graphemeCluster:
@@ -40,6 +41,7 @@ extension Processor {
4041
}
4142

4243
func isAtEndOfLine(_ payload: AssertionPayload) -> Bool {
44+
// TODO: needs benchmark coverage
4345
if currentPosition == subjectBounds.upperBound { return true }
4446
switch payload.semanticLevel {
4547
case .graphemeCluster:
@@ -50,6 +52,8 @@ extension Processor {
5052
}
5153

5254
mutating func builtinAssert(by payload: AssertionPayload) throws -> Bool {
55+
// TODO: needs benchmark coverage
56+
5357
// Future work: Optimize layout and dispatch
5458
switch payload.kind {
5559
case .startOfSubject: return currentPosition == subjectBounds.lowerBound

Sources/_StringProcessing/Engine/MEQuantify.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ extension Processor {
33
var next: Input.Index?
44
switch payload.type {
55
case .bitset:
6-
next = _doMatchBitset(registers[payload.bitset])
6+
next = input.matchBitset(
7+
registers[payload.bitset], at: currentPosition, limitedBy: end)
78
case .asciiChar:
8-
next = _doMatchScalar(
9-
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
9+
next = input.matchScalar(
10+
UnicodeScalar.init(_value: UInt32(payload.asciiChar)),
11+
at: currentPosition,
12+
limitedBy: end,
13+
boundaryCheck: true)
1014
case .builtin:
1115
// We only emit .quantify if it consumes a single character
1216
next = input._matchBuiltinCC(
@@ -16,6 +20,7 @@ extension Processor {
1620
isStrictASCII: payload.builtinIsStrict,
1721
isScalarSemantics: false)
1822
case .any:
23+
// TODO: call out to existing code with quick check
1924
let matched = currentPosition != input.endIndex
2025
&& (!input[currentPosition].isNewline || payload.anyMatchesNewline)
2126
next = matched ? input.index(after: currentPosition) : nil

Sources/_StringProcessing/Engine/Metrics.swift

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,71 @@
11
extension Processor {
2+
#if PROCESSOR_MEASUREMENTS_ENABLED
23
struct ProcessorMetrics {
34
var instructionCounts: [Instruction.OpCode: Int] = [:]
45
var backtracks: Int = 0
56
var resets: Int = 0
7+
var cycleCount: Int = 0
8+
9+
var isTracingEnabled: Bool = false
10+
var shouldMeasureMetrics: Bool = false
11+
12+
init(isTracingEnabled: Bool, shouldMeasureMetrics: Bool) {
13+
self.isTracingEnabled = isTracingEnabled
14+
self.shouldMeasureMetrics = shouldMeasureMetrics
15+
}
616
}
7-
17+
#else
18+
struct ProcessorMetrics {
19+
var isTracingEnabled: Bool { false }
20+
var shouldMeasureMetrics: Bool { false }
21+
var cycleCount: Int { 0 }
22+
23+
init(isTracingEnabled: Bool, shouldMeasureMetrics: Bool) { }
24+
}
25+
#endif
26+
}
27+
28+
extension Processor {
29+
30+
mutating func startCycleMetrics() {
31+
#if PROCESSOR_MEASUREMENTS_ENABLED
32+
if metrics.cycleCount == 0 {
33+
trace()
34+
measureMetrics()
35+
}
36+
#endif
37+
}
38+
39+
mutating func endCycleMetrics() {
40+
#if PROCESSOR_MEASUREMENTS_ENABLED
41+
metrics.cycleCount += 1
42+
trace()
43+
measureMetrics()
44+
_checkInvariants()
45+
#endif
46+
}
47+
}
48+
49+
extension Processor.ProcessorMetrics {
50+
51+
mutating func addReset() {
52+
#if PROCESSOR_MEASUREMENTS_ENABLED
53+
self.resets += 1
54+
#endif
55+
}
56+
57+
mutating func addBacktrack() {
58+
#if PROCESSOR_MEASUREMENTS_ENABLED
59+
self.backtracks += 1
60+
#endif
61+
}
62+
}
63+
64+
extension Processor {
65+
#if PROCESSOR_MEASUREMENTS_ENABLED
866
func printMetrics() {
967
print("===")
10-
print("Total cycle count: \(cycleCount)")
68+
print("Total cycle count: \(metrics.cycleCount)")
1169
print("Backtracks: \(metrics.backtracks)")
1270
print("Resets: \(metrics.resets)")
1371
print("Instructions:")
@@ -21,7 +79,7 @@ extension Processor {
2179
}
2280

2381
mutating func measure() {
24-
let (opcode, _) = fetch().destructure
82+
let (opcode, _) = fetch()
2583
if metrics.instructionCounts.keys.contains(opcode) {
2684
metrics.instructionCounts[opcode]! += 1
2785
} else {
@@ -30,8 +88,9 @@ extension Processor {
3088
}
3189

3290
mutating func measureMetrics() {
33-
if shouldMeasureMetrics {
91+
if metrics.shouldMeasureMetrics {
3492
measure()
3593
}
3694
}
95+
#endif
3796
}

0 commit comments

Comments
 (0)