Skip to content

Bug fix and hot path for quantified . #658

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 10 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
20 changes: 13 additions & 7 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 All @@ -59,10 +63,10 @@ extension Processor {
switch payload.semanticLevel {
case .graphemeCluster:
return input.index(after: currentPosition) == subjectBounds.upperBound
&& input[currentPosition].isNewline
&& input[currentPosition].isNewline
case .unicodeScalar:
return input.unicodeScalars.index(after: currentPosition) == subjectBounds.upperBound
&& input.unicodeScalars[currentPosition].isNewline
&& input.unicodeScalars[currentPosition].isNewline
}

case .endOfSubject: return currentPosition == subjectBounds.upperBound
Expand Down Expand Up @@ -117,6 +121,7 @@ extension Processor {

// MARK: Matching `.`
extension String {
// TODO: Should the below have a `limitedBy` parameter?

func _matchAnyNonNewline(
at currentPosition: String.Index,
Expand Down Expand Up @@ -151,11 +156,11 @@ extension String {
return .unknown
}
switch asciiValue {
case ._lineFeed, ._carriageReturn:
return .definite(nil)
default:
assert(!isCRLF)
return .definite(next)
case (._lineFeed)...(._carriageReturn):
return .definite(nil)
default:
assert(!isCRLF)
return .definite(next)
}
}

Expand All @@ -179,6 +184,7 @@ extension String {

// MARK: - Built-in character class matching
extension String {
// TODO: Should the below have a `limitedBy` parameter?

// Mentioned in ProgrammersManual.md, update docs if redesigned
func _matchBuiltinCC(
Expand Down
29 changes: 20 additions & 9 deletions Sources/_StringProcessing/Engine/MEQuantify.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
extension Processor {
func _doQuantifyMatch(_ payload: QuantifyPayload) -> Input.Index? {
var next: Input.Index?
// FIXME: is the below updated for scalar semantics?
switch payload.type {
case .bitset:
next = _doMatchBitset(registers[payload.bitset])
return input.matchBitset(
registers[payload.bitset], at: currentPosition, limitedBy: end)
case .asciiChar:
next = _doMatchScalar(
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
return input.matchScalar(
UnicodeScalar.init(_value: UInt32(payload.asciiChar)),
at: currentPosition,
limitedBy: end,
boundaryCheck: true)
case .builtin:
// FIXME: bounds check? endIndex or end?

// We only emit .quantify if it consumes a single character
next = input._matchBuiltinCC(
return input._matchBuiltinCC(
payload.builtin,
at: currentPosition,
isInverted: payload.builtinIsInverted,
isStrictASCII: payload.builtinIsStrict,
isScalarSemantics: false)
case .any:
let matched = currentPosition != input.endIndex
&& (!input[currentPosition].isNewline || payload.anyMatchesNewline)
next = matched ? input.index(after: currentPosition) : nil
// FIXME: endIndex or end?
guard currentPosition < input.endIndex else { return nil }

if payload.anyMatchesNewline {
return input.index(after: currentPosition)
}

return input._matchAnyNonNewline(
at: currentPosition, isScalarSemantics: false)
}
return next
}

/// Generic quantify instruction interpreter
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