Skip to content

perf(#3182): linear JS/TS func_start start anchor on blank runs (nx wall -73%) - #3335

Merged
squid-protocol merged 1 commit into
mainfrom
perf/3182-next
Sep 23, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
perf/3182-next

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Part of #3182. Next fleet hotspot after #3317: typescript::Cartography_Mode_B_Braces (539s fleet CPU in v2.9.1), led by nx at 90 s/MLOC versus ~10 for grafana/sentry.

Root cause

The assignment branch of JS/TS func_start began (?:^|(?<=[^<>(,\s]))[ \t\n]* under re.M. _build_brace_safe_stream blanks a multi-line template literal to same-length whitespace, and the bare ^ fired on every line of the blank run. Each attempt scanned to the run's end before failing, which is quadratic: 4× per doubling, about 5.5s for 4,000 blank lines. nx's lock-file/__fixtures__/*.ts files (one 16k-line template each) cost 13–60s apiece, and the worst hit the 60s fuse. A sweep of every language's rules found this shape only in JS and TS func_start at fleet-relevant cost.

Fix

Inside a whitespace run, \b can only hold at the run's end, so every start in one run gives the same result. ^ is now taken only:

  • on a non-blank line, or
  • on a blank run's first line start (the previous line ends in a non-space), or
  • at the file start.

These anchors are mutually exclusive. An earlier non-exclusive draft re-ran the failed continuation through a second anchor and was slower on a 1.3MB single-line file.

Evidence

main this PR A-vs-A control
nx wall 70.8s 19.4s (−72.6%) −0.1%
nx 5_Optical_Detector 320.5s 70.5s (−78.0%) +0.9%
nx Cartography_Mode_B 90.5s 21.1s noisy (±16%)
vscode Cartography_Mode_B 65.3s 62.4s (−4.3%) 0.0%
vscode wall 44.5s 43.9s −1.2% (noise)
  • audit.json and sarif are identical on both repos. On nx, every function_data row (name, start_line, loc, complexity, args) is also identical.
  • Match-set parity across 58 JS/TS repos (~245k files, raw and brace-safe streams): identical except 21 matches in 20 files. In each of those, the OLD match started on a blank line after a line ending in <>(, plus trailing blanked text (a string or comment). The new match starts on the declaration's own line, with the same end and the same groups. Every one was machine-checked for that shape. This is a small line-attribution improvement: the old start sat 1–7 lines early.
  • Golden crucible (full leg) passes unchanged. Locally, main and this branch both fail it with a cold .gitgalaxy dependency cache and pass warm, so that flake is pre-existing and environmental.
  • ruff, mypy and dead-key audits are clean. The JS/TS and extraction suites pass (8,574).

New test tests/extraction/languages/test_func_start_blank_run_3182.py:

  • a ReDoS guard (old anchor 5.5s, new 0.03s on the payload);
  • an old-versus-new equivalence sweep over prefix × whitespace × suffix combinations, which allows only the documented shift.

Not in scope

🤖 Generated with Claude Code

…all -73%)

The assignment branch of javascript/typescript func_start started with
`(?:^|(?<=[^<>(,\s]))[ \t\n]*` under re.M. `_build_brace_safe_stream` blanks a
multi-line template literal to same-length whitespace, and the bare `^` fired
on every line of that blank run; each attempt's `[ \t\n]*` scanned to the run's
end before failing -- quadratic in the run. nx's lock-file fixtures (one 16k-line
template each) cost 13-60s apiece in Cartography_Mode_B_Braces, the worst
hitting the 60s fuse.

Inside a whitespace run `\b` can only hold at the run's end, so every start in
one run shares one outcome. `^` is now taken only on a non-blank line, on a
blank run's first line start (previous line ends in a non-space), or at the
file start -- three mutually exclusive anchors, so a failed continuation is
never re-run through a second one.

nx, phase_ab 2 rounds + A-vs-A control (control <1% on wall/Optical):
  wall 70.8s -> 19.4s (-72.6%); 5_Optical_Detector 320.5s -> 70.5s (-78.0%);
  Cartography_Mode_B 90.5s -> 21.1s. audit.json + sarif identical; every
  function_data row (name, start_line, loc, complexity, args) identical.
vscode: Cartography_Mode_B -4.3% (control 0.0%), wall within noise, identical.

Match-set parity on 58 JS/TS repos (~245k files, raw + brace-safe streams):
identical except 21 matches in 20 files whose OLD start sat on a blank line
after a line ending `<>(,` + trailing blanked text; those now start on the
declaration's own line (same end, same groups). Golden crucible (full) passes
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 34f6a3e into main Sep 23, 2026
33 checks passed
@squid-protocol
squid-protocol deleted the perf/3182-next branch September 23, 2026 12:58
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