Title: Pre-commit performance issues in our large repository · Issue #3603 · pre-commit/pre-commit · GitHub
Open Graph Title: Pre-commit performance issues in our large repository · Issue #3603 · pre-commit/pre-commit
X Title: Pre-commit performance issues in our large repository · Issue #3603 · pre-commit/pre-commit
Description: search you tried in the issue tracker is:issue state:open rev-list describe your issue Summary We are experiencing two significant performance bottlenecks: _rev_exists() (pre-push): Uses git rev-list --quiet which walks the entire commit...
Open Graph Description: search you tried in the issue tracker is:issue state:open rev-list describe your issue Summary We are experiencing two significant performance bottlenecks: _rev_exists() (pre-push): Uses git rev-li...
X Description: search you tried in the issue tracker is:issue state:open rev-list describe your issue Summary We are experiencing two significant performance bottlenecks: _rev_exists() (pre-push): Uses git rev-li...
Opengraph URL: https://github.com/pre-commit/pre-commit/issues/3603
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Pre-commit performance issues in our large repository","articleBody":"### search you tried in the issue tracker\n\nis:issue state:open rev-list\n\n### describe your issue\n\n## Summary\n\nWe are experiencing two significant performance bottlenecks:\n\n1. **`_rev_exists()`** (pre-push): Uses `git rev-list --quiet` which walks the entire commit history (~5.2s)\n2. **`_get_diff()`** (pre-commit): Scans the entire working tree after each hook (~250ms × N hooks)\n\n---\n\n## Issue 1: `_rev_exists()` in hook_impl.py (PRE-PUSH)\n\n### Problem\n\n```python\ndef _rev_exists(rev: str) -> bool:\n return not subprocess.call(('git', 'rev-list', '--quiet', rev))\n```\n\n`git rev-list --quiet` walks the entire commit history to verify a revision exists.\n\n### Profiling\n\n| Command | Time | Notes |\n|---------|------|-------|\n| `git rev-list --quiet HEAD` | **5,201ms** | Current - walks ~1M commits |\n| `git rev-parse --verify --quiet HEAD` | **2.6ms** | Fix - just validates ref |\n\n**Speedup: 2000x**\n\n### Verified Fix\n\n```python\ndef _rev_exists(rev: str) -> bool:\n return not subprocess.call(\n ('git', 'rev-parse', '--verify', '--quiet', rev),\n stdout=subprocess.DEVNULL,\n stderr=subprocess.DEVNULL,\n )\n```\n\n**Safety verification:**\n- Z40 (null SHA) case is already guarded: `remote_sha != Z40 and _rev_exists(remote_sha)`\n- Exit codes: both return 0 for valid refs, non-zero for invalid\n- Tested with: valid refs, invalid refs, commit SHAs\n\n---\n\n## Issue 2: `_get_diff()` in run.py (PRE-COMMIT)\n\n### Problem\n\n```python\ndef _get_diff() -> bytes:\n _, out, _ = cmd_output_b(\n 'git', 'diff', '--no-ext-diff', '--no-textconv', '--ignore-submodules',\n check=False,\n )\n return out\n```\n\nCalled after **every hook that runs** to detect if files were modified. Scans entire working tree.\n\n### Profiling\n\n| Scope | Time | Speedup |\n|-------|------|---------|\n| Full working tree | 250ms | 1x |\n| Scoped to hook files | 16ms | **15x** |\n\nWith 50 hooks running: 50 calls × 250ms = **12.5s seconds overhead**\n\n### Verified Fix\n\nScope diff to the files the hook processed:\n\n```python\ndef _get_diff(files: tuple[str, ...] = ()) -> bytes:\n cmd = ['git', 'diff', '--no-ext-diff', '--no-textconv', '--ignore-submodules']\n if files:\n cmd.extend(['--', *files])\n _, out, _ = cmd_output_b(*cmd, check=False)\n return out\n```\n\n**Important caveat:** Only safe when `pass_filenames: true` (the default).\n\nFor hooks with `pass_filenames: false`, must fall back to full tree diff since they may modify files outside their trigger patterns (e.g., code generators).\n\nIn `_run_single_hook()`:\n```python\n# filenames from classifier are available before pass_filenames check\nfilenames = tuple(classifier.filenames_for_hook(hook))\n...\nif not hook.pass_filenames:\n # Can't scope diff - hook may modify any file\n diff_after = _get_diff()\nelse:\n # Safe to scope diff to the files hook received\n diff_after = _get_diff(filenames)\n```\n---\n\n## Other Slow Operations (No Easy Fix)\n\nThese scan the working tree and can't be easily optimized:\n\n| Operation | Time | Location | Notes |\n|-----------|------|----------|-------|\n| `intent_to_add_files()` | 257ms | git.py | Full tree scan required |\n| `_unstaged_changes_cleared()` | 265ms | staged_files_only.py | Full tree scan required |\n\nCould benefit from git's fsmonitor?\n\n---\n\n## Reproduction\n\n```bash\n# Test _rev_exists performance\npython3 -c \"\nimport time, subprocess\nfor cmd, name in [\n (['git', 'rev-list', '--quiet', 'HEAD'], 'current'),\n (['git', 'rev-parse', '--verify', '--quiet', 'HEAD'], 'fix'),\n]:\n start = time.perf_counter()\n subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n print(f'{name}: {(time.perf_counter()-start)*1000:.1f}ms')\n\"\n\n# Test _get_diff performance\npython3 -c \"\nimport time, subprocess\nfor scope in ['', '-- path/to/file.py']:\n cmd = ['git', 'diff', '--no-ext-diff', '--no-textconv', '--ignore-submodules'] + scope.split()\n start = time.perf_counter()\n subprocess.run(cmd, capture_output=True)\n print(f'{scope or 'full tree'}: {(time.perf_counter()-start)*1000:.1f}ms')\n\"\n```\n\n\n### pre-commit --version\n\n4.5.0\n\n### .pre-commit-config.yaml\n\n```yaml\n(sadly I can't share)\n```\n\n### ~/.cache/pre-commit/pre-commit.log (if present)\n\n_No response_","author":{"url":"https://github.com/ptarjan","@type":"Person","name":"ptarjan"},"datePublished":"2025-12-26T21:26:52Z","url":"https://github.com/pre-commit/pre-commit/issues/3603"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:577575d7-186d-7653-4dae-2310a3502c88 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9DB4:15F7F4:15ED1C4:1CEFBC4:6B066908 |
| html-safe-nonce | d7de05ccc057c1315abed1d62d8b2925d089a5e9be4a113b2276cc55c856af59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5REI0OjE1RjdGNDoxNUVEMUM0OjFDRUZCQzQ6NkIwNjY5MDgiLCJ2aXNpdG9yX2lkIjoiNjcyNzg3NTUxNDMwMjA5NzY3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b24b384ea439fd893f1c982c32827d3716505d86b21b47cf9fb88e95bd2bf288 |
| hovercard-subject-tag | issue:3764029052 |
| github-keyboard-shortcuts | repository,issues,commits,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pre-commit/pre-commit/3603/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5f8362f96688f38cc87a64edb3ca2a06a4dc474f953dbaa2ab599c4e82bf6798/pre-commit/pre-commit/issues/3603 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5f8362f96688f38cc87a64edb3ca2a06a4dc474f953dbaa2ab599c4e82bf6798/pre-commit/pre-commit/issues/3603 |
| og:image:alt | search you tried in the issue tracker is:issue state:open rev-list describe your issue Summary We are experiencing two significant performance bottlenecks: _rev_exists() (pre-push): Uses git rev-li... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ptarjan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6324e6ac990fb6befbec840cc0e102a1071c339bfe8f9826bb12de2e3ba1d297 |
| turbo-cache-control | no-preview |
| go-import | github.com/pre-commit/pre-commit git https://github.com/pre-commit/pre-commit.git |
| octolytics-dimension-user_id | 6943086 |
| octolytics-dimension-user_login | pre-commit |
| octolytics-dimension-repository_id | 17689377 |
| octolytics-dimension-repository_nwo | pre-commit/pre-commit |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 17689377 |
| octolytics-dimension-repository_network_root_nwo | pre-commit/pre-commit |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7f0aa75f4141d440c6110dc31af7ea7797abb6c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width