Repository navigation
Skip the inline-code mask when every backtick is inside a fence - #9991
Merged
Merged
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recovers most of a streaming performance regression that
Frontend CIhas been failing on intermittently.What the test was telling us
a reply with math streams near the cost of one withoutinstreaming-latex-retro-edit.test.tshas been failing on and off inFrontend build + bundle sanityandFrontend unit tests (Windows):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 commite584ffc4(#9796, escaped inline math in lists). Measured either side of it on the same host, same Node, samenode_modules:preprocessLaTeXstabilizeStreamingMarkdowncache.updateAll of it is in
preprocessLaTeX. A CPU profile puts 29 percent of total runtime infindInlineCodeRegions, which #9796 added, and whichfindCodeBlockRegionsnow calls.preprocessLaTeXrunsfindCodeBlockRegionstwice per frame, once insideconvertLatexDelimitersand once for currency escaping, at 66 ms per pass out of 169 ms total.The change
findInlineCodeRegionsmasks 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,
codeSpansreturns an empty list andcrossesBlockis 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:
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
findCodeBlockRegionspass, which cannot be reused as is because conversion shifts offsets and can change indented-code detection. Left alone here rather than guessed at.Verification
`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.