Skip to content

perf(cartography): skip provably-no-op per-match work in _slice_by_braces (#3174) - #3179

Merged
squid-protocol merged 2 commits into
mainfrom
perf/3174-mode-b-microopt
Sep 18, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
perf/3174-mode-b-microopt

Conversation

@squid-protocol

@squid-protocol squid-protocol commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

Profiling for #3174 overturned its premise (full diagnostic in the issue): the brace scan (_find_balanced_end) is ~0.5% of the Mode B key — already C-fast — and there's no quadratic re-scan. Mode B's cost is diffuse per-match Python plus per-function block metrics. This PR removes the clearly-wasteful part of the per-match Python.

Change (all output-identical)

Three shavings in the _slice_by_braces per-match loop:

  • Guard the c/cpp member-initializer-list char walk with ":" in args_search_text — its only effect is truncating at a top-level :, a no-op when the signature has none (the common case).
  • Guard the c/cpp #else/#elif preprocessor-branch finditer with "#" in args_search_text — no # ⇒ no match ⇒ no re-slice.
  • Compute block.count("\n") once instead of twice on the (often large) function body.

Each guard skips work that provably cannot change the result, so nothing behavioral moves.

Verification

  • Output identity: full-curl scan audit.json + sarif.json byte-identical to main (via tests/tools/phase_ab.py artifact diff).
  • Tests: 290 detector/extraction/galaxyscope tests green.
  • Isolated effect (cProfile, curl): _slice_by_braces tottime 0.370s → 0.329s (−11% of the per-match Python).

On the size of the win — read the hardware caveat

End-to-end on the dev box this is ~0.2% of Optical_Detector, below that machine's wall-clock noise floor — but that box has many cores and a fast clock, so galaxyscope's per-file parallelism hides per-worker CPU. This change reduces single-threaded, CPU-bound per-match work; on hosts with fewer cores (per-worker CPU sits closer to the wall) or a slower clock, the same −11% translates more directly to wall time. Framed as a low-risk CPU cleanup that helps most exactly where the engine is most constrained, not a headline number on fast hardware.

Closes #3174 (diagnosed: no headline Mode B lever; brace scan ~0.5%; the dominant Optical_Detector cost is coding_analysis, already worked by #3069/#3072).

🤖 Generated with Claude Code


Also in this PR: speed-testing docs

Adds docs/perf_speed_testing.md — how to A/B a hot-path change honestly (phase telemetry, the phase_ab harness, A-vs-A control, deterministic-surface identity) and the multi-hardware perspective this change surfaced: the scan runs one worker per file, so a fast many-core box hides single-threaded per-worker CPU. A reduction that is sub-noise on the wall here can matter on core-constrained / slower hosts, where per-worker CPU sits closer to the wall. Judge per-worker CPU (cProfile tottime / --file-speed phase totals), not just wall on the dev machine — and don't over-claim the mirror image on I/O- or memory-bound hosts either.

…aces (#3174)

Profiling #3174 overturned its premise: the brace scan (`_find_balanced_end`)
is ~0.5% of the Mode B key -- already C-fast -- and there is no quadratic
re-scan. Mode B's cost is diffuse per-match Python plus per-function block
metrics. Three output-identical shavings in the per-match loop:

- Guard the c/cpp member-initializer-list char walk with `":" in
  args_search_text` -- its only effect is truncating at a top-level `:`, a no-op
  when the signature has none (the common case).
- Guard the c/cpp `#else`/`#elif` preprocessor-branch finditer with `"#" in
  args_search_text` -- no `#` means no match and no re-slice.
- Compute `block.count("\n")` once instead of twice on the (often large) body.

cProfile on curl: `_slice_by_braces` tottime 0.370s -> 0.329s (-11% of the
per-match Python). Output-identical: full-curl `audit.json`/`sarif.json` byte-
identical to main via phase_ab, 290 detector/extraction tests green. End-to-end
this is ~0.2% of Optical_Detector -- below the wall-clock noise floor -- so it is
a clean-up, not a headline win; see the issue for the full diagnostic.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

#3174)

Captures how to A/B a scan-hot-path change honestly (phase telemetry, the
phase_ab harness, license key, alternating rounds, A-vs-A control, deterministic
surface) and, new here, the perspective this PR crystallized: the scan
parallelizes one worker per file, so on a fast many-core box the wall clock hides
single-threaded per-worker CPU. A change that is sub-noise on the wall here can
matter on core-constrained or slower hosts, where per-worker CPU sits closer to
the wall -- so judge per-worker CPU (cProfile tottime / --file-speed phase
totals), not just wall on the dev machine, and don't over-claim the mirror image
on I/O- or memory-bound hosts either.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@squid-protocol
squid-protocol merged commit c4f86cb into main Sep 18, 2026
31 checks passed
@squid-protocol
squid-protocol deleted the perf/3174-mode-b-microopt branch September 18, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: optimize the Mode B brace slicer itself — measured as pure slicing cost, NOT tiktoken (~20% of Optical_Detector)

1 participant