hmarr / codeowners

🔒 Command line tool and Go library for CODEOWNERS files
MIT License
167 stars 19 forks source link

Simple left-anchored patterns load and match faster #11

Closed hmarr closed 1 year ago

hmarr commented 1 year ago

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