Skip to content

Simple left-anchored patterns load and match faster #11

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

Merged
merged 1 commit into from
Oct 25, 2022

Conversation

hmarr
Copy link
Owner

@hmarr hmarr commented Oct 25, 2022

Avoid building and compiling a regex for simple patterns (those that don't contain a *, ?, or / character), and instead do a relatively simple string prefix match.

Results on a relatively large CODEOWNERS file that mostly contains simple rules:

$ hyperfine 'codeowners README.md' 'codeowners-new README.md'
Benchmark 1: codeowners README.md
  Time (mean ± σ):     195.0 ms ±   1.5 ms    [User: 307.6 ms, System: 23.4 ms]
  Range (min … max):   192.3 ms … 197.0 ms    15 runs

Benchmark 2: codeowners-new README.md
  Time (mean ± σ):      45.1 ms ±   0.8 ms    [User: 55.0 ms, System: 7.5 ms]
  Range (min … max):    43.7 ms …  47.5 ms    61 runs

Summary
  'codeowners-new README.md' ran
    4.33 ± 0.09 times faster than 'codeowners README.md'

A profile suggests that the speedup there is so dramatic mostly because we're compiling fewer regexp objects, which happens once at parse time.

However, a quick test of running this branch against a repository with a large number of files and rules indicates that the matching speed is also improved:

$ time codeowners
codeowners  238.78s user 4.73s system 127% cpu 3:11.58 total
$ time codeowners-new
codeowners-new  188.28s user 9.31s system 170% cpu 1:55.77 total
$ time codeowners
codeowners  237.87s user 4.48s system 128% cpu 3:08.60 total
$ time codeowners-new
codeowners-new  188.10s user 9.30s system 170% cpu 1:56.00 total

Avoid building and compiling a regex for simple patterns (those that
don't contain a *, ?, or / character), and instead do a relatively
simple string prefix match.
@hmarr hmarr changed the title Faster matching for simple left-anchored patterns Simple left-anchored patterns load and match faster Oct 25, 2022
@hmarr hmarr merged commit 75f94cf into main Oct 25, 2022
@hmarr hmarr deleted the optimise-prefix-matches branch October 25, 2022 14:35
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