Skip to content

Test simple backend patterns with their regex directly - #183

Open
g4lb wants to merge 1 commit into
cpburnz:masterfrom
g4lb:simple-backend-bound-search
Open

g4lb wants to merge 1 commit into
cpburnz:masterfrom
g4lb:simple-backend-bound-search

Conversation

@g4lb

@g4lb g4lb commented Nov 11, 2026

Copy link
Copy Markdown

The simple backend called pattern.match_file() for each pattern and file. RegexPattern.match_file() calls regex.search() and wraps the result in a RegexMatchResult. This change keeps the regex of each pattern in a list when the backend starts, and calls regex.search() directly. A pattern class that overrides match_file(), or a regex that is not a re.Pattern, still uses match_file(). On PyPy, all patterns use match_file(), because the direct call was up to 7% slower there. The results do not change.

match_files() on the CPython source tree (5,614 files), with lines of its .gitignore:

Benchmark CPython 3.9 CPython 3.14
GitIgnoreSpec, 1 line -7.1% -5.9%
GitIgnoreSpec, 15 lines -22.5% -16.8%
GitIgnoreSpec, 150 lines -26.4% -22.6%
PathSpec, 1 line -12.2% -10.4%
PathSpec, 15 lines -23.2% -17.1%
PathSpec, 150 lines -26.4% -21.4%
GitIgnoreSpec, *.py *.c *.h *.rst (most files match) -18.8% -14.1%

Slowdown: the list costs about 55 ns for each pattern when a spec is created. Thus, to create a spec and match one file is up to 1.4% slower. On PyPy 3.11, no benchmark changed significantly.

I used 12 interleaved rounds and a Mann-Whitney test on 33 inputs (Apple M5). I did not measure other architectures.

Checks:

  • tox -e ci-base, ci-re2, ci-hyperscan and docs pass.
  • Old and new results matched on 1M random inputs. They compare match_files(), check_files() and match_file() with str, PathLike and \ separators, on GitIgnoreSpec and PathSpec with all three backends.

This branch has not been deployed

No deployments
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.

1 participant