Skip to content

Skip diff checks for read-only hooks#2108

Merged
j178 merged 1 commit into
masterfrom
skip-readonly-diff
May 20, 2026
Merged

Skip diff checks for read-only hooks#2108
j178 merged 1 commit into
masterfrom
skip-readonly-diff

Conversation

@j178

@j178 j178 commented May 20, 2026

Copy link
Copy Markdown
Owner

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

Copilot AI review requested due to automatic review settings May 20, 2026 06:45
@j178 j178 added the performance Performance improvements label May 20, 2026
@codecov

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.83673% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.38%. Comparing base (871b9ed) to head (6adb024).

Files with missing lines Patch % Lines
crates/prek/src/hooks/mod.rs 80.95% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_path to 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.

Comment thread crates/prek/src/cli/run/run.rs
Comment thread crates/prek/src/hooks/mod.rs
Comment thread crates/prek/src/hooks/mod.rs
@prek-ci-bot

prek-ci-bot Bot commented May 20, 2026

Copy link
Copy Markdown

📦 Cargo Bloat Comparison

Binary size change: +0.00% (26.3 MiB → 26.3 MiB)

Expand for cargo-bloat output

Head Branch Results

 File  .text     Size             Crate Name
 1.2%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_41_0_aes_gcm_encrypt_avx512
 1.2%   2.6% 332.0KiB        aws_lc_sys aws_lc_0_41_0_aes_gcm_decrypt_avx512
 0.3%   0.7%  91.3KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.7%  86.4KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.6%  74.7KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.3%   0.5%  70.0KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  51.3KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  48.7KiB              prek prek::run::{{closure}}
 0.2%   0.3%  42.6KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  33.3KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  30.4KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  30.2KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  28.1KiB      serde_saphyr granit_parser::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_41_0_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_41_0_edwards25519_scalarmuldouble
 0.1%   0.2%  26.5KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  23.7KiB              prek prek::hooks::meta_hooks::MetaHooks::run::{{closure}}
 0.1%   0.2%  23.0KiB      serde_saphyr granit_parser::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.5KiB      clap_builder clap_builder::parser::parser::Parser::get_matches_with
41.7%  86.2%  11.0MiB                   And 23983 smaller methods. Use -n N to show more.
48.4% 100.0%  12.7MiB                   .text section size, the file size is 26.3MiB

Base Branch Results

 File  .text     Size             Crate Name
 1.2%   2.5% 332.0KiB        aws_lc_sys aws_lc_0_41_0_aes_gcm_encrypt_avx512
 1.2%   2.5% 332.0KiB        aws_lc_sys aws_lc_0_41_0_aes_gcm_decrypt_avx512
 0.3%   0.7%  92.5KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.7%  86.6KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.6%  74.7KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.3%   0.5%  70.0KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  51.3KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  48.7KiB              prek prek::run::{{closure}}
 0.2%   0.3%  42.3KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  33.3KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  30.4KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  30.2KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  28.1KiB      serde_saphyr granit_parser::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_41_0_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_41_0_edwards25519_scalarmuldouble
 0.1%   0.2%  26.3KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  23.7KiB              prek prek::hooks::meta_hooks::MetaHooks::run::{{closure}}
 0.1%   0.2%  23.0KiB      serde_saphyr granit_parser::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.5KiB      clap_builder clap_builder::parser::parser::Parser::get_matches_with
41.8%  86.2%  11.0MiB                   And 23993 smaller methods. Use -n N to show more.
48.4% 100.0%  12.7MiB                   .text section size, the file size is 26.3MiB

@prek-ci-bot

prek-ci-bot Bot commented May 20, 2026

Copy link
Copy Markdown

⚡️ Hyperfine Benchmarks

Summary: 3 regressions, 13 improvements above the 10% threshold.

Environment
  • OS: Linux 6.17.0-1013-azure
  • CPU: 4 cores
  • prek version: prek 0.4.1+2 (cf58967 2026-05-20)
  • Rust version: rustc 1.95.0 (59807616e 2026-04-14)
  • Hyperfine version: hyperfine 1.20.0
CLI Commands

Benchmarking basic commands in the main repo:

prek --version

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

⚠️ Warning: Performance regression for 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

⚠️ Warning: Performance regression for 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

⚠️ Warning: Performance regression for 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

@j178 j178 merged commit 3184e7d into master May 20, 2026
35 checks passed
@j178 j178 deleted the skip-readonly-diff branch May 20, 2026 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants