Skip to content

Commit 1384a69

Browse files
committed
Benchmarking housekeeping
1 parent 4ea430c commit 1384a69

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public struct Benchmark: RegexBenchmark {
1515
public enum MatchType {
1616
case whole
1717
case first
18-
case enumerate
18+
case allMatches
1919
}
2020

2121
public func run() {
2222
switch ty {
2323
case .whole: blackHole(target.wholeMatch(of: regex))
24-
case .enumerate: blackHole(target.matches(of: regex))
24+
case .allMatches: blackHole(target.matches(of: regex))
2525
case .first: blackHole(target.firstMatch(of: regex))
2626
}
2727
}
@@ -54,14 +54,14 @@ public struct BenchmarkRunner {
5454
// Register instances of Benchmark and run them
5555
let suiteName: String
5656
var suite: [any RegexBenchmark]
57-
let samples: Int = 40
57+
let samples: Int = 20
5858

5959
public init(suiteName: String) {
6060
self.suiteName = suiteName
6161
self.suite = []
6262
}
6363

64-
public mutating func register(new: some RegexBenchmark) {
64+
public mutating func register(_ new: some RegexBenchmark) {
6565
suite.append(new)
6666
}
6767

Sources/RegexBenchmark/Suite/Backtracking.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension BenchmarkRunner {
1212
let basicBacktrack = Benchmark(
1313
name: "BasicBacktrack",
1414
regex: try! Regex(r),
15-
ty: .enumerate,
15+
ty: .allMatches,
1616
target: s
1717
)
1818

@@ -37,9 +37,9 @@ extension BenchmarkRunner {
3737
target: s
3838
)
3939

40-
register(new: basicBacktrack)
41-
register(new: basicBacktrackNS)
42-
register(new: basicBacktrackFirstMatch)
43-
register(new: basicBacktrackNSFirstMatch)
40+
register(basicBacktrack)
41+
register(basicBacktrackNS)
42+
register(basicBacktrackFirstMatch)
43+
register(basicBacktrackNSFirstMatch)
4444
}
4545
}

Sources/RegexBenchmark/Suite/CssRegex.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ extension BenchmarkRunner {
16561656
let cssRegex = Benchmark(
16571657
name: "cssRegex",
16581658
regex: try! Regex(r),
1659-
ty: .enumerate,
1659+
ty: .allMatches,
16601660
target: css
16611661
)
16621662

@@ -1666,7 +1666,7 @@ extension BenchmarkRunner {
16661666
ty: .all,
16671667
target: css
16681668
)
1669-
register(new: cssRegex)
1670-
register(new: cssRegexNS)
1669+
register(cssRegex)
1670+
register(cssRegexNS)
16711671
}
16721672
}

Sources/RegexBenchmark/Suite/FirstMatch.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,44 @@ extension BenchmarkRunner {
66
let r = "a"
77
let s = String(repeating: " ", count: 100000)
88

9+
// this does nothing but loop through the loop in
10+
// Match.swift (Regex._firstMatch) since the engine should fail right away,
911
let firstMatch = Benchmark(
1012
name: "FirstMatch",
1113
regex: try! Regex(r),
1214
ty: .first,
1315
target: s
1416
)
17+
18+
// a comparison with now NSRegularExpression handles this situation
1519
let firstMatchNS = NSBenchmark(
1620
name: "FirstMatchNS",
1721
regex: try! NSRegularExpression(pattern: r),
1822
ty: .first,
1923
target: s
2024
)
2125

22-
register(new: firstMatch)
23-
register(new: firstMatchNS)
26+
let s2 = String(repeating: "a", count: 10000)
27+
28+
// matches calls into firstMatch, so really they're the same
29+
// this also stress tests the captures
30+
let allMatches = Benchmark(
31+
name: "AllMatches",
32+
regex: try! Regex(r),
33+
ty: .allMatches,
34+
target: s2
35+
)
36+
37+
let allMatchesNS = NSBenchmark(
38+
name: "AllMatchesNS",
39+
regex: try! NSRegularExpression(pattern: r),
40+
ty: .all,
41+
target: s2
42+
)
43+
44+
register(firstMatch)
45+
register(firstMatchNS)
46+
register(allMatches)
47+
register(allMatchesNS)
2448
}
2549
}

Sources/RegexBenchmark/Suite/ReluctantQuant.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ extension BenchmarkRunner {
3434
target: String(repeating: "a", count: size) + ";"
3535
)
3636

37-
register(new: reluctantQuant)
38-
register(new: reluctantQuantWithTerminal)
39-
register(new: eagarQuantWithTerminal)
37+
register(reluctantQuant)
38+
register(reluctantQuantWithTerminal)
39+
register(eagarQuantWithTerminal)
4040
}
4141
}

0 commit comments

Comments
 (0)