Skip to content

Commit 9c8e567

Browse files
committed
Add conditional compilation for metric measuring
Ideally this would be done in Package.swift instead of having to add a flag to swift build but alas it appears that it is not possible - https://forums.swift.org/t/swiftpm-and-conditional-compilation/36874 - https://forums.swift.org/t/compilation-conditions-and-swift-packages/34627/4
1 parent 3d0789f commit 9c8e567

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Sources/RegexBenchmark/CLI.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ struct Runner: ParsableCommand {
3838
@Flag(help: "Exclude running NSRegex benchmarks")
3939
var excludeNs = false
4040

41-
@Flag(help: "Enable tracing of the engine (warning: lots of output)")
41+
@Flag(help: """
42+
Enable tracing of the engine (warning: lots of output). Prints out processor state each cycle
43+
44+
Note: swift-experimental-string-processing must be built with processor measurements enabled
45+
46+
swift build -c release -Xswiftc -DPROCESSOR_MEASUREMENTS_ENABLED
47+
""")
4248
var enableTracing: Bool = false
4349

44-
@Flag(help: "Enable engine metrics (warning: lots of output)")
50+
@Flag(help: """
51+
Enable engine metrics (warning: lots of output). Prints out cycle count, instruction counts, number of backtracks
52+
53+
Note: swift-experimental-string-processing must be built with processor measurements enabled
54+
55+
swift build -c release -Xswiftc -DPROCESSOR_MEASUREMENTS_ENABLED
56+
""")
4557
var enableMetrics: Bool = false
4658

4759
@Flag(help: "Include firstMatch benchmarks in CrossBenchmark (off by default")

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ extension Processor {
400400
mutating func cycle() {
401401
_checkInvariants()
402402
assert(state == .inProgress)
403+
404+
#if PROCESSOR_MEASUREMENTS_ENABLED
403405
if cycleCount == 0 {
404406
trace()
405407
measureMetrics()
@@ -410,6 +412,8 @@ extension Processor {
410412
measureMetrics()
411413
_checkInvariants()
412414
}
415+
#endif
416+
413417
let (opcode, payload) = fetch().destructure
414418
switch opcode {
415419
case .invalid:

Sources/_StringProcessing/Executor.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ struct Executor {
3030
input: input,
3131
subjectBounds: subjectBounds,
3232
searchBounds: searchBounds)
33+
#if PROCESSOR_MEASUREMENTS_ENABLED
3334
defer { if cpu.shouldMeasureMetrics { cpu.printMetrics() } }
35+
#endif
3436
var low = searchBounds.lowerBound
3537
let high = searchBounds.upperBound
3638
while true {
@@ -57,7 +59,9 @@ struct Executor {
5759
) throws -> Regex<Output>.Match? {
5860
var cpu = engine.makeProcessor(
5961
input: input, bounds: subjectBounds, matchMode: mode)
62+
#if PROCESSOR_MEASUREMENTS_ENABLED
6063
defer { if cpu.shouldMeasureMetrics { cpu.printMetrics() } }
64+
#endif
6165
return try _match(input, from: subjectBounds.lowerBound, using: &cpu)
6266
}
6367

0 commit comments

Comments
 (0)