Repository navigation
<regex>: Accelerate greedy loops of small or negated empty character classes - #6540
Open
Julian Müller (muellerj3) wants to merge 7 commits into
Open
Julian Müller (muellerj3) wants to merge 7 commits into
Julian Müller (muellerj3) wants to merge 7 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
…he code points of a and b when narrowing to unsigned char
…hose lower-case version has code point < 256 in icase mode
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
Author
|
I addressed the Copilot remark on missing test coverage. I also deliberately changed the unicode characters with code points >= 256 to be matched to character class |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Towards #5971. This special-cases loops over the simplest kinds of character classes: The negated empty character class
[^], which matches everything, and a character class that is fully described by a bitmap over the first 256 code points. Thus, this essentially handles the character classes for character typecharexcept for those involving some collation feature. It does not handle all relevant character classes forwchar_t, e.g., those derived from a named character class such as[[:alpha:]]or\s.[^]*is by far the fastest way to write a loop now, beating even.*, as the matcher can just jump ahead to the end of the input string in constant time. This is why the complexity counter does not have to account for the length of the input consumed by[^]*.It turns out that handling the negation of the character class outside of the for loop in
_Find_first_char_not_in_char_classnoticeably improves performance (increasing the speedup from ~9 to ~12 on my machine), so the subtype labels for these character classes also differentiate by negation of the character class in addition to icase and collate mode.The subtype of a negated class in collate mode is missing because it can't happen currently: A loop over a negated class is never marked as simple in
_Calculate_loop_simplicity()in collate mode because we haven't fully settled the semantics of such classes yet. But it feels like a strange wart that this subtype is missing, so maybe we should commit to specific semantics in time for 14.53. I will open an issue about this over the next few days._Rx_translate_identityis essentially a copy ofstd::identity. Butstd::identityis only available starting from C++20, while<regex>is supported in all standard modes. Feel free to change this if you prefer another solution.With this PR, we would have essentially run out of labels for special loop cases. Since it's certain we need more to speed up character classes for type
wchar_t, I doubled them by reserving another bit. This doesn't break ABI because there hasn't been an ABI-stable release yet that includes these subtypes.(I wonder if there is a better solution than
boolas a template parameter of_Find_first_char_not_in_char_class(). It feels a bit strange thattruemeans negated and not that this means the class is positive/non-negated, but this is consistent with some other pieces of code in<regex>. I considered using an enum class with underlying typeboolinstead, but then I wondered if this is worth it for bool. I also remember that some compilers, Clang included, don't handle such types correctly in some corner cases.)Benchmarks
regex_match