Skip diff checks for read-only hooks#2108
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2108 +/- ##
=======================================
Coverage 92.37% 92.38%
=======================================
Files 120 120
Lines 24772 24804 +32
=======================================
+ Hits 22883 22914 +31
- Misses 1889 1890 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR reduces unnecessary git diff invocations during hook runs by only performing before/after diff checks for priority groups that contain hooks which may modify files, while skipping diff detection for read-only native (builtin / fast-path) hooks.
Changes:
- Added per-hook “may modify files” classification for builtin hooks and fast-path
pre-commit-hooks. - Refactored fast-path detection into a helper and updated
run_fast_pathto use it. - Updated the run loop to lazily capture and compare diffs only when a priority group includes file-modifying hooks; added an integration test asserting no diff detection for a read-only builtin hook.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
crates/prek/src/cli/run/run.rs |
Lazily captures git diff baseline and compares diffs only for priority groups that may modify files. |
crates/prek/src/hooks/mod.rs |
Refactors fast-path resolution and introduces hooks::may_modify_files routing by repo type. |
crates/prek/src/hooks/pre_commit_hooks/mod.rs |
Adds PreCommitHooks::may_modify_files() classification for fast-path pre-commit-hooks. |
crates/prek/src/hooks/builtin_hooks/mod.rs |
Adds BuiltinHooks::may_modify_files() classification for builtin hooks. |
crates/prek/tests/skipped_hooks.rs |
Adds regression test verifying read-only builtin hooks do not trigger diff detection. |
📦 Cargo Bloat ComparisonBinary size change: +0.00% (26.3 MiB → 26.3 MiB) Expand for cargo-bloat outputHead Branch ResultsBase Branch Results |
⚡️ Hyperfine BenchmarksSummary: 3 regressions, 13 improvements above the 10% threshold. Environment
CLI CommandsBenchmarking basic commands in the main repo:
|
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base --version |
1.9 ± 0.0 | 1.8 | 2.1 | 1.00 |
prek-head --version |
1.9 ± 0.1 | 1.8 | 2.2 | 1.00 ± 0.04 |
prek list
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base list |
7.7 ± 2.4 | 7.1 | 31.0 | 1.00 |
prek-head list |
8.1 ± 3.4 | 7.2 | 31.5 | 1.05 ± 0.55 |
prek validate-config .pre-commit-config.yaml
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base validate-config .pre-commit-config.yaml |
2.7 ± 0.6 | 2.6 | 6.7 | 1.00 |
prek-head validate-config .pre-commit-config.yaml |
3.2 ± 3.6 | 2.5 | 27.8 | 1.16 ± 1.33 |
prek validate-config .pre-commit-config.yaml: 15.5600% slower
prek sample-config
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base sample-config |
2.1 ± 0.0 | 2.1 | 2.3 | 1.00 |
prek-head sample-config |
2.3 ± 1.0 | 2.1 | 9.1 | 1.09 ± 0.46 |
Cold vs Warm Runs
Comparing first run (cold) vs subsequent runs (warm cache):
prek run --all-files (cold - no cache)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run --all-files |
111.7 ± 1.0 | 110.2 | 113.5 | 1.83 ± 0.06 |
prek-head run --all-files |
61.1 ± 2.1 | 58.1 | 63.9 | 1.00 |
✅ Performance improvement for prek run --all-files (cold - no cache): 45.3500% faster
prek run --all-files (warm - with cache)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run --all-files |
113.0 ± 4.6 | 107.1 | 128.5 | 1.78 ± 0.16 |
prek-head run --all-files |
63.5 ± 5.0 | 58.3 | 76.5 | 1.00 |
✅ Performance improvement for prek run --all-files (warm - with cache): 43.7900% faster
Full Hook Suite
Running the builtin hook suite on the benchmark workspace:
prek run --all-files (full builtin hook suite)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run --all-files |
114.2 ± 5.8 | 107.0 | 136.4 | 1.74 ± 0.46 |
prek-head run --all-files |
65.4 ± 17.0 | 58.0 | 179.0 | 1.00 |
✅ Performance improvement for prek run --all-files (full builtin hook suite): 42.7000% faster
Individual Hook Performance
Benchmarking each hook individually on the test repo:
prek run trailing-whitespace --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run trailing-whitespace --all-files |
17.0 ± 0.5 | 16.1 | 18.2 | 1.06 ± 0.05 |
prek-head run trailing-whitespace --all-files |
16.1 ± 0.5 | 15.3 | 17.6 | 1.00 |
prek run end-of-file-fixer --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run end-of-file-fixer --all-files |
27.8 ± 32.2 | 18.5 | 194.1 | 1.23 ± 1.45 |
prek-head run end-of-file-fixer --all-files |
22.7 ± 5.2 | 18.7 | 43.7 | 1.00 |
✅ Performance improvement for prek run end-of-file-fixer --all-files: 18.6600% faster
prek run check-json --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-json --all-files |
24.1 ± 40.3 | 9.0 | 169.0 | 3.05 ± 5.50 |
prek-head run check-json --all-files |
7.9 ± 5.2 | 6.2 | 29.7 | 1.00 |
✅ Performance improvement for prek run check-json --all-files: 67.2700% faster
prek run check-yaml --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-yaml --all-files |
14.4 ± 28.8 | 9.0 | 167.1 | 1.73 ± 3.81 |
prek-head run check-yaml --all-files |
8.3 ± 7.7 | 6.2 | 46.7 | 1.00 |
✅ Performance improvement for prek run check-yaml --all-files: 42.3200% faster
prek run check-toml --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-toml --all-files |
9.8 ± 3.2 | 8.9 | 26.6 | 1.51 ± 0.49 |
prek-head run check-toml --all-files |
6.5 ± 0.2 | 6.1 | 6.9 | 1.00 |
✅ Performance improvement for prek run check-toml --all-files: 33.9900% faster
prek run check-xml --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-xml --all-files |
11.5 ± 5.4 | 9.3 | 34.1 | 1.54 ± 0.83 |
prek-head run check-xml --all-files |
7.5 ± 2.0 | 6.7 | 17.9 | 1.00 |
✅ Performance improvement for prek run check-xml --all-files: 34.8800% faster
prek run detect-private-key --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run detect-private-key --all-files |
20.7 ± 24.7 | 12.5 | 147.7 | 1.00 |
prek-head run detect-private-key --all-files |
86.5 ± 137.0 | 9.4 | 373.4 | 4.18 ± 8.28 |
prek run detect-private-key --all-files: 317.8100% slower
prek run fix-byte-order-marker --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run fix-byte-order-marker --all-files |
18.2 ± 3.7 | 15.6 | 36.1 | 1.00 |
prek-head run fix-byte-order-marker --all-files |
30.9 ± 55.0 | 15.9 | 300.2 | 1.70 ± 3.04 |
prek run fix-byte-order-marker --all-files: 69.7400% slower
Installation Performance
Benchmarking hook installation (fast path hooks skip Python setup):
prek install-hooks (cold - no cache)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base install-hooks |
3.9 ± 0.1 | 3.8 | 3.9 | 1.01 ± 0.02 |
prek-head install-hooks |
3.8 ± 0.1 | 3.7 | 3.9 | 1.00 |
prek install-hooks (warm - with cache)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base install-hooks |
3.9 ± 0.1 | 3.8 | 4.0 | 1.01 ± 0.02 |
prek-head install-hooks |
3.8 ± 0.0 | 3.8 | 3.9 | 1.00 |
File Filtering/Scoping Performance
Testing different file selection modes:
prek run (staged files only)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run |
42.1 ± 9.0 | 38.5 | 79.0 | 1.16 ± 0.58 |
prek-head run |
36.3 ± 16.3 | 31.5 | 105.1 | 1.00 |
✅ Performance improvement for prek run (staged files only): 13.6900% faster
prek run --files '*.json' (specific file type)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run --files '*.json' |
7.7 ± 4.0 | 6.6 | 24.6 | 1.13 ± 0.59 |
prek-head run --files '*.json' |
6.8 ± 0.1 | 6.6 | 7.0 | 1.00 |
✅ Performance improvement for prek run --files '*.json' (specific file type): 11.6200% faster
Workspace Discovery & Initialization
Benchmarking hook discovery and initialization overhead:
prek run --dry-run --all-files (measures init overhead)
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run --dry-run --all-files |
9.1 ± 2.1 | 8.3 | 18.0 | 1.52 ± 0.36 |
prek-head run --dry-run --all-files |
6.0 ± 0.1 | 5.8 | 6.1 | 1.00 |
✅ Performance improvement for prek run --dry-run --all-files (measures init overhead): 34.2900% faster
Meta Hooks Performance
Benchmarking meta hooks separately:
prek run check-hooks-apply --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-hooks-apply --all-files |
19.2 ± 36.6 | 8.8 | 151.1 | 2.13 ± 4.06 |
prek-head run check-hooks-apply --all-files |
9.0 ± 0.1 | 8.9 | 9.2 | 1.00 |
✅ Performance improvement for prek run check-hooks-apply --all-files: 53.1000% faster
prek run check-useless-excludes --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run check-useless-excludes --all-files |
9.5 ± 2.2 | 8.7 | 17.5 | 1.07 ± 0.25 |
prek-head run check-useless-excludes --all-files |
8.9 ± 0.1 | 8.8 | 9.1 | 1.00 |
prek run identity --all-files
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|---|---|---|---|---|
prek-base run identity --all-files |
14.4 ± 23.5 | 8.1 | 99.3 | 1.56 ± 2.56 |
prek-head run identity --all-files |
9.2 ± 1.4 | 8.5 | 14.1 | 1.00 |
✅ Performance improvement for prek run identity --all-files: 35.9100% faster
Avoid before-and-after git diff checks for native hooks that cannot modify files, while keeping modification detection for fixers and other hooks that may write files.
For #1327