Skip to content

Processor cleanup #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/_StringProcessing/Engine/MEBuiltins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extension Processor {
}

func isAtStartOfLine(_ payload: AssertionPayload) -> Bool {
// TODO: needs benchmark coverage
if currentPosition == subjectBounds.lowerBound { return true }
switch payload.semanticLevel {
case .graphemeCluster:
Expand All @@ -40,6 +41,7 @@ extension Processor {
}

func isAtEndOfLine(_ payload: AssertionPayload) -> Bool {
// TODO: needs benchmark coverage
if currentPosition == subjectBounds.upperBound { return true }
switch payload.semanticLevel {
case .graphemeCluster:
Expand All @@ -50,6 +52,8 @@ extension Processor {
}

mutating func builtinAssert(by payload: AssertionPayload) throws -> Bool {
// TODO: needs benchmark coverage

// Future work: Optimize layout and dispatch
switch payload.kind {
case .startOfSubject: return currentPosition == subjectBounds.lowerBound
Expand Down
11 changes: 8 additions & 3 deletions Sources/_StringProcessing/Engine/MEQuantify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ extension Processor {
var next: Input.Index?
switch payload.type {
case .bitset:
next = _doMatchBitset(registers[payload.bitset])
next = input.matchBitset(
registers[payload.bitset], at: currentPosition, limitedBy: end)
case .asciiChar:
next = _doMatchScalar(
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
next = input.matchScalar(
UnicodeScalar.init(_value: UInt32(payload.asciiChar)),
at: currentPosition,
limitedBy: end,
boundaryCheck: true)
case .builtin:
// We only emit .quantify if it consumes a single character
next = input._matchBuiltinCC(
Expand All @@ -16,6 +20,7 @@ extension Processor {
isStrictASCII: payload.builtinIsStrict,
isScalarSemantics: false)
case .any:
// TODO: call out to existing code with quick check
let matched = currentPosition != input.endIndex
&& (!input[currentPosition].isNewline || payload.anyMatchesNewline)
next = matched ? input.index(after: currentPosition) : nil
Expand Down
67 changes: 63 additions & 4 deletions Sources/_StringProcessing/Engine/Metrics.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
extension Processor {
#if PROCESSOR_MEASUREMENTS_ENABLED
struct ProcessorMetrics {
var instructionCounts: [Instruction.OpCode: Int] = [:]
var backtracks: Int = 0
var resets: Int = 0
var cycleCount: Int = 0

var isTracingEnabled: Bool = false
var shouldMeasureMetrics: Bool = false

init(isTracingEnabled: Bool, shouldMeasureMetrics: Bool) {
self.isTracingEnabled = isTracingEnabled
self.shouldMeasureMetrics = shouldMeasureMetrics
}
}

#else
struct ProcessorMetrics {
var isTracingEnabled: Bool { false }
var shouldMeasureMetrics: Bool { false }
var cycleCount: Int { 0 }

init(isTracingEnabled: Bool, shouldMeasureMetrics: Bool) { }
}
#endif
}

extension Processor {

mutating func startCycleMetrics() {
#if PROCESSOR_MEASUREMENTS_ENABLED
if metrics.cycleCount == 0 {
trace()
measureMetrics()
}
#endif
}

mutating func endCycleMetrics() {
#if PROCESSOR_MEASUREMENTS_ENABLED
metrics.cycleCount += 1
trace()
measureMetrics()
_checkInvariants()
#endif
}
}

extension Processor.ProcessorMetrics {

mutating func addReset() {
#if PROCESSOR_MEASUREMENTS_ENABLED
self.resets += 1
#endif
}

mutating func addBacktrack() {
#if PROCESSOR_MEASUREMENTS_ENABLED
self.backtracks += 1
#endif
}
}

extension Processor {
#if PROCESSOR_MEASUREMENTS_ENABLED
func printMetrics() {
print("===")
print("Total cycle count: \(cycleCount)")
print("Total cycle count: \(metrics.cycleCount)")
print("Backtracks: \(metrics.backtracks)")
print("Resets: \(metrics.resets)")
print("Instructions:")
Expand All @@ -21,7 +79,7 @@ extension Processor {
}

mutating func measure() {
let (opcode, _) = fetch().destructure
let (opcode, _) = fetch()
if metrics.instructionCounts.keys.contains(opcode) {
metrics.instructionCounts[opcode]! += 1
} else {
Expand All @@ -30,8 +88,9 @@ extension Processor {
}

mutating func measureMetrics() {
if shouldMeasureMetrics {
if metrics.shouldMeasureMetrics {
measure()
}
}
#endif
}
Loading