Skip to content

perf(#3182): extend the _is_orphan bisect fast path to hyphenated names - #3184

Merged
squid-protocol merged 1 commit into
mainfrom
perf/3182-optical-detector
Sep 18, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
perf/3182-optical-detector

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Partial progress on #3182. Honest framing up front: this is a latent-quadratic bound, not a measured corpus speedup.

What

_is_orphan ("does this name occur outside its own span?") answers via an O(log n) bisect over a precomputed occurrence index — but the index only covered single \w+ tokens. Hyphenated identifier names (COBOL paragraphs, Lisp/Scheme, the column_sensitive family) failed that guard and fell to an O(filesize) per-function (?<!\w)name(?!\w) rescan — the same O(functions × filesize) quadratic the \w+ fast path exists to prevent (documented at ~10s on a 1MB, ~900-function header).

This indexes the segment-aligned hyphenated sub-runs of every [\w-]+ token so those names bisect too. Segment alignment makes occ_index[name] exactly the set of (?<!\w)name(?!\w) match starts — byte-identical to the fallback, including the boundary case where a shorter paragraph name matches inside a longer hyphenated token (CALC inside CALC-TOTAL). Single-\w+ names keep the existing code path untouched.

Why it's flat (and why I'm landing it anyway)

I profiled a COBOL-heavy scan under cProfile and _is_orphan showed as ~20% of extraction — which is how I found this. But the real-harness A/B (phase_ab.py --rounds 3 --control on curl + all_language_repo) is flat: wall within the A-vs-A control noise floor, Optical Detector cumulative CPU unchanged. cProfile over-weights _is_orphan ~50× because its real cost is C-level regex scanning. The A/B is ground truth.

So this bounds the quadratic for a large single hyphen-name file (where it would genuinely bite), but does not move the mixed corpus. Same character as #3175 — a worst-case bound, landed for robustness, not a headline number.

Verification

  • function_data (incl. usage_status / sec_dead_code) byte-identical vs baseline across all 11,530 COBOL-corpus functions and all 43,084 all_language_repo functions.
  • audit.json + sarif.json byte-identical on both repos.
  • New regression test (tests/core_engine/test_orphan_hyphen_fast_path_3182.py): runs a hyphen-overlap COBOL fixture through the fast path and the monkeypatched \w+-only fallback and asserts the census agrees.
  • Existing orphan/COBOL suites green (test_unreferenced_by_name_contract_2806, cobol_mainframe/, line-gate tests).

Note for #3182 scope

The issue assumed the Optical Detector's COBOL cost could be gated like Security Lens (#3069/#3072/#3173). It can't: see the issue comment I'm posting — func_start (the dominant real cost, in both the func_start rule and _slice_by_labels) has no required literal (it anchors on column position + any identifier) and isn't line-local, so it's ungate-able by the literal-prefilter machinery. The only remaining levers are a parity-critical regex rewrite or trimming the _slice_by_labels per-function loop — deferred pending a decision on ROI.

🤖 Generated with Claude Code

_is_orphan asks "does this name occur outside its own span?" via an O(log n)
bisect over a precomputed occurrence index -- but the index only covered single
\w+ tokens, so hyphenated identifier names (COBOL paragraphs, Lisp/Scheme, the
column_sensitive family) fell to an O(filesize) per-function boundary-regex
rescan (the same O(functions x filesize) quadratic the \w+ fast path exists to
prevent -- documented at ~10s on a 1MB, ~900-function header).

Index the segment-aligned hyphenated sub-runs of every [\w-]+ token so those
names bisect too. Segment alignment makes occ_index[name] exactly the set of
(?<!\w)name(?!\w) match starts, so it is byte-identical to the fallback,
including the boundary case where a shorter paragraph name matches inside a
longer hyphenated token (CALC inside CALC-TOTAL). Single-\w+ names keep the
existing code path untouched.

Scope/honesty: this bounds a latent quadratic for LARGE single hyphen-name
files; it is flat on the current corpus (phase_ab A/B on curl + all_language_repo:
wall within the A-vs-A control noise floor). cProfile over-weights _is_orphan
~50x because its real cost is C-level regex scanning -- the flat A/B is ground
truth. Landing it as a correctness/robustness bound, not a measured speedup.

Verified: function_data (incl. usage_status/sec_dead_code) byte-identical vs
baseline across all 11,530 COBOL-corpus functions and all 43,084 all_language_repo
functions; audit.json + sarif.json byte-identical both repos. New regression test
runs a hyphen-overlap COBOL fixture through the fast path and the monkeypatched
fallback and asserts the census agrees.

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

@squid-protocol
squid-protocol merged commit 10e9e4d into main Sep 18, 2026
31 checks passed
@squid-protocol
squid-protocol deleted the perf/3182-optical-detector branch September 18, 2026 18:02
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.

1 participant