Skip to content

Commit 9062855

Browse files
committed
Cleanup benchmark protocols
1 parent 4b107a8 commit 9062855

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,40 @@ import Foundation
33

44
protocol RegexBenchmark {
55
var name: String { get }
6-
mutating func compile()
76
func run()
87
func debug()
98
}
109

11-
struct Benchmark: RegexBenchmark {
12-
let name: String
13-
var regex: Regex<AnyRegexOutput>
14-
let type: MatchType
15-
let target: String
10+
protocol SwiftRegexBenchmark: RegexBenchmark {
11+
var regex: Regex<AnyRegexOutput> { get set }
12+
mutating func compile()
13+
mutating func enableTracing()
14+
mutating func enableMetrics()
15+
}
1616

17-
enum MatchType {
18-
case whole
19-
case first
20-
case allMatches
21-
}
22-
17+
extension SwiftRegexBenchmark {
2318
mutating func compile() {
2419
let _ = regex._forceAction(.recompile)
2520
}
26-
2721
mutating func enableTracing() {
2822
let _ = regex._forceAction(.addOptions(.enableTracing))
2923
}
3024
mutating func enableMetrics() {
3125
let _ = regex._forceAction(.addOptions([.enableMetrics, .disableOptimizations]))
3226
}
27+
}
28+
29+
struct Benchmark: SwiftRegexBenchmark {
30+
let name: String
31+
var regex: Regex<AnyRegexOutput>
32+
let type: MatchType
33+
let target: String
34+
35+
enum MatchType {
36+
case whole
37+
case first
38+
case allMatches
39+
}
3340

3441
func run() {
3542
switch type {
@@ -55,9 +62,6 @@ struct NSBenchmark: RegexBenchmark {
5562
case first
5663
}
5764

58-
// Not measured for NSRegularExpression
59-
mutating func compile() {}
60-
6165
func run() {
6266
switch type {
6367
case .allMatches: blackHole(regex.matches(in: target, range: range))
@@ -67,20 +71,10 @@ struct NSBenchmark: RegexBenchmark {
6771
}
6872

6973
/// A benchmark running a regex on strings in input set
70-
struct InputListBenchmark: RegexBenchmark {
74+
struct InputListBenchmark: SwiftRegexBenchmark {
7175
let name: String
7276
var regex: Regex<AnyRegexOutput>
7377
let targets: [String]
74-
75-
mutating func compile() {
76-
blackHole(regex._forceAction(.recompile))
77-
}
78-
mutating func enableTracing() {
79-
let _ = regex._forceAction(.addOptions(.enableTracing))
80-
}
81-
mutating func enableMetrics() {
82-
let _ = regex._forceAction(.addOptions(.enableMetrics))
83-
}
8478

8579
func run() {
8680
for target in targets {
@@ -103,8 +97,6 @@ struct InputListNSBenchmark: RegexBenchmark {
10397
func range(in target: String) -> NSRange {
10498
NSRange(target.startIndex..<target.endIndex, in: target)
10599
}
106-
107-
mutating func compile() {}
108100

109101
func run() {
110102
for target in targets {

Sources/RegexBenchmark/BenchmarkRunner.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,7 @@ struct BenchmarkRunner {
1818
suite.append(benchmark)
1919
}
2020

21-
mutating func register(_ benchmark: Benchmark) {
22-
var benchmark = benchmark
23-
if enableTracing {
24-
benchmark.enableTracing()
25-
}
26-
if enableMetrics {
27-
benchmark.enableMetrics()
28-
}
29-
suite.append(benchmark)
30-
}
31-
32-
mutating func register(_ benchmark: InputListBenchmark) {
21+
mutating func register(_ benchmark: some SwiftRegexBenchmark) {
3322
var benchmark = benchmark
3423
if enableTracing {
3524
benchmark.enableTracing()
@@ -44,24 +33,28 @@ struct BenchmarkRunner {
4433
benchmark: some RegexBenchmark,
4534
samples: Int
4635
) -> BenchmarkResult {
47-
var benchmark = benchmark
4836
var runtimes: [Time] = []
49-
var compileTimes: [Time] = []
5037
// Initial run to make sure the regex has been compiled
5138
benchmark.run()
5239

53-
// Measure compilataion time
54-
for _ in 0..<samples {
55-
let start = Tick.now
56-
benchmark.compile()
57-
let end = Tick.now
58-
let time = end.elapsedTime(since: start)
59-
compileTimes.append(time)
40+
// Measure compilataion time for Swift regex
41+
let compileTime: Time
42+
if benchmark is SwiftRegexBenchmark {
43+
var benchmark = benchmark as! SwiftRegexBenchmark
44+
var compileTimes: [Time] = []
45+
for _ in 0..<samples {
46+
let start = Tick.now
47+
benchmark.compile()
48+
let end = Tick.now
49+
let time = end.elapsedTime(since: start)
50+
compileTimes.append(time)
51+
}
52+
compileTimes.sort()
53+
compileTime = compileTimes[samples/2]
54+
} else {
55+
compileTime = .zero
6056
}
6157

62-
compileTimes.sort()
63-
let compileTime = compileTimes[samples/2]
64-
6558
// FIXME: use suspendingclock?
6659
for _ in 0..<samples {
6760
let start = Tick.now

0 commit comments

Comments
 (0)