Skip to content

Reuse the executor in firstMatch #489

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 4 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
17 changes: 8 additions & 9 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Processor<
typealias Element = Input.Element

let input: Input
var bounds: Range<Position>
let bounds: Range<Position>
let matchMode: MatchMode
var currentPosition: Position

Expand Down Expand Up @@ -95,19 +95,18 @@ extension Processor {
assert(currentPosition <= end)
}

mutating func reset(newBounds: Range<Position>)
mutating func reset(newPosition: Position)
{
self.bounds = newBounds

self.controller = Controller(pc: 0)
self.currentPosition = bounds.lowerBound
self.currentPosition = newPosition

self.registers.reset(bounds.upperBound)
self.storedCaptures = self.storedCaptures.map {_ in .init()}
for idx in storedCaptures.indices {
storedCaptures[idx] = .init()
}

self.cycleCount = 0
self.savePoints = []
self.callStack = []
self.savePoints.removeAll(keepingCapacity: true)
self.callStack.removeAll(keepingCapacity: true)
self.state = .inProgress
self.failureReason = nil
}
Expand Down
24 changes: 14 additions & 10 deletions Sources/_StringProcessing/Engine/Registers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,21 @@ extension Processor.Registers {
mutating func reset(_ sentinel: Input.Index) {
// note: Is there any issue with the program transform functions holding
// state and not getting reset here? Do we care?
self.bools = Array(repeating: false, count: info.bools)
self.ints = Array(repeating: 0, count: info.ints)
self.floats = Array(repeating: 0, count: info.floats)
self.positions = Array(repeating: sentinel, count: info.positions)
self.values = Array(
repeating: SentinelValue(), count: info.values)
func clear<T>(_ xs: inout [T], _ v: T) {
for idx in xs.indices {
xs[idx] = v
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or as an extension on MutableCollection. @natecook1000 does such an algorithm exist?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could try these out here before adding them to swift-algorithms:

extension MutableCollection {
  mutating func setAll(to element: Element) {
    self.withEach { $0 = element }
  }
  
  mutating func withEach(_ body: (inout Element) throws -> Void) rethrows {
    var i = startIndex
    while i < endIndex {
      try body(&self[i])
      formIndex(after: &i)
    }
  }
}

// usage:
self.bools.setAll(to: false)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also considering an underscore them so that no one accidentally ships it 😅


self.instructionAddresses = Array(repeating: 0, count: info.instructionAddresses)
self.classStackAddresses = Array(repeating: 0, count: info.classStackAddresses)
self.positionStackAddresses = Array(repeating: 0, count: info.positionStackAddresses)
self.savePointAddresses = Array(repeating: 0, count: info.savePointAddresses)
clear(&self.bools, false)
clear(&self.ints, 0)
clear(&self.floats, 0)
clear(&self.positions, sentinel)
clear(&self.values, SentinelValue())
clear(&self.instructionAddresses, 0)
clear(&self.classStackAddresses, 0)
clear(&self.positionStackAddresses, 0)
clear(&self.savePointAddresses, 0)
}
}

Expand Down
15 changes: 10 additions & 5 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Executor {
) throws -> Regex<Output>.Match? {
var cpu = engine.makeProcessor(
input: input, bounds: inputRange, matchMode: mode)
return try consume(input, &cpu)
return try consume(input, &cpu, startingFrom: inputRange.lowerBound)
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -43,7 +43,11 @@ struct Executor {
input: input, bounds: inputRange, matchMode: mode)

while true {
if let m: Regex<Output>.Match = try consume(input, &cpu) {
if let m: Regex<Output>.Match = try consume(
input,
&cpu,
startingFrom: low
Comment on lines +47 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
input,
&cpu,
startingFrom: low
input, &cpu, startingFrom: low

) {
return m
}

Expand All @@ -54,14 +58,15 @@ struct Executor {
input.unicodeScalars.formIndex(after: &low)
}

cpu.reset(newBounds: low..<high)
cpu.reset(newPosition: low)
}
}

@available(SwiftStdlib 5.7, *)
func consume<Output>(
_ input: String,
_ cpu: inout Processor<String>
_ cpu: inout Processor<String>,
startingFrom startIdx: String.Index
) throws -> Regex<Output>.Match? {
guard let endIdx = cpu.consume() else {
if let e = cpu.failureReason {
Expand All @@ -75,7 +80,7 @@ struct Executor {
referencedCaptureOffsets: engine.program.referencedCaptureOffsets,
namedCaptureOffsets: engine.program.namedCaptureOffsets)

let range = cpu.bounds.lowerBound..<endIdx
let range = startIdx..<endIdx
let caps = engine.program.captureList.createElements(capList, input)

// FIXME: This is a workaround for not tracking (or
Expand Down