Repository navigation
refactor: stop compiling the AI catalog and loading urllib at import - #693
Conversation
Pre-merge checks:
|
Every pre-commit hook run, and every commit the Action or the App checks, starts a fresh interpreter, so import work is paid once per commit. It had grown by about half since 2.15: importing the CLI compiled all 160+ AI signature regexes, although ai_attribution is off by default, and loaded urllib and importlib.metadata, which only inherit_from URLs and --version use. - Signature patterns keep their source and compile on first use. ALL_PATTERNS is built when something asks for it. - The HTTPS-only opener moves to commit_check._https and is imported when a parent config is fetched. - __version__ is read from the package metadata when it is asked for. CPU instructions for `commit-check -m <file>` drop from 478M to about 250M (python itself is 75M). The benchmarks time test bodies after everything is imported, so they could not see this; startup_test.py now fails if import pulls these modules back in or compiles the catalog, and one benchmark measures the catalog's cold compile as it grows.
9fd940d to
4e1c012
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #693 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 15 +1
Lines 2552 2587 +35
Branches 499 502 +3
=========================================
+ Hits 2552 2587 +35 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will improve performance by ×2.12 warnings, 1 note
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|



What
Importing the CLI no longer compiles the AI signature catalog or loads
urllibandimportlib.metadata. Each is loaded the first time it is used.Every pre-commit hook run, and every commit the Action or the App checks, starts a fresh interpreter, so import cost is paid once per commit. It had grown by about half since 2.15 without anything flagging it. CodSpeed times test bodies after everything is already imported, so it cannot see this cost.
AiSignaturePatternkeeps itssourceandflagsand compilesregexon first use.KnownAiToolkeepsnamesand compilesname_patternon first use.ALL_PATTERNSis built when something asks for it.ai_attributionis off by default, so most runs now compile none of the catalog. With a policy set, the scanner still builds only its grouped alternations, as before; the per-pattern regexes were used only by tests.inherit_fromURLs: the HTTPS-only opener moves tocommit_check/_https.pyand is imported only when a parent config is fetched.urllib.requestandurllib.errortogether cost more than the rest of the CLI's own imports.--version:__version__is read from the package metadata only when something asks for it.from commit_check import __version__still works, which matters for commit-check-mcp.Before vs after
CPU instructions per run (valgrind callgrind, deterministic, wheel installs of
mainand this branch). The Python interpreter alone is 38M.commit-check -m <file>commit-check -bcommit-check -m -b -n -e-m <file>withai_attribution = "forbid"commit-check --versionWall clock, median of 40 runs:
commit-check -m <file>commit-check -m -b -n -eFor reference, 2.15.0 measured 317M for
-m <file>, so this also takes startup below where it was before the AI rules were added.Regexes compiled at import: 193 before, 29 after.
Keeping it from coming back
tests/startup_test.pyimports the CLI in a fresh interpreter. It fails if that import loadsimportlib.metadata,urllib,http.clientorssl, compiles any AI signature pattern, or compiles more than 40 regexes. One new benchmark,test_compiling_the_catalog_cold, measures what the first AI scan of a run pays. CodSpeed will flag it as the catalog grows.Checks
ruff check,ruff format --checkandmypyare clean.