-
Notifications
You must be signed in to change notification settings - Fork 51
Use a bitset for ascii-only character classes #511
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
rctcwyvrn
merged 35 commits into
swiftlang:main
from
rctcwyvrn:many-closures-vs-one-bitset-boi
Jun 29, 2022
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
5fd8840
[benchmark] Add no-capture version of grapheme breaking exercise
milseman 03fe8d6
[benchmark] Add cross-engine benchmark helpers
milseman 5667705
[benchmark] Hangul Syllable finding benchmark
milseman bde259b
Add debug mode
rctcwyvrn bf95e81
Fix typo in css regex
rctcwyvrn 243ec7b
Add HTML benchmark
rctcwyvrn eeb0852
Add email regex benchmarks
rctcwyvrn 49efd67
Add save/compare functionality to the benchmarker
rctcwyvrn b3a61a7
Clean up compare and add cli flags
rctcwyvrn 926d208
Merge branch 'main' into more_more_benchmarks
milseman 752ea76
Make fixes
rctcwyvrn 7327e74
Merge branch 'more_more_benchmarks' of github.com:rctcwyvrn/swift-exp…
rctcwyvrn 7a900b6
oops, remove some leftover code
rctcwyvrn 50e8e6d
Fix linux build issue + add cli option for specifying compare file
rctcwyvrn 3c7f62c
First ver of bitset character classes
rctcwyvrn b71b177
Did a dumb and didn't use the new api I had added...
rctcwyvrn e2a011c
Fix bug in inverted character sets
rctcwyvrn f7900e5
Remove nested chararcter class cases
rctcwyvrn e9d1902
Remove comment
rctcwyvrn cf59091
Merge branch 'main' into many-closures-vs-one-bitset-boi
rctcwyvrn f4019d4
Cleanup handling of isInverted
rctcwyvrn ed82cb0
Cleanup
rctcwyvrn cc1ac9d
Remove isCaseInsensitive property
rctcwyvrn ccf6ade
Add tests for special cases
rctcwyvrn 7b83e0c
Use switch on ranges instead of if
rctcwyvrn 5121076
Rename asciivalue to singleScalarAsciiValue
rctcwyvrn 3607b65
Properly handle unicode scalars mode in custom character classes
rctcwyvrn 291a974
I most definitely did not forget to commit the tests
rctcwyvrn ddcf40f
Cleanup
rctcwyvrn f87b325
Add support for testing if compilation contains certain opcodes
rctcwyvrn 2d8ac2d
Forgot the tests again, twice in one day...
rctcwyvrn fd66693
Spelling mistakes
rctcwyvrn 22c8213
Make expectProgram take sets of opcodes
rctcwyvrn 0781b93
Add compiler options + validation testing against unoptimized regexes
rctcwyvrn ffff944
Cleanup, clear cache of Regex.Program when setting new compile options
rctcwyvrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Cleanup
- Loading branch information
commit ed82cb00a3e74b3f0fb89edf9f86b4bda0679773
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,39 +52,22 @@ extension DSLTree.Node { | |
} | ||
|
||
extension DSLTree._AST.Atom { | ||
func isAscii() -> Bool { | ||
return ast.isAscii() | ||
} | ||
|
||
func toAsciiValue() -> UInt8 { | ||
return ast.toAsciiValue() | ||
var asciiValue: UInt8? { | ||
return ast.asciiValue | ||
} | ||
} | ||
|
||
extension DSLTree.Atom { | ||
func isAscii() -> Bool { | ||
var asciiValue: UInt8? { | ||
switch self { | ||
case let .char(c): | ||
return c.isASCII | ||
case let .scalar(s): | ||
return s.isASCII | ||
case let .unconverted(atom): | ||
return atom.isAscii() | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func toAsciiValue() -> UInt8 { | ||
switch self { | ||
case let .char(c): | ||
return c.asciiValue! | ||
case let .scalar(s): | ||
case let .char(c) where c != "\r\n": | ||
return c.asciiValue | ||
case let .scalar(s) where s.isASCII: | ||
return UInt8(ascii: s) | ||
case let .unconverted(atom): | ||
return atom.toAsciiValue() | ||
return atom.asciiValue | ||
default: | ||
fatalError("Should have been checked by isAscii first") | ||
return nil | ||
} | ||
} | ||
|
||
rctcwyvrn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -213,26 +196,15 @@ extension AST.Atom { | |
default: return nil | ||
} | ||
} | ||
|
||
func isAscii() -> Bool { | ||
switch kind { | ||
case let .char(c): | ||
return c.isASCII | ||
case let .scalar(s): | ||
return s.value.isASCII | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func toAsciiValue() -> UInt8 { | ||
var asciiValue: UInt8? { | ||
switch kind { | ||
case let .char(c): | ||
return c.asciiValue! | ||
case let .scalar(s): | ||
case let .char(c) where c != "\r\n": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future cleanup: something like the below to consolidate logic
|
||
return c.asciiValue | ||
case let .scalar(s) where s.value.isASCII: | ||
return UInt8(ascii: s.value) | ||
default: | ||
fatalError("Should have been checked by isAscii first") | ||
return nil | ||
} | ||
} | ||
|
||
|
@@ -293,40 +265,32 @@ extension AST.Atom { | |
} | ||
|
||
extension DSLTree.CustomCharacterClass.Member { | ||
func isAscii() -> Bool { | ||
switch self { | ||
case let .atom(a): | ||
return a.isAscii() | ||
case let .range(low, high): | ||
return low.isAscii() && high.isAscii() | ||
// The remaining cases have nested character classes with possibly different | ||
// inversion so leave them out of this optimization | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func asAsciiBitset( | ||
_ opts: MatchingOptions, | ||
_ isInverted: Bool | ||
) -> DSLTree.CustomCharacterClass.AsciiBitset { | ||
) -> DSLTree.CustomCharacterClass.AsciiBitset? { | ||
switch self { | ||
case let .atom(a): | ||
return DSLTree.CustomCharacterClass.AsciiBitset( | ||
a.toAsciiValue(), | ||
isInverted, | ||
opts.isCaseInsensitive | ||
) | ||
if let val = a.asciiValue { | ||
return DSLTree.CustomCharacterClass.AsciiBitset( | ||
val, | ||
isInverted, | ||
opts.isCaseInsensitive | ||
) | ||
} | ||
case let .range(low, high): | ||
return DSLTree.CustomCharacterClass.AsciiBitset( | ||
low: low.toAsciiValue(), | ||
high: high.toAsciiValue(), | ||
isInverted: isInverted, | ||
isCaseInsensitive: opts.isCaseInsensitive | ||
) | ||
if let lowVal = low.asciiValue, let highVal = high.asciiValue { | ||
return DSLTree.CustomCharacterClass.AsciiBitset( | ||
low: lowVal, | ||
high: highVal, | ||
isInverted: isInverted, | ||
isCaseInsensitive: opts.isCaseInsensitive | ||
) | ||
} | ||
default: | ||
fatalError("Should have been checked by isAscii first") | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func generateConsumer( | ||
|
@@ -436,14 +400,16 @@ extension DSLTree.CustomCharacterClass.Member { | |
} | ||
|
||
extension DSLTree.CustomCharacterClass { | ||
func isAscii() -> Bool { | ||
return members.allSatisfy { member in member.isAscii() } | ||
} | ||
|
||
func asAsciiBitset(_ opts: MatchingOptions) -> AsciiBitset { | ||
func asAsciiBitset(_ opts: MatchingOptions) -> AsciiBitset? { | ||
return members.reduce( | ||
.init(isInverted: isInverted, isCaseInsensitive: opts.isCaseInsensitive), | ||
{result, member in result.union(member.asAsciiBitset(opts, isInverted))} | ||
{result, member in | ||
if let next = member.asAsciiBitset(opts, isInverted) { | ||
return result?.union(next) | ||
} else { | ||
return nil | ||
} | ||
} | ||
) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.