Skip to content

Stop recompiling import-sorting regexes for every file and comparison - #4762

Open
adamjernst wants to merge 1 commit into
uncrustify:masterfrom
adamjernst:perf/cache-import-sort-regex
Open

Stop recompiling import-sorting regexes for every file and comparison#4762
adamjernst wants to merge 1 commit into
uncrustify:masterfrom
adamjernst:perf/cache-import-sort-regex

Conversation

@adamjernst

Copy link
Copy Markdown
Contributor

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.

@micheleCTDEAdmin

Copy link
Copy Markdown
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
adamjernst force-pushed the perf/cache-import-sort-regex branch from 6e2cc80 to 4ab7874 Compare August 24, 2026 16:05
@adamjernst

Copy link
Copy Markdown
Contributor Author

Fixed the failures, and also refactored to avoid adding globals.

Comment thread src/sorting.cpp
&& ch <= 'Z')
|| ( ch >= '0'
&& ch <= '9')
|| ch == '_');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isalnum(ch) || ch == '_' looks better :-)

Comment thread src/sorting.cpp
* 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants