Skip to content

perf: the remaining speed residuals are structural — optimize the shared coding_analysis rule pass, not individual repos #3069

Closed

Description

@squid-protocol

Summary

After the outlier-driven perf round (residual = actual / rate-model-predicted, via gitgalaxy-raw-output/tools/rank_outliers.py), the single-hot-path outliers are largely fixed — ghostty (ReDoS), merge-node-client (import-graph resolution, #3062). The remaining top residuals are not single bugs; they are all the same structural cost, so chasing them one repo at a time has diminishing returns. This issue proposes attacking the shared cost instead.

Evidence (this round's investigations)

repo residual verdict
merge-node-client 14× fixed — O(candidates×pathlib) import resolution (#3062)
nx (TS) 4.1× structural — per-file work; graph resolves in 0.5s
node-linux-x64 3.0× structural — 2,724 headerless .h, language detection-bound; no safe win (a detection-content cap broke cross-language crucible parity)
sokol-zig 2.0× structural — vendored large C headers; one latent quadratic removed (#3068)

In every structural case the dominant CPU cost is detector.py::coding_analysis running each language's full rule set as pattern.finditer(seg_code) over the whole segment (≈40 rules for C, more for TS), plus the per-hit spatial-map/offset bookkeeping. On big files this is the bottleneck; it scales with files × rules × filesize and is legitimate work (no bug), which is why per-repo fixes barely move it.

Proposed work (the actual lever)

Optimize the shared coding_analysis rule pass so the whole corpus benefits:

  1. Cheap content pre-filter per rule. Many rules can't match a segment lacking a required literal/keyword. A substring in seg_code (or a fast prefilter set) gate before finditer would skip a large fraction of the ~40 regexes on most files. (Same idea as the '=' in line var-decl pre-filter, perf(detector): skip =-less lines before the var-decl backtracking regex #3063, generalized.) Must preserve exact match sets — validate against the golden crucible.
  2. Combined/alternation scan. Investigate a single combined pass (one automaton) over the segment instead of N independent finditer calls, or grouping rules that share anchors. Higher risk to parity; needs careful per-rule attribution.
  3. Right-size the work to the file. Confirm segments/streams aren't scanned more than once and that large files don't re-run identical passes.

Target: reduce the per-file coding_analysis cost that dominates nx/node/sokol-zig and every other large repo, rather than one outlier.

Methodology note (save the next person a day)

A thread-based statistical sampler systematically over-attributes GIL-releasing work — syscalls (iterdir/stat), tiktoken.encode, and re calls all release the GIL, so the sampler thread over-counts the Python frame parked at that call. It falsely fingered tiktoken (~85%/34% vs ~1% real) and a directory census (77% vs 1.2% real) in this round. Use cProfile for CPU-bound Python attribution; treat the sampler as reliable only for pure-Python loops.

References

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions