Skip to content

Commit 4b107a8

Browse files
committed
Fix tracing
1 parent b9f68c8 commit 4b107a8

File tree

9 files changed

+29
-46
lines changed

9 files changed

+29
-46
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ struct Benchmark: RegexBenchmark {
2525
}
2626

2727
mutating func enableTracing() {
28-
let _ = regex._forceAction(.setOptions(.enableTracing))
28+
let _ = regex._forceAction(.addOptions(.enableTracing))
2929
}
3030
mutating func enableMetrics() {
31-
let _ = regex._forceAction(.setOptions(.enableMetrics))
31+
let _ = regex._forceAction(.addOptions([.enableMetrics, .disableOptimizations]))
3232
}
3333

3434
func run() {
@@ -76,10 +76,10 @@ struct InputListBenchmark: RegexBenchmark {
7676
blackHole(regex._forceAction(.recompile))
7777
}
7878
mutating func enableTracing() {
79-
let _ = regex._forceAction(.setOptions(.enableTracing))
79+
let _ = regex._forceAction(.addOptions(.enableTracing))
8080
}
8181
mutating func enableMetrics() {
82-
let _ = regex._forceAction(.setOptions(.enableMetrics))
82+
let _ = regex._forceAction(.addOptions(.enableMetrics))
8383
}
8484

8585
func run() {

Sources/_StringProcessing/Engine/Consume.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension Engine {
3030
subjectBounds: Range<String.Index>,
3131
searchBounds: Range<String.Index>
3232
) -> Processor {
33-
Processor(
33+
return Processor(
3434
program: program,
3535
input: input,
3636
subjectBounds: subjectBounds,

Sources/_StringProcessing/Engine/Engine.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,16 @@
1313
// But, we can play around with this.
1414
struct Engine {
1515

16-
var program: MEProgram
16+
let program: MEProgram
1717

1818
// TODO: Pre-allocated register banks
1919

2020
var instructions: InstructionList<Instruction> { program.instructions }
2121

22-
var enableTracing: Bool {
23-
get { program.enableTracing }
24-
set { program.enableTracing = newValue }
25-
}
26-
var enableMetrics: Bool {
27-
get { program.enableMetrics }
28-
set { program.enableMetrics = newValue }
29-
}
22+
var enableTracing: Bool { program.enableTracing }
23+
var enableMetrics: Bool { program.enableMetrics }
3024

31-
init(
32-
_ program: MEProgram,
33-
enableTracing: Bool? = nil
34-
) {
35-
var program = program
36-
if let t = enableTracing {
37-
program.enableTracing = t
38-
}
25+
init(_ program: MEProgram) {
3926
self.program = program
4027
}
4128
}

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ extension Processor {
110110
isTracingEnabled: Bool,
111111
shouldMeasureMetrics: Bool
112112
) {
113-
// print("metrics? \(shouldMeasureMetrics) tracing? \(isTracingEnabled)")
114113
self.controller = Controller(pc: 0)
115114
self.instructions = program.instructions
116115
self.input = input

Sources/_StringProcessing/Engine/Tracing.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ extension Instruction: CustomStringConvertible {
7676
case .matchScalar:
7777
let (scalar, caseInsensitive, boundaryCheck) = payload.scalarPayload
7878
if caseInsensitive {
79-
return "matchScalarCaseInsensitive \(scalar) boundaryCheck: \(boundaryCheck)"
79+
return "matchScalarCaseInsensitive '\(scalar)' boundaryCheck: \(boundaryCheck)"
8080
} else {
81-
return "matchScalar \(scalar) boundaryCheck: \(boundaryCheck)"
81+
return "matchScalar '\(scalar)' boundaryCheck: \(boundaryCheck)"
8282
}
8383
case .moveCurrentPosition:
8484
let reg = payload.position

Sources/_StringProcessing/Executor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ struct Executor {
1515
// TODO: consider let, for now lets us toggle tracing
1616
var engine: Engine
1717

18-
init(program: MEProgram, enablesTracing: Bool = false) {
19-
self.engine = Engine(program, enableTracing: enablesTracing)
18+
init(program: MEProgram) {
19+
self.engine = Engine(program)
2020
}
2121

2222
@available(SwiftStdlib 5.7, *)

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,17 @@ extension Regex {
149149
public enum _RegexInternalAction {
150150
case parse(String)
151151
case recompile
152-
case setOptions(CompileOptions)
152+
case addOptions(CompileOptions)
153153
}
154154

155155
/// Internal API for RegexBenchmark
156156
/// Forces the regex to perform the given action, returning if it was successful
157157
public mutating func _forceAction(_ action: _RegexInternalAction) -> Bool {
158158
do {
159159
switch action {
160-
case .setOptions(let opts):
161-
_setCompilerOptionsForTesting(opts)
160+
case .addOptions(let opts):
161+
program.compileOptions.insert(opts)
162+
program._loweredProgramStorage = nil
162163
return true
163164
case .parse(let pattern):
164165
let _ = try parse(pattern, .traditional)
@@ -174,13 +175,3 @@ extension Regex {
174175
}
175176
}
176177
}
177-
178-
@available(SwiftStdlib 5.7, *)
179-
extension Regex {
180-
internal mutating func _setCompilerOptionsForTesting(
181-
_ opts: CompileOptions
182-
) {
183-
program.compileOptions = opts
184-
program._loweredProgramStorage = nil
185-
}
186-
}

Sources/_StringProcessing/Utility/Traced.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,21 @@ extension TracedProcessor {
8080
}
8181

8282
func formatInput() -> String {
83+
let distanceFromStart = input.distance(
84+
from: input.startIndex,
85+
to: currentPosition)
86+
8387
// Cut a reasonably sized substring from the input to print
8488
let start = input.index(
8589
currentPosition,
86-
offsetBy: -10,
90+
offsetBy: -30,
8791
limitedBy: input.startIndex) ?? input.startIndex
8892
let end = input.index(
8993
currentPosition,
90-
offsetBy: 10,
94+
offsetBy: 30,
9195
limitedBy: input.endIndex) ?? input.endIndex
92-
let input = input[start...end]
93-
96+
let input = input[start..<end]
97+
9498
// String override for printing sub-character information.
9599
if !input.indices.contains(currentPosition) {
96100
// Format unicode scalars as:
@@ -111,6 +115,7 @@ extension TracedProcessor {
111115
.joined())
112116
\(String(repeating: ".", count: matchedHighlightWidth))\
113117
^\(String(repeating: "~", count: nextHighlightWidth - 1))
118+
position: \(distanceFromStart)
114119
"""
115120
}
116121
if let string = input as? String {
@@ -119,10 +124,11 @@ extension TracedProcessor {
119124
return _format(substring)
120125
}
121126
}
122-
let dist = input.distance(from: input.startIndex, to: currentPosition)
127+
let dist = input.distance(from: start, to: currentPosition)
123128
return """
124129
input: \(input)
125130
\(String(repeating: "~", count: dist))^
131+
position: \(distanceFromStart)
126132
"""
127133
}
128134

Tests/RegexTests/MatchTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func _firstMatch(
3232
let result = try regex.firstMatch(in: input)
3333

3434
if validateOptimizations {
35-
regex._setCompilerOptionsForTesting(.disableOptimizations)
35+
assert(regex._forceAction(.addOptions(.disableOptimizations)))
3636
let unoptResult = try regex.firstMatch(in: input)
3737
if result != nil && unoptResult == nil {
3838
throw MatchError("match not found for unoptimized \(regexStr) in \(input)")

0 commit comments

Comments
 (0)