Skip to content

Fast-path ASCII character class matching #690

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 1 commit into from
Dec 11, 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
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Engine/InstPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ extension Instruction.Payload {
struct QuantifyPayload: RawRepresentable {
let rawValue: UInt64
enum PayloadType: UInt64 {
case bitset = 0
case asciiBitset = 0
case asciiChar = 1
case any = 2
case builtin = 4
Expand Down Expand Up @@ -448,7 +448,7 @@ struct QuantifyPayload: RawRepresentable {
) {
assert(bitset.bits <= _payloadMask)
self.rawValue = bitset.bits
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .bitset, isScalarSemantics: isScalarSemantics)
+ QuantifyPayload.packInfoValues(kind, minTrips, maxExtraTrips, .asciiBitset, isScalarSemantics: isScalarSemantics)
}

init(
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Engine/MEQuantify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extension Processor {
let isScalarSemantics = payload.isScalarSemantics

switch payload.type {
case .bitset:
return input.matchBitset(
case .asciiBitset:
return input.matchASCIIBitset(
registers[payload.bitset],
at: currentPosition,
limitedBy: end,
Expand Down
55 changes: 43 additions & 12 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ extension Processor {
_ bitset: DSLTree.CustomCharacterClass.AsciiBitset,
isScalarSemantics: Bool
) -> Bool {
guard let next = input.matchBitset(
guard let next = input.matchASCIIBitset(
bitset,
at: currentPosition,
limitedBy: end,
Expand Down Expand Up @@ -723,22 +723,53 @@ extension String {
return idx
}

func matchBitset(
func matchASCIIBitset(
_ bitset: DSLTree.CustomCharacterClass.AsciiBitset,
at pos: Index,
limitedBy end: Index,
isScalarSemantics: Bool
) -> Index? {
// TODO: extremely quick-check-able
// TODO: can be sped up with string internals
if isScalarSemantics {
guard pos < end else { return nil }
guard bitset.matches(unicodeScalars[pos]) else { return nil }
return unicodeScalars.index(after: pos)
} else {
guard let (char, next) = characterAndEnd(at: pos, limitedBy: end),
bitset.matches(char) else { return nil }
return next

// FIXME: Inversion should be tracked and handled in only one place.
// That is, we should probably store it as a bit in the instruction, so that
// bitset matching and bitset inversion is bit-based rather that semantically
// inverting the notion of a match or not. As-is, we need to track both
// meanings in some code paths.
let isInverted = bitset.isInverted

// TODO: More fodder for refactoring `_quickASCIICharacter`, see the comment
// there
guard let (asciiByte, next, isCRLF) = _quickASCIICharacter(
at: pos,
limitedBy: end
) else {
if isScalarSemantics {
guard pos < end else { return nil }
guard bitset.matches(unicodeScalars[pos]) else { return nil }
return unicodeScalars.index(after: pos)
} else {
guard let (char, next) = characterAndEnd(at: pos, limitedBy: end),
bitset.matches(char) else { return nil }
return next
}
}

guard bitset.matches(asciiByte) else {
// FIXME: check inversion here after refactored out of bitset
return nil
}

// CR-LF should only match `[\r]` in scalar semantic mode or if inverted
if isCRLF {
if isScalarSemantics {
return self.unicodeScalars.index(before: next)
}
if isInverted {
return next
}
return nil
}

return next
}
}
12 changes: 9 additions & 3 deletions Sources/_StringProcessing/Utility/AsciiBitset.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Probably refactor out of DSLTree
extension DSLTree.CustomCharacterClass {
internal struct AsciiBitset {
let isInverted: Bool
Expand Down Expand Up @@ -49,18 +50,23 @@ extension DSLTree.CustomCharacterClass {
}
}

private func matches(_ val: UInt8) -> Bool {
private func _matchesWithoutInversionCheck(_ val: UInt8) -> Bool {
if val < 64 {
return (a >> val) & 1 == 1
} else {
return (b >> (val - 64)) & 1 == 1
}
}

internal func matches(_ byte: UInt8) -> Bool {
guard byte < 128 else { return isInverted }
return _matchesWithoutInversionCheck(byte) == !isInverted
}

internal func matches(_ char: Character) -> Bool {
let matched: Bool
if let val = char._singleScalarAsciiValue {
matched = matches(val)
matched = _matchesWithoutInversionCheck(val)
} else {
matched = false
}
Expand All @@ -75,7 +81,7 @@ extension DSLTree.CustomCharacterClass {
let matched: Bool
if scalar.isASCII {
let val = UInt8(ascii: scalar)
matched = matches(val)
matched = _matchesWithoutInversionCheck(val)
} else {
matched = false
}
Expand Down