Skip to content

General ascii fast paths for character classes #644

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 7 commits into from
Apr 4, 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
30 changes: 30 additions & 0 deletions Documentation/ProgrammersManual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Programmer's Manual

## Programming patterns

### Engine quick checks and fast paths

In the engine nomenclature, a quick-check results in a yes/no/maybe while a thorough check always results in a definite answer.

The nature of quick checks and fast paths is that they bifurcate testing coverage. One easy way to prevent this in simple cases is to assert that a definite quick result matches the thorough result.

One example of this pattern is matching against a builtin character class. The engine has a `_matchBuiltinCC`

```swift
func _matchBuiltinCC(...) -> Input.Index? {
// Calls _quickMatchBuiltinCC, if that gives a definite result
// asserts that it is the same as the result of
// _thoroughMatchBuiltinCC and returns it. Otherwise returns the
// result of _thoroughMatchBuiltinCC
}

@inline(__always)
func _quickMatchBuiltinCC(...) -> QuickResult<Input.Index?>

@inline(never)
func _thoroughMatchBuiltinCC(...) -> Input.Index?
```

The thorough check is never inlined, as it is a lot of cold code. Note that quick and thorough functions should be pure, that is they shouldn't update processor state.


1 change: 1 addition & 0 deletions Sources/_StringProcessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_library(_StringProcessing
Regex/DSLTree.swift
Regex/Match.swift
Regex/Options.swift
Unicode/ASCII.swift
Unicode/CaseConversion.swift
Unicode/CharacterProps.swift
Unicode/Comparison.swift
Expand Down
259 changes: 160 additions & 99 deletions Sources/_StringProcessing/Engine/MEBuiltins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,114 +9,26 @@ extension Character {
}

extension Processor {
mutating func matchBuiltin(
mutating func matchBuiltinCC(
_ cc: _CharacterClassModel.Representation,
_ isInverted: Bool,
_ isStrictASCII: Bool,
_ isScalarSemantics: Bool
isInverted: Bool,
isStrictASCII: Bool,
isScalarSemantics: Bool
) -> Bool {
guard let next = _doMatchBuiltin(
guard let next = input._matchBuiltinCC(
cc,
isInverted,
isStrictASCII,
isScalarSemantics
at: currentPosition,
isInverted: isInverted,
isStrictASCII: isStrictASCII,
isScalarSemantics: isScalarSemantics
) else {
signalFailure()
return false
}
currentPosition = next
return true
}

func _doMatchBuiltin(
_ cc: _CharacterClassModel.Representation,
_ isInverted: Bool,
_ isStrictASCII: Bool,
_ isScalarSemantics: Bool
) -> Input.Index? {
guard let char = load(), let scalar = loadScalar() else {
return nil
}

let asciiCheck = !isStrictASCII
|| (scalar.isASCII && isScalarSemantics)
|| char.isASCII

var matched: Bool
var next: Input.Index
switch (isScalarSemantics, cc) {
case (_, .anyGrapheme):
next = input.index(after: currentPosition)
case (_, .anyScalar):
next = input.unicodeScalars.index(after: currentPosition)
case (true, _):
next = input.unicodeScalars.index(after: currentPosition)
case (false, _):
next = input.index(after: currentPosition)
}

switch cc {
case .any, .anyGrapheme:
matched = true
case .anyScalar:
if isScalarSemantics {
matched = true
} else {
matched = input.isOnGraphemeClusterBoundary(next)
}
case .digit:
if isScalarSemantics {
matched = scalar.properties.numericType != nil && asciiCheck
} else {
matched = char.isNumber && asciiCheck
}
case .horizontalWhitespace:
if isScalarSemantics {
matched = scalar.isHorizontalWhitespace && asciiCheck
} else {
matched = char._isHorizontalWhitespace && asciiCheck
}
case .verticalWhitespace:
if isScalarSemantics {
matched = scalar.isNewline && asciiCheck
} else {
matched = char._isNewline && asciiCheck
}
case .newlineSequence:
if isScalarSemantics {
matched = scalar.isNewline && asciiCheck
if matched && scalar == "\r"
&& next != input.endIndex && input.unicodeScalars[next] == "\n" {
// Match a full CR-LF sequence even in scalar semantics
input.unicodeScalars.formIndex(after: &next)
}
} else {
matched = char._isNewline && asciiCheck
}
case .whitespace:
if isScalarSemantics {
matched = scalar.properties.isWhitespace && asciiCheck
} else {
matched = char.isWhitespace && asciiCheck
}
case .word:
if isScalarSemantics {
matched = scalar.properties.isAlphabetic && asciiCheck
} else {
matched = char.isWordCharacter && asciiCheck
}
}

if isInverted {
matched.toggle()
}

guard matched else {
return nil
}
return next
}

func isAtStartOfLine(_ payload: AssertionPayload) -> Bool {
if currentPosition == subjectBounds.lowerBound { return true }
switch payload.semanticLevel {
Expand All @@ -126,7 +38,7 @@ extension Processor {
return input.unicodeScalars[input.unicodeScalars.index(before: currentPosition)].isNewline
}
}

func isAtEndOfLine(_ payload: AssertionPayload) -> Bool {
if currentPosition == subjectBounds.upperBound { return true }
switch payload.semanticLevel {
Expand Down Expand Up @@ -169,7 +81,7 @@ extension Processor {
return isAtStartOfLine(payload)
case .endOfLine:
return isAtEndOfLine(payload)

case .caretAnchor:
if payload.anchorsMatchNewlines {
return isAtStartOfLine(payload)
Expand Down Expand Up @@ -202,3 +114,152 @@ extension Processor {
}
}
}

// MARK: Built-in character class matching

extension String {

// Mentioned in ProgrammersManual.md, update docs if redesigned
func _matchBuiltinCC(
_ cc: _CharacterClassModel.Representation,
at currentPosition: String.Index,
isInverted: Bool,
isStrictASCII: Bool,
isScalarSemantics: Bool
) -> String.Index? {
guard currentPosition < endIndex else {
return nil
}
if case .definite(let result) = _quickMatchBuiltinCC(
cc,
at: currentPosition,
isInverted: isInverted,
isStrictASCII: isStrictASCII,
isScalarSemantics: isScalarSemantics
) {
assert(result == _thoroughMatchBuiltinCC(
cc,
at: currentPosition,
isInverted: isInverted,
isStrictASCII: isStrictASCII,
isScalarSemantics: isScalarSemantics))
return result
}
return _thoroughMatchBuiltinCC(
cc,
at: currentPosition,
isInverted: isInverted,
isStrictASCII: isStrictASCII,
isScalarSemantics: isScalarSemantics)
}

// Mentioned in ProgrammersManual.md, update docs if redesigned
@inline(__always)
func _quickMatchBuiltinCC(
_ cc: _CharacterClassModel.Representation,
at currentPosition: String.Index,
isInverted: Bool,
isStrictASCII: Bool,
isScalarSemantics: Bool
) -> QuickResult<String.Index?> {
assert(currentPosition < endIndex)
guard let (next, result) = _quickMatch(
cc, at: currentPosition, isScalarSemantics: isScalarSemantics
) else {
return .unknown
}
return .definite(result == isInverted ? nil : next)
}

// Mentioned in ProgrammersManual.md, update docs if redesigned
@inline(never)
func _thoroughMatchBuiltinCC(
_ cc: _CharacterClassModel.Representation,
at currentPosition: String.Index,
isInverted: Bool,
isStrictASCII: Bool,
isScalarSemantics: Bool
) -> String.Index? {
assert(currentPosition < endIndex)
let char = self[currentPosition]
let scalar = unicodeScalars[currentPosition]

let asciiCheck = !isStrictASCII
|| (scalar.isASCII && isScalarSemantics)
|| char.isASCII

var matched: Bool
var next: String.Index
switch (isScalarSemantics, cc) {
case (_, .anyGrapheme):
next = index(after: currentPosition)
case (_, .anyScalar):
next = unicodeScalars.index(after: currentPosition)
case (true, _):
next = unicodeScalars.index(after: currentPosition)
case (false, _):
next = index(after: currentPosition)
}

switch cc {
case .any, .anyGrapheme:
matched = true
case .anyScalar:
if isScalarSemantics {
matched = true
} else {
matched = isOnGraphemeClusterBoundary(next)
}
case .digit:
if isScalarSemantics {
matched = scalar.properties.numericType != nil && asciiCheck
} else {
matched = char.isNumber && asciiCheck
}
case .horizontalWhitespace:
if isScalarSemantics {
matched = scalar.isHorizontalWhitespace && asciiCheck
} else {
matched = char._isHorizontalWhitespace && asciiCheck
}
case .verticalWhitespace:
if isScalarSemantics {
matched = scalar.isNewline && asciiCheck
} else {
matched = char._isNewline && asciiCheck
}
case .newlineSequence:
if isScalarSemantics {
matched = scalar.isNewline && asciiCheck
if matched && scalar == "\r"
&& next != endIndex && unicodeScalars[next] == "\n" {
// Match a full CR-LF sequence even in scalar semantics
unicodeScalars.formIndex(after: &next)
}
} else {
matched = char._isNewline && asciiCheck
}
case .whitespace:
if isScalarSemantics {
matched = scalar.properties.isWhitespace && asciiCheck
} else {
matched = char.isWhitespace && asciiCheck
}
case .word:
if isScalarSemantics {
matched = scalar.properties.isAlphabetic && asciiCheck
} else {
matched = char.isWordCharacter && asciiCheck
}
}

if isInverted {
matched.toggle()
}

guard matched else {
return nil
}
return next
}
}
9 changes: 5 additions & 4 deletions Sources/_StringProcessing/Engine/MEQuantify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ extension Processor {
UnicodeScalar.init(_value: UInt32(payload.asciiChar)), true)
case .builtin:
// We only emit .quantify if it consumes a single character
next = _doMatchBuiltin(
next = input._matchBuiltinCC(
payload.builtin,
payload.builtinIsInverted,
payload.builtinIsStrict,
false)
at: currentPosition,
isInverted: payload.builtinIsInverted,
isStrictASCII: payload.builtinIsStrict,
isScalarSemantics: false)
case .any:
let matched = currentPosition != input.endIndex
&& (!input[currentPosition].isNewline || payload.anyMatchesNewline)
Expand Down
8 changes: 4 additions & 4 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ extension Processor {

case .matchBuiltin:
let payload = payload.characterClassPayload
if matchBuiltin(
if matchBuiltinCC(
payload.cc,
payload.isInverted,
payload.isStrictASCII,
payload.isScalarSemantics
isInverted: payload.isInverted,
isStrictASCII: payload.isStrictASCII,
isScalarSemantics: payload.isScalarSemantics
) {
controller.step()
}
Expand Down
Loading