Stop recompiling import-sorting regexes for every file and comparison - #4762
Open
adamjernst wants to merge 1 commit into
Open
Stop recompiling import-sorting regexes for every file and comparison#4762adamjernst wants to merge 1 commit into
adamjernst wants to merge 1 commit into
Conversation
adamjernst
requested review from
PoeticPeteAdmin,
gmaurel,
micheleCTDEAdmin and
mwoehlke-kitware
as code owners
August 21, 2026 17:55
Collaborator
|
please amend in order to pass all actions. |
Import sorting was the single largest source of std::regex compilation in profiles of an Objective-C workload, in two places: prepare_categories() built a fresh std::regex for each include_category_N option at the start of every file, and cleanup_categories() destroyed them again at the end. Keep the compiled regexes between files and only rebuild one when the option string it came from has changed. text_contains_filename_without_ext() compiled two regexes on every call that missed the cache: one to escape the filename, and one for the pattern '\S?<filename>\b.*' that it then ran through std::regex_match(). That pattern can only match with the filename at offset 0, or at offset 1 behind a single non-space character, so test those two positions with a plain string compare plus a word-boundary check. Neither the sort options nor the configured categories change. A differential test of the old regex against the replacement over 1908 filename/text pairs, covering regex metacharacters in filenames, empty filenames, extensionless paths and leading whitespace, reports no behavior difference, and formatting 941 files in a single process produces byte-identical output. Measured on a large Objective-C corpus: 93.592s before, 79.209s after replacing the filename regex, 60.667s after also caching the category regexes. std::__detail::__compile_nfa no longer appears in the profile.
adamjernst
force-pushed
the
perf/cache-import-sort-regex
branch
from
August 24, 2026 16:05
6e2cc80 to
4ab7874
Compare
Contributor
Author
|
Fixed the failures, and also refactored to avoid adding globals. |
| && ch <= 'Z') | ||
| || ( ch >= '0' | ||
| && ch <= '9') | ||
| || ch == '_'); |
Collaborator
There was a problem hiding this comment.
isalnum(ch) || ch == '_' looks better :-)
| * This used to build the equivalent regex '\S?<filename>\b.*' and run | ||
| * std::regex_match() against it, which meant escaping the filename and | ||
| * compiling two regexes on every call. The pattern only allows the filename at | ||
| * offset 0, or at offset 1 behind a single non-space character, so match those |
Collaborator
There was a problem hiding this comment.
Since we are reworking this logic, would it make sense to remove the 0 or 1 offset? I am not really sure what the original logic intended to do with that offset. Any idea?
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.
Import sorting was the single largest source of std::regex compilation in profiles of an Objective-C workload, in two places:
prepare_categories() built a fresh std::regex for each include_category_N option at the start of every file, and cleanup_categories() destroyed them again at the end. Keep the compiled regexes between files and only rebuild one when the option string it came from has changed.
text_contains_filename_without_ext() compiled two regexes on every call that missed the cache: one to escape the filename, and one for the pattern '\S?\b.*' that it then ran through std::regex_match(). That pattern can only match with the filename at offset 0, or at offset 1 behind a single non-space character, so test those two positions with a plain string compare plus a word-boundary check.
Neither the sort options nor the configured categories change. A differential test of the old regex against the replacement over 1908 filename/text pairs, covering regex metacharacters in filenames, empty filenames, extensionless paths and leading whitespace, reports no behavior difference, and formatting 941 files in a single process produces byte-identical output.
Measured on a large Objective-C corpus: 93.592s before, 79.209s after replacing the filename regex, 60.667s after also caching the category regexes. std::__detail::__compile_nfa no longer appears in the profile.