Skip to content

Skip the inline-code mask when every backtick is inside a fence - #9991

Merged
danielhanchen merged 1 commit into
mainfrom
perf/latex-inline-code-region-early-out
Aug 30, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
perf/latex-inline-code-region-early-out

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Recovers most of a streaming performance regression that Frontend CI has been failing on intermittently.

What the test was telling us

a reply with math streams near the cost of one without in streaming-latex-retro-edit.test.ts has been failing on and off in Frontend build + bundle sanity and Frontend unit tests (Windows):

math reply cost 4.2x the plain reply (math 510 ms, plain 122 ms)

It reads as a timing flake, so I checked before touching the threshold. It is not a flake. On an idle host the ratio is a stable 3.3x, with under 2 percent variance across runs. The test's own comment says it should be 1.1x to 1.2x.

Bisected across the 191 commits touching studio/frontend/src, first bad commit e584ffc4 (#9796, escaped inline math in lists). Measured either side of it on the same host, same Node, same node_modules:

before #9796 after
ratio the test measures 1.5x 3.3x
preprocessLaTeX 55 ms 166 ms
stabilizeStreamingMarkdown 3 ms 3 ms
cache.update 42 ms 39 ms

All of it is in preprocessLaTeX. A CPU profile puts 29 percent of total runtime in findInlineCodeRegions, which #9796 added, and which findCodeBlockRegions now calls. preprocessLaTeX runs findCodeBlockRegions twice per frame, once inside convertLatexDelimiters and once for currency escaping, at 66 ms per pass out of 169 ms total.

The change

findInlineCodeRegions masks every block region to spaces and rescans the result. When no backtick sits outside a block region the mask cannot contain one, so the scan can only return nothing and the whole rebuild is spent to learn that.

The check is exact rather than a heuristic. The mask replaces every non-line-ending character inside a block region with a space, so with no backtick outside one the masked string holds no backtick at all, codeSpans returns an empty list and crossesBlock is false. Returning early is the same answer by a shorter route.

It matters because the streaming path calls this once per frame over a growing prefix, so the mask is O(n) per frame and O(n^2) over a reply, and a reply with one fenced example and no inline code is the ordinary shape.

Result

On the fixture that test uses, 300 units and 23,000 characters over 958 frames:

  • math arm 217 ms to 136 ms
  • ratio 3.35x to 2.11x

That clears the threshold of 4 with real headroom on slower CI hardware. It does not fully close the gap to the 1.5x before #9796; the rest is the second findCodeBlockRegions pass, which cannot be reused as is because conversion shifts offsets and can change indented-code detection. Left alone here rather than guessed at.

Verification

  • Full frontend suite: 5976 tests, 5976 pass, 0 fail
  • The new test pins both directions, because an early return that fired too eagerly would look like a pure speedup. Currency in prose beside a fence is still escaped, currency inside the fence is still left alone, and an inline span before or after a fence still protects its own currency.
  • Confirmed the new test fails if the early return is made unconditional: `cost $5` comes back as `cost \$5`.

Note on the threshold

I deliberately did not touch the threshold of 4. It is what guards the whole-prefix rebuild that measured 9.8x, which is the thing the test exists for, and moving it would have hidden this instead of fixing it.

findInlineCodeRegions masks every block region to spaces and rescans the result
for code spans. When no backtick sits outside a block region that mask cannot
contain one, so the scan can only return nothing, and the rebuild is spent to
learn that.

It is not a cheap thing to spend. The streaming path calls this once per frame
over a growing prefix, and preprocessLaTeX calls findCodeBlockRegions twice per
frame, so the mask is O(n) per frame and O(n^2) over a reply. A reply carrying
one fenced example and no inline code, where every backtick is inside the fence,
is the ordinary shape rather than a corner case.

Checking for a backtick outside the block regions first is exact, not an
approximation: the mask replaces every non-line-ending character inside a block
region with a space, so if no backtick is outside one the masked string holds no
backtick at all, codeSpans returns an empty list and crossesBlock is false.
Returning early is the same answer by a shorter route.

On the streaming-latex-retro-edit fixture, 300 units and 23,000 characters over
958 frames, the math arm goes from 217ms to 136ms and the ratio that test
measures from 3.35x to 2.11x. For reference the same fixture measured 1.5x
before #9796 and 3.3x after, so this recovers most but not all of that.

The new test pins both directions, since an early return that fires too eagerly
would look like a pure win: currency in prose next to a fence is still escaped,
currency inside the fence is still left alone, and an inline span before or
after a fence still protects its own currency. Verified it fails if the early
return is made unconditional.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-08-30T03:36:46.913389Z 4ee42d3 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen
danielhanchen merged commit ecea20a into main Aug 30, 2026
34 of 41 checks passed
@danielhanchen
danielhanchen deleted the perf/latex-inline-code-region-early-out branch August 30, 2026 03:40
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.

2 participants