Repository navigation
perf(studio): remove quadratic region scan in LaTeX preprocessing - #7538
Conversation
findCodeBlockRegions scanned every region found so far for each inline code match, and accepted inline spans were appended to the same array, making it quadratic in the number of inline spans. preprocessLaTeX runs on the full message text every animation frame while streaming and calls it twice. Fenced and inline matches are both ascending and non-overlapping, so walk the fenced list with a cursor instead. Only fenced regions can contain an inline span, so previously accepted inline regions never needed checking. 34,670 chars with 2,100 inline spans: 5.51ms per call to 0.12ms.
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Independently verified. No findings; safe to merge as is. Equivalence. Two harnesses compared the merge-base and PR revisions of Three specifics that looked like they could bite, and did not:
Mutation testing confirms the corpus discriminates rather than passing blindly: removing the lower bound, removing the upper bound, and freezing the cursor were all killed. Performance. The old growth is about 3.9x per doubling of inline spans, the new about 2.0x. At 8000 spans, 880.7 ms to 1.0 ms. Worth noting for the description: Consumers. Below bar, pre-existing, and explicitly not for this PR:
The red |
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Problem
findCodeBlockRegionsinstudio/frontend/src/lib/latex.tscollects fenced regions first, then walks inline code spans. For each inline match it scanned every region found so far to decide whether that match sits inside a fence. Accepted inline spans are appended to the same array, so the scan grows with each match, making it quadratic in the number of inline spans.This is on a hot path.
preprocessLaTeXruns over the full message text on every animation frame while a response is streaming, and callsfindCodeBlockRegionstwice per call, so it shows up on code heavy answers.Fix
Fenced matches and inline matches are both produced by
RegExp.execin ascending, non overlapping order, so the fenced list can be walked with a cursor instead of rescanned per match. Only a fenced region can contain an inline span, so previously accepted inline regions never needed to be checked at all.Results
34,670 chars containing 2,100 inline spans, averaged over 40 calls:
In the app this is a secondary cost, so the end to end gain is smaller than the microbenchmark: on a stream of that text, dropped frames went from 167 to 76. The remaining cost there is a character scan inside Streamdown, not ours.
Correctness
Output is unchanged. A 4,000 case fuzz over fenced blocks, tilde fences, unclosed fences, inline spans inside and outside fences, adjacent backticks, tables and currency text produced identical region lists from the old and new implementations, with zero mismatches.
Typecheck and build pass.