Skip to content
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