Skip to content

Commit 2ada1d5

Browse files
committed
Refactor metrics out, and void their storage in release builds
1 parent 0e49e35 commit 2ada1d5

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

Sources/_StringProcessing/Engine/Metrics.swift

Lines changed: 62 additions & 3 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:")
@@ -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
}

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ struct Processor {
8686

8787
var failureReason: Error? = nil
8888

89-
// MARK: Metrics, debugging, etc.
90-
var cycleCount = 0
91-
var isTracingEnabled: Bool
92-
let shouldMeasureMetrics: Bool
93-
var metrics: ProcessorMetrics = ProcessorMetrics()
89+
var metrics: ProcessorMetrics
9490
}
9591

9692
extension Processor {
@@ -116,8 +112,11 @@ extension Processor {
116112
self.subjectBounds = subjectBounds
117113
self.searchBounds = searchBounds
118114
self.matchMode = matchMode
119-
self.isTracingEnabled = isTracingEnabled
120-
self.shouldMeasureMetrics = shouldMeasureMetrics
115+
116+
self.metrics = ProcessorMetrics(
117+
isTracingEnabled: isTracingEnabled,
118+
shouldMeasureMetrics: shouldMeasureMetrics)
119+
121120
self.currentPosition = searchBounds.lowerBound
122121

123122
// Initialize registers with end of search bounds
@@ -144,8 +143,8 @@ extension Processor {
144143

145144
self.state = .inProgress
146145
self.failureReason = nil
147-
148-
if shouldMeasureMetrics { metrics.resets += 1 }
146+
147+
metrics.addReset()
149148
_checkInvariants()
150149
}
151150

@@ -400,8 +399,8 @@ extension Processor {
400399
storedCaptures = capEnds
401400
registers.ints = intRegisters
402401
registers.positions = posRegisters
403-
404-
if shouldMeasureMetrics { metrics.backtracks += 1 }
402+
403+
metrics.addBacktrack()
405404
}
406405

407406
mutating func abort(_ e: Error? = nil) {
@@ -440,18 +439,8 @@ extension Processor {
440439
_checkInvariants()
441440
assert(state == .inProgress)
442441

443-
#if PROCESSOR_MEASUREMENTS_ENABLED
444-
if cycleCount == 0 {
445-
trace()
446-
measureMetrics()
447-
}
448-
defer {
449-
cycleCount += 1
450-
trace()
451-
measureMetrics()
452-
_checkInvariants()
453-
}
454-
#endif
442+
startCycleMetrics()
443+
defer { endCycleMetrics() }
455444

456445
let (opcode, payload) = fetch()
457446
switch opcode {

Sources/_StringProcessing/Engine/Tracing.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
13+
// TODO: Remove this protocol (and/or reuse it for something like a FastProcessor)
1214
extension Processor: TracedProcessor {
15+
var cycleCount: Int { metrics.cycleCount }
16+
var isTracingEnabled: Bool { metrics.isTracingEnabled }
17+
1318
var isFailState: Bool { state == .fail }
1419
var isAcceptState: Bool { state == .accept }
1520

Sources/_StringProcessing/Executor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Executor {
3131
subjectBounds: subjectBounds,
3232
searchBounds: searchBounds)
3333
#if PROCESSOR_MEASUREMENTS_ENABLED
34-
defer { if cpu.shouldMeasureMetrics { cpu.printMetrics() } }
34+
defer { if cpu.metrics.shouldMeasureMetrics { cpu.printMetrics() } }
3535
#endif
3636
var low = searchBounds.lowerBound
3737
let high = searchBounds.upperBound
@@ -60,7 +60,7 @@ struct Executor {
6060
var cpu = engine.makeProcessor(
6161
input: input, bounds: subjectBounds, matchMode: mode)
6262
#if PROCESSOR_MEASUREMENTS_ENABLED
63-
defer { if cpu.shouldMeasureMetrics { cpu.printMetrics() } }
63+
defer { if cpu.metrics.shouldMeasureMetrics { cpu.printMetrics() } }
6464
#endif
6565
return try _match(input, from: subjectBounds.lowerBound, using: &cpu)
6666
}

Sources/_StringProcessing/Utility/Traced.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// TODO: Place shared formatting and trace infrastructure here
1414

1515
protocol Traced {
16-
var isTracingEnabled: Bool { get set }
16+
var isTracingEnabled: Bool { get }
1717
}
1818

1919
protocol TracedProcessor: ProcessorProtocol, Traced {

0 commit comments

Comments
 (0)