@@ -3,33 +3,40 @@ import Foundation
3
3
4
4
protocol RegexBenchmark {
5
5
var name : String { get }
6
- mutating func compile( )
7
6
func run( )
8
7
func debug( )
9
8
}
10
9
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
+ }
16
16
17
- enum MatchType {
18
- case whole
19
- case first
20
- case allMatches
21
- }
22
-
17
+ extension SwiftRegexBenchmark {
23
18
mutating func compile( ) {
24
19
let _ = regex. _forceAction ( . recompile)
25
20
}
26
-
27
21
mutating func enableTracing( ) {
28
22
let _ = regex. _forceAction ( . addOptions( . enableTracing) )
29
23
}
30
24
mutating func enableMetrics( ) {
31
25
let _ = regex. _forceAction ( . addOptions( [ . enableMetrics, . disableOptimizations] ) )
32
26
}
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
+ }
33
40
34
41
func run( ) {
35
42
switch type {
@@ -55,9 +62,6 @@ struct NSBenchmark: RegexBenchmark {
55
62
case first
56
63
}
57
64
58
- // Not measured for NSRegularExpression
59
- mutating func compile( ) { }
60
-
61
65
func run( ) {
62
66
switch type {
63
67
case . allMatches: blackHole ( regex. matches ( in: target, range: range) )
@@ -67,20 +71,10 @@ struct NSBenchmark: RegexBenchmark {
67
71
}
68
72
69
73
/// A benchmark running a regex on strings in input set
70
- struct InputListBenchmark : RegexBenchmark {
74
+ struct InputListBenchmark : SwiftRegexBenchmark {
71
75
let name : String
72
76
var regex : Regex < AnyRegexOutput >
73
77
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
- }
84
78
85
79
func run( ) {
86
80
for target in targets {
@@ -103,8 +97,6 @@ struct InputListNSBenchmark: RegexBenchmark {
103
97
func range( in target: String ) -> NSRange {
104
98
NSRange ( target. startIndex..< target. endIndex, in: target)
105
99
}
106
-
107
- mutating func compile( ) { }
108
100
109
101
func run( ) {
110
102
for target in targets {
0 commit comments