Skip to content

Lazy rule compilation (#3923) makes Linux scans ~9% slower: forked workers each recompile the rules #3929

Closed

Description

@squid-protocol

Summary

#3923 (for #3914) made rule compilation lazy: rules compile on first use, not at import. That took import gitgalaxy.galaxyscope from 3.6 s to 1.6 s, a real win under spawn, which macOS and Windows use by default.

On Linux, though, the scan's worker pool uses fork. Before #3923 the rules were compiled once in the parent and every worker inherited them for free. Now each worker compiles the same rules itself, the first time it meets each language. On a multi-worker scan that costs more than it saves: curl got ~9% slower in wall time.

Measurements

Setup:

  • curl (/nvme-data/gitgalaxy-data/curl, 4,250 files) on a 12-CPU Linux box, Python 3.12.
  • tests/tools/phase_ab.py: alternating rounds, an A-vs-A control, and GITGALAXY_LICENSE_KEY=COMMUNITY_FREE_TIER so the licence delay doesn't count.

1. Before and after #3923 (2 rounds; the A-vs-A control moved wall by −0.7%):

engine wall 3_Language_Detector (CPU) 5_Optical_Detector (CPU)
main before #3923 (c69bea35) 7.14 s 2.04 s 16.35 s
main with #3923 (34fc2dc9) 7.78 s 2.60 s 16.95 s

2. Mechanism facts:

  • multiprocessing.get_start_method() is fork. The pool is a plain ProcessPoolExecutor (galaxyscope.py, the Full Suite Gate: Windows at its 30-minute limit, because every tiny test scan spawns worker processes that re-import GitGalaxy #3913 pool block), and there are 12 workers.
  • Compiling the rules of curl's 15 languages via detector._compiled_rules costs 302 ms per process: c 85 ms, cpp 98, perl 55, python 42, the rest under 5 each.
  • With lazy compilation, each worker pays that the first time it meets each language: up to 12 × ~0.3 s ≈ 3.6 s of repeated CPU.
  • Compiling every language's rules takes about 1.5 s (1,208 ms for the other 40), so compiling everything up front is not the fix.

3. Direct test: the same main engine (34fc2dc9), plain versus a driver that compiles curl's 15 languages in the parent before main(). 3 alternating rounds:

variant wall per round mean
lazy (main as is) 7.82 / 7.81 / 7.75 7.79 s
parent-precompiled 7.31 / 7.33 / 7.32 7.32 s (−0.47 s, −6%)

The precompiled wall includes the parent's ~0.3 s of compiling, and it still recovers most of the 0.64 s regression.

Proposed fix

When the pool's start method is fork, compile in the parent the rules of the languages the Phase-0 census found, right before creating the ProcessPoolExecutor. That means _compiled_rules(LANGUAGE_DEFINITIONS[lang]) for each language in the census or ext tally, plus the embedded languages they pull in.

Possible refinement: start that parent compile on a thread during the census, so its ~0.3 s overlaps with I/O.

Acceptance: a phase_ab A/B on curl shows wall back to at or below the pre-#3923 7.14 s. The deterministic surface (audit.json and sarif.json) must be identical, and the golden masters must not move.

Context

The wider gap is separate from this issue. On the same runs, v2.9.1 scans curl in 4.71 s, so current main is +65% and pre-#3923 main is +52%.

  • The biggest movers since v2.9.1:
    • c::Cartography_Mode_B_Braces: 1.73 → 2.63 s;
    • python Cartography_Mode_C_Indentation: 0.14 → 1.68 s;
    • 5_Optical_Detector: 12.4 → 16.4 s.
  • In-process profiling points at _calculate_block_metrics: tiktoken encoding became ~6× costlier per call after the Epic: Refactor calls_out_to extraction to support non-C language paradigms #3264 per-language call-extraction rewrite.
  • Worth its own bisect.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    core-engine
    on Sep 27, 2026
  2. added 2 commits that reference this issue on Sep 27, 2026
  3. squid-protocol commented on Sep 27, 2026

    @squid-protocol
    OwnerAuthor

    Results. The fix is #3931, merged, plus #3939, open. #3939 closes this issue.

    Mechanism, confirmed. A compile trace logged every lazy compile by process during a real crucible scan:

    • Lazy (#3914: compile the language registry's rules on first use, not at import #3923): each forked worker compiled what it met, including objective-c (0.44 s per worker). The language lens scores an ambiguous .h with every claimant's rules, so every worker meets objective-c.
    • With the pre-fork compile: the parent compiles the 55 active languages once (2.2 s), and workers compile 0.012 s in total.

    The metric. phase_ab's wall excludes the import, and #3923 moved about 2 s of compiling out of the import into that window. #3931 adds process, the whole command. Some of the 9% was that shift, not extra work.

    Three-way A/B, final state. This was a 4-core Linux box, so the pool had 3 workers. Each figure is the whole-command time against pre-#3923 main, with an A-vs-A control:

    40 C files (5 rounds) 3,280-file corpus (3 rounds)
    control +0.7% −0.1%
    main with #3923 −29.6% +0.1%
    #3931 ungated (earlier run) −21.3% −0.7%
    #3939 −30.4% +0.0%
    • audit.json and sarif.json are IDENTICAL for every engine on both targets.

    Not measured here: curl. It isn't on this box, and with only 3 workers the dedup win is small. The large-scan benefit rests on your own 12-worker curl result: −0.47 s with a parent pre-compile. The 500-file threshold sits between the measured points: 40 files (lazy is better), 3,280 (neutral to better), and curl's 4,250 (better).

    Still open, as you noted: curl is +65% against v2.9.1, from c::Cartography_Mode_B_Braces, python Mode_C_Indentation and tiktoken in _calculate_block_metrics. That wants its own bisect.


    Generated by Claude Code

  4. added a commit that references this issue on Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-engineperformancepriority: medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions