Skip to content

DRAFT: add word boundary benchmark and re-use processor persistent state across all-matches #772

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor executor interface
  • Loading branch information
milseman committed Oct 10, 2024
commit f91839561d40cc30aba0edd1ee3b9e3a2b21ecc4
10 changes: 7 additions & 3 deletions Sources/_StringProcessing/Algorithms/Matching/Matches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ extension RegexMatchesSequence: Sequence {
guard let position = currentPosition, position <= base.searchBounds.upperBound else {
return nil
}

// Otherwise, find the next match (if any) and compute `nextStart`
let match = try? base.regex._firstMatch(
let match = try? Executor<Output>.firstMatch(
base.regex.program.loweredProgram,
base.input,
subjectBounds: base.subjectBounds,
subjectBounds: base.subjectBounds,
searchBounds: position..<base.searchBounds.upperBound)
currentPosition = match.flatMap(base.searchIndex(after:))
return match
Expand Down Expand Up @@ -116,6 +117,9 @@ extension BidirectionalCollection where SubSequence == Substring {
// FIXME: Array init calls count, which double-executes the regex :-(
// FIXME: just return some Collection<Regex<Output>.Match>
var result = Array<Regex<Output>.Match>()



for match in _matches(of: r) {
result.append(match)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/_StringProcessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ add_library(_StringProcessing
Algorithms/Searchers/CollectionSearcher.swift
Algorithms/Searchers/ZSearcher.swift
Engine/Backtracking.swift
Engine/Consume.swift
Engine/InstPayload.swift
Engine/Instruction.swift
Engine/MEBuilder.swift
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func _compileRegex(
_ regex: String,
_ syntax: SyntaxOptions = .traditional,
_ semanticLevel: RegexSemanticLevel? = nil
) throws -> Executor {
) throws -> MEProgram {
let ast = try parse(regex, syntax)
let dsl: DSLTree

Expand All @@ -104,7 +104,7 @@ func _compileRegex(
dsl = ast.dslTree
}
let program = try Compiler(tree: dsl).emit()
return Executor(program: program)
return program
}

@_spi(RegexBenchmark)
Expand Down
27 changes: 0 additions & 27 deletions Sources/_StringProcessing/Engine/Consume.swift

This file was deleted.

19 changes: 8 additions & 11 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ extension Processor {
input: Input,
subjectBounds: Range<Position>,
searchBounds: Range<Position>,
matchMode: MatchMode,
isTracingEnabled: Bool,
shouldMeasureMetrics: Bool
matchMode: MatchMode
) {
self.controller = Controller(pc: 0)
self.instructions = program.instructions
Expand All @@ -117,8 +115,8 @@ extension Processor {
self.matchMode = matchMode

self.metrics = ProcessorMetrics(
isTracingEnabled: isTracingEnabled,
shouldMeasureMetrics: shouldMeasureMetrics)
isTracingEnabled: program.enableTracing,
shouldMeasureMetrics: program.enableTracing)

self.currentPosition = searchBounds.lowerBound

Expand Down Expand Up @@ -155,12 +153,11 @@ extension Processor {
_checkInvariants()
}

func isReset(
currentPosition: Position, searchBounds: Range<Position>
) -> Bool {
guard self.currentPosition == currentPosition,
self.searchBounds == searchBounds,
self.controller == Controller(pc: 0),
// Check that resettable state has been reset. Note that `reset()`
// takes a new current position and search bounds.
func isReset() -> Bool {
_checkInvariants()
guard self.controller == Controller(pc: 0),
self.savePoints.isEmpty,
self.callStack.isEmpty,
self.storedCaptures.allSatisfy({ $0.range == nil }),
Expand Down
Loading