Skip to content

Commit 8cdc76e

Browse files
committed
Add compiler options for tracing and metrics
1 parent aa81037 commit 8cdc76e

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extension Compiler {
3333
self.options = options
3434
self.compileOptions = compileOptions
3535
self.builder.captureList = captureList
36+
self.builder.enableTracing = compileOptions.contains(.enableTracing)
37+
self.builder.enableMetrics = compileOptions.contains(.enableMetrics)
3638
}
3739
}
3840
}

Sources/_StringProcessing/Compiler.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,15 @@ func _compileRegex(
107107
return Executor(program: program)
108108
}
109109

110-
extension Compiler {
111-
struct CompileOptions: OptionSet {
112-
let rawValue: Int
113-
static let disableOptimizations = CompileOptions(rawValue: 1)
114-
static let `default`: CompileOptions = []
110+
@_spi(RegexBenchmark)
111+
public struct CompileOptions: OptionSet {
112+
public let rawValue: Int
113+
public init(rawValue: Int) {
114+
self.rawValue = rawValue
115115
}
116+
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 = []
116121
}

Sources/_StringProcessing/Engine/Engine.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ struct Engine {
2323
get { program.enableTracing }
2424
set { program.enableTracing = newValue }
2525
}
26+
var enableMetrics: Bool {
27+
get { program.enableTracing }
28+
set { program.enableMetrics = newValue }
29+
}
2630

2731
init(
2832
_ program: MEProgram,

Sources/_StringProcessing/Engine/MEBuilder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
extension MEProgram {
1515
struct Builder {
1616
var instructions: [Instruction] = []
17+
18+
// Tracing
19+
var enableTracing = false
20+
var enableMetrics = false
1721

1822
var elements = TypedSetVector<Input.Element, _ElementRegister>()
1923
var sequences = TypedSetVector<[Input.Element], _SequenceRegister>()
@@ -378,6 +382,8 @@ extension MEProgram.Builder {
378382
staticTransformFunctions: transformFunctions,
379383
staticMatcherFunctions: matcherFunctions,
380384
registerInfo: regInfo,
385+
enableTracing: enableTracing,
386+
enableMetrics: enableMetrics,
381387
captureList: captureList,
382388
referencedCaptureOffsets: referencedCaptureOffsets,
383389
initialOptions: initialOptions)

Sources/_StringProcessing/Engine/MEProgram.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ struct MEProgram {
3131

3232
var registerInfo: RegisterInfo
3333

34-
var enableTracing: Bool = false
35-
34+
var enableTracing: Bool
35+
var enableMetrics: Bool
36+
3637
let captureList: CaptureList
3738
let referencedCaptureOffsets: [ReferenceID: Int]
3839

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 34 additions & 11 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: Compiler.CompileOptions = .default
85+
fileprivate var compileOptions: CompileOptions = .default
8686

8787
private final class ProgramBox {
8888
let value: MEProgram
@@ -138,16 +138,37 @@ extension Regex {
138138
@available(SwiftStdlib 5.7, *)
139139
@_spi(RegexBenchmark)
140140
extension Regex {
141-
/// Compiles the stored DSLTree into bytecode and return if it was successful
142-
/// For measuring compilation times
143-
///
144-
/// Note: This bypasses the cached program that is normally used
145-
public func _compileRegex() -> Bool {
141+
public struct QueryResult {
142+
143+
}
144+
145+
public func _queryRegex() -> QueryResult {
146+
QueryResult()
147+
}
148+
149+
public enum _RegexInternalAction {
150+
case parse(String)
151+
case recompile
152+
case setOptions(CompileOptions)
153+
}
154+
155+
/// Internal API for RegexBenchmark
156+
/// Forces the regex to perform the given action, returning if it was successful
157+
public mutating func _forceAction(_ action: _RegexInternalAction) -> Bool {
146158
do {
147-
let _ = try Compiler(
148-
tree: program.tree,
149-
compileOptions: program.compileOptions).emit()
150-
return true
159+
switch action {
160+
case .setOptions(let opts):
161+
_setCompilerOptionsForTesting(opts)
162+
return true
163+
case .parse(let pattern):
164+
let _ = try parse(pattern, .traditional)
165+
return true
166+
case .recompile:
167+
let _ = try Compiler(
168+
tree: program.tree,
169+
compileOptions: program.compileOptions).emit()
170+
return true
171+
}
151172
} catch {
152173
return false
153174
}
@@ -156,7 +177,9 @@ extension Regex {
156177

157178
@available(SwiftStdlib 5.7, *)
158179
extension Regex {
159-
internal mutating func _setCompilerOptionsForTesting(_ opts: Compiler.CompileOptions) {
180+
internal mutating func _setCompilerOptionsForTesting(
181+
_ opts: CompileOptions
182+
) {
160183
program.compileOptions = opts
161184
program._loweredProgramStorage = nil
162185
}

0 commit comments

Comments
 (0)