Skip to content
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

Extend Regex 32-bit/64-bit read optimization to IgnoreCase as well #62459

Closed
stephentoub opened this issue Dec 6, 2021 · 2 comments
Closed

Comments

@stephentoub
Copy link
Member

When comparing a string in a regex (RegexNode.Multi), we unroll the matching for smaller strings. But rather than reading and comparing a single character at a time, we try to read/compare two or four characters at a time by reading the data as a UInt32 or UInt64. We were never able to do this with RegexOptions.IgnoreCase, however, because the design demanded that we char.ToLower each individual character read from the input. But, we're now switching to a design where casing is almost entirely handled at parse time (#61048), where most letters are then parsed into a set, e.g. 'a' becomes '[Aa]'. For most of ASCII, then, we can actually apply this optimization, e.g. the string "abcd" becomes "[Aa][Bb][Cc][Dd]", and we can read a UInt64 input OR'd with 0x20202020, and then compare it against the precomputed UInt64 value similarly OR'd with 0x20202020. This will give us most of the vectorization gains we see for case-sensitive for case-insensitive as well.

@stephentoub stephentoub added the tenet-performance Performance related issue label Dec 6, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.RegularExpressions untriaged New issue has not been triaged by the area owner labels Dec 6, 2021
@stephentoub stephentoub removed the untriaged New issue has not been triaged by the area owner label Dec 6, 2021
@stephentoub stephentoub added this to the 7.0.0 milestone Dec 6, 2021
@ghost
Copy link

ghost commented Dec 6, 2021

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

When comparing a string in a regex (RegexNode.Multi), we unroll the matching for smaller strings. But rather than reading and comparing a single character at a time, we try to read/compare two or four characters at a time by reading the data as a UInt32 or UInt64. We were never able to do this with RegexOptions.IgnoreCase, however, because the design demanded that we char.ToLower each individual character read from the input. But, we're now switching to a design where casing is almost entirely handled at parse time (#61048), where most letters are then parsed into a set, e.g. 'a' becomes '[Aa]'. For most of ASCII, then, we can actually apply this optimization, e.g. the string "abcd" becomes "[Aa][Bb][Cc][Dd]", and we can read a UInt64 input OR'd with 0x20202020, and then compare it against the precomputed UInt64 value similarly OR'd with 0x20202020. This will give us most of the vectorization gains we see for case-sensitive for case-insensitive as well.

Author: stephentoub
Assignees: -
Labels:

area-System.Text.RegularExpressions, tenet-performance, untriaged

Milestone: -

@stephentoub
Copy link
Member Author

Fixed by #66339

@ghost ghost locked as resolved and limited conversation to collaborators Apr 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant