Skip to content

#3914: compile the language registry's rules on first use, not at import - #3923

Merged
squid-protocol merged 2 commits into
mainfrom
claude/lazy-language-patterns
Sep 27, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
claude/lazy-language-patterns

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

import gitgalaxy.galaxyscope took 3.6 s, 2.3 s of it compiling ~2,200 rules across every language at import -- paid by every scanning process (and, under spawn, again by every worker unpickling its config), although a scan uses the rules of the languages it meets.

  • language_standards/_lazy_re.py: re for the language modules, with compile returning a LazyPattern -- a duck-typed re.Pattern that keeps .pattern without compiling, compiles on first use (.flags, any match method), pickles as its source, copies as itself, and equals/hashes as the compiled pattern. is_pattern() / materialize() helpers.
  • the 55 language modules import it as re (one line each).
  • detector: _compiled_rules() compiles a language's rules in place the first time it is read (primary or embedded), so the hot path and its isinstance(p, re.Pattern) checks see real patterns.
  • tests: registry audits that filtered on isinstance(p, re.Pattern) use is_pattern (they would otherwise skip every rule); new test_lazy_re.py, including a check that every rule of every language still compiles (the fail-fast a broken regex used to get at import).

Import: 3.6 s -> 1.6 s; no rule compiled at import.

Claude-Session: https://claude.ai/code/session_01X9PBcL8evG7CEzff1zE8v5

Description

Core Engine Modification Checklist

If your PR changes the core parsing engine (language_standards.py, detector.py, prism.py), you MUST complete the following validation gauntlet before merging:

  • Golden Master Verification: Ran crucible_check.py to verify diffs, and ran crucible_check.py --update --yes if the baseline changed due to intentional parsing improvements.
  • Tri-Comparison Audit: Handled by CI automatically, but you should run python tests/tools/tri_comparison_chart.py --all --ci locally first. If you intentionally improved precision, regenerate the baseline: python tests/tools/tri_comparison_chart.py --regenerate --languages <lang>.
  • Tree-Sitter Accuracy: Ran python tests/tools/tree_sitter_accuracy_audit.py --ci --all. If ground truth drifted for valid reasons, regenerated the baseline with python tests/tools/tree_sitter_accuracy_audit.py --regenerate --lang <lang>.

Note for AI Agents: If you are an AI agent generating this PR, you MUST invoke the ci-push-checklist skill to ensure these steps are completed properly.

`import gitgalaxy.galaxyscope` took 3.6 s, 2.3 s of it compiling ~2,200 rules
across every language at import -- paid by every scanning process (and, under
`spawn`, again by every worker unpickling its config), although a scan uses
the rules of the languages it meets.

- language_standards/_lazy_re.py: `re` for the language modules, with
  `compile` returning a LazyPattern -- a duck-typed re.Pattern that keeps
  .pattern without compiling, compiles on first use (.flags, any match
  method), pickles as its source, copies as itself, and equals/hashes as the
  compiled pattern. is_pattern() / materialize() helpers.
- the 55 language modules import it as `re` (one line each).
- detector: _compiled_rules() compiles a language's rules in place the first
  time it is read (primary or embedded), so the hot path and its
  isinstance(p, re.Pattern) checks see real patterns.
- tests: registry audits that filtered on isinstance(p, re.Pattern) use
  is_pattern (they would otherwise skip every rule); new test_lazy_re.py,
  including a check that every rule of every language still compiles (the
  fail-fast a broken regex used to get at import).

Import: 3.6 s -> 1.6 s; no rule compiled at import.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X9PBcL8evG7CEzff1zE8v5
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 34fc2dc into main Sep 27, 2026
37 checks passed
@squid-protocol
squid-protocol deleted the claude/lazy-language-patterns branch September 27, 2026 20:52
@squid-protocol squid-protocol added this to the Yes, but can it go faster...? milestone Sep 27, 2026 — with Claude
@squid-protocol squid-protocol added core-engine Modifications to the central physics and parsing engine performance labels Sep 27, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

Outcome: a partial win. It caused a regression on Linux, #3929, which is fixed by #3931 and #3939.

What held up:

  • import gitgalaxy.galaxyscope went from 3.6 s to 1.6 s, with no rule compiled at import.
  • Small and medium scans got about 30% faster end to end. On 40 C files, the whole command went from 7.2 s to 5.1 s.
  • Output is unchanged: the golden masters are byte-identical in both modes.

What it broke:

Part of that 9% was the metric, not the engine. phase_ab's wall is PIPELINE_SUCCESS, which starts after the import. This PR moved about 2 s of compiling from before that clock into it. #3931 added a process column that times the whole command.

The chain:


Generated by Claude Code

squid-protocol pushed a commit that referenced this pull request Sep 27, 2026
assert_redos_immune spawns a child and allows it timeout_sec (1 s) from
start(). Since #3923 a registry rule is a LazyPattern, which unpickles
through gitgalaxy's language registry: the child imported 74 gitgalaxy
modules (258 ms here, more on a loaded runner) inside that window and a
linear rule read as hung (macOS 3.9 on #3939: agc_assembly func_start).

_plain() compiles the rule in the parent; a plain pattern unpickles through
`re` alone (0.6 ms, no gitgalaxy import). _best_of_timing compiles before
its clock too. A disabled rule (None) passes through as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X9PBcL8evG7CEzff1zE8v5
squid-protocol added a commit that referenced this pull request Sep 27, 2026
…scans keep #3923's 30% win) (#3939)

* #3929: pre-compile before a fork only for a scan of 500+ files

Three-way A/B (pre-#3914 / main / fix, A-vs-A control, whole-command time):
pre-compiling every claimant in the parent took the 3,280-file crucible
corpus to -0.7% (main +0.2%), but made a 40-file C scan slower than main
(-21% vs -26% against pre-#3914): its 3 workers start on idle cores, where
compiling lazily -- in parallel, only what each meets (never objective-c
there) -- beats one serial compile. Large scans keep every core busy to the
end, where a worker's own compile is wall time (#3929: curl -0.47 s).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X9PBcL8evG7CEzff1zE8v5

* #3914: ReDoS harnesses send their child a plain compiled pattern

assert_redos_immune spawns a child and allows it timeout_sec (1 s) from
start(). Since #3923 a registry rule is a LazyPattern, which unpickles
through gitgalaxy's language registry: the child imported 74 gitgalaxy
modules (258 ms here, more on a loaded runner) inside that window and a
linear rule read as hung (macOS 3.9 on #3939: agc_assembly func_start).

_plain() compiles the rule in the parent; a plain pattern unpickles through
`re` alone (0.6 ms, no gitgalaxy import). _best_of_timing compiles before
its clock too. A disabled rule (None) passes through as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01X9PBcL8evG7CEzff1zE8v5

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-engine Modifications to the central physics and parsing engine performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants