Skip to content

Commit 3d0789f

Browse files
committed
Cleanup
- make charts prettier - underscore compileroptions - cleanup debug
1 parent 82f71dd commit 3d0789f

File tree

6 files changed

+45
-21
lines changed

6 files changed

+45
-21
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
@_implementationOnly import _RegexParser
33
import Foundation
44

5-
protocol RegexBenchmark {
5+
protocol RegexBenchmark: Debug {
66
var name: String { get }
77
func run()
8-
func debug()
98
}
109

1110
protocol SwiftRegexBenchmark: RegexBenchmark {

Sources/RegexBenchmark/BenchmarkChart.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,23 @@ struct BenchmarkChart: View {
4040
RuleMark(y: .value("Time", 1.0))
4141
.foregroundStyle(.red)
4242
.lineStyle(.init(lineWidth: 1, dash: [2]))
43+
.annotation(position: .top, alignment: .leading) {
44+
Text("Baseline").foregroundStyle(.red)
45+
}
4346

44-
}.frame(idealHeight: 400)
47+
}
48+
.frame(idealWidth: 800, idealHeight: 800)
49+
.chartYScale(domain: 0...2.0)
50+
.chartYAxis {
51+
AxisMarks(values: .stride(by: 0.1))
52+
}
53+
.chartXAxis {
54+
AxisMarks { value in
55+
AxisGridLine()
56+
AxisTick()
57+
AxisValueLabel(value.as(String.self)!, orientation: .vertical)
58+
}
59+
}
4560
}
4661
}
4762
}

Sources/RegexBenchmark/Debug.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import Foundation
22

3+
protocol Debug {
4+
func debug()
5+
}
6+
7+
extension Debug {
8+
var maxStringLengthForPrint: Int { 1000 }
9+
var maxMatchCountForPrint: Int { 100 }
10+
}
11+
312
extension Benchmark {
413
func debug() {
514
switch type {
615
case .whole:
716
let result = target.wholeMatch(of: regex)
817
if let match = result {
9-
if match.0.count > 1000 {
18+
if match.0.count > maxStringLengthForPrint {
1019
print("- Match: len = \(match.0.count)")
1120
} else {
1221
print("- Match: \(match.0)")
@@ -22,7 +31,7 @@ extension Benchmark {
2231
}
2332

2433
print("- Total matches: \(results.count)")
25-
if results.count > 100 {
34+
if results.count > maxMatchCountForPrint {
2635
print("# Too many matches, not printing")
2736
let avgLen = results.map({result in String(target[result.range]).count})
2837
.reduce(0.0, {$0 + Double($1)}) / Double(results.count)
@@ -32,7 +41,7 @@ extension Benchmark {
3241
}
3342

3443
for match in results {
35-
if match.0.count > 1000 {
44+
if match.0.count > maxStringLengthForPrint {
3645
print("- Match: len = \(match.0.count)")
3746
} else {
3847
print("- Match: \(match.0)")
@@ -42,7 +51,7 @@ extension Benchmark {
4251
case .first:
4352
let result = target.firstMatch(of: regex)
4453
if let match = result {
45-
if match.0.count > 1000 {
54+
if match.0.count > maxStringLengthForPrint {
4655
print("- Match: len = \(match.0.count)")
4756
} else {
4857
print("- Match: \(match.0)")
@@ -56,6 +65,7 @@ extension Benchmark {
5665
}
5766

5867
extension NSBenchmark {
68+
5969
func debug() {
6070
switch type {
6171
case .allMatches:
@@ -66,13 +76,13 @@ extension NSBenchmark {
6676
}
6777

6878
print("- Total matches: \(results.count)")
69-
if results.count > 100 {
79+
if results.count > maxMatchCountForPrint {
7080
print("# Too many matches, not printing")
7181
return
7282
}
7383

7484
for m in results {
75-
if m.range.length > 1000 {
85+
if m.range.length > maxStringLengthForPrint {
7686
print("- Match: len = \(m.range.length)")
7787
} else {
7888
print("- Match: \(target[Range(m.range, in: target)!])")
@@ -81,7 +91,7 @@ extension NSBenchmark {
8191
case .first:
8292
let result = regex.firstMatch(in: target, range: range)
8393
if let match = result {
84-
if match.range.length > 1000 {
94+
if match.range.length > maxStringLengthForPrint {
8595
print("- Match: len = \(match.range.length)")
8696
} else {
8797
print("- Match: \(target[Range(match.range, in: target)!])")

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ extension Compiler {
2222
/// This is used to determine whether to apply initial options.
2323
var hasEmittedFirstMatchableAtom = false
2424

25-
private let compileOptions: CompileOptions
25+
private let compileOptions: _CompileOptions
2626
fileprivate var optimizationsEnabled: Bool { !compileOptions.contains(.disableOptimizations) }
2727

2828
init(
2929
options: MatchingOptions,
30-
compileOptions: CompileOptions,
30+
compileOptions: _CompileOptions,
3131
captureList: CaptureList
3232
) {
3333
self.options = options

Sources/_StringProcessing/Compiler.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Compiler {
1616

1717
// TODO: Or are these stored on the tree?
1818
var options = MatchingOptions()
19-
private var compileOptions: CompileOptions = .default
19+
private var compileOptions: _CompileOptions = .default
2020

2121
init(ast: AST) {
2222
self.tree = ast.dslTree
@@ -26,7 +26,7 @@ class Compiler {
2626
self.tree = tree
2727
}
2828

29-
init(tree: DSLTree, compileOptions: CompileOptions) {
29+
init(tree: DSLTree, compileOptions: _CompileOptions) {
3030
self.tree = tree
3131
self.compileOptions = compileOptions
3232
}
@@ -108,14 +108,14 @@ func _compileRegex(
108108
}
109109

110110
@_spi(RegexBenchmark)
111-
public struct CompileOptions: OptionSet {
111+
public struct _CompileOptions: OptionSet {
112112
public let rawValue: Int
113113
public init(rawValue: Int) {
114114
self.rawValue = rawValue
115115
}
116116

117-
public static let disableOptimizations = CompileOptions(rawValue: 1 << 0)
118-
public static let enableTracing = CompileOptions(rawValue: 1 << 1)
119-
public static let enableMetrics = CompileOptions(rawValue: 1 << 2)
120-
public static let `default`: CompileOptions = []
117+
public static let disableOptimizations = _CompileOptions(rawValue: 1 << 0)
118+
public static let enableTracing = _CompileOptions(rawValue: 1 << 1)
119+
public static let enableMetrics = _CompileOptions(rawValue: 1 << 2)
120+
public static let `default`: _CompileOptions = []
121121
}

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension Regex {
8282
let tree: DSLTree
8383

8484
/// OptionSet of compiler options for testing purposes
85-
fileprivate var compileOptions: CompileOptions = .default
85+
fileprivate var compileOptions: _CompileOptions = .default
8686

8787
private final class ProgramBox {
8888
let value: MEProgram
@@ -140,7 +140,7 @@ extension Regex {
140140
extension Regex {
141141
public enum _RegexInternalAction {
142142
case recompile
143-
case addOptions(CompileOptions)
143+
case addOptions(_CompileOptions)
144144
}
145145

146146
/// Internal API for RegexBenchmark

0 commit comments

Comments
 (0)