fix: quadratic code-block scan in isInsideCodeBlock - #574
Open
Vikramb1 wants to merge 1 commit into
Open
Conversation
Contributor
|
@Vikramb1 is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
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.
Problem
isInsideCodeBlock(text, position)rescans the entire prefix[0, position)on every call. Several handlers probe it once per candidate character — the link/image handler calls it for every[while walking backwards — so total work is O(brackets × length).The pathological case is exactly what streaming AI produces: a long, still-unclosed code fence full of brackets, re-repaired on every arriving token. Repro (58k chars):
That's ~915ms of main-thread time per streamed chunk when rendering a large code block.
Fix
Build the answer for every position in one linear pass (
buildCodeBlockLookup, mirroring the original scan's control flow including escaped-backtick and triple-backtick handling), and cache it per text with a single-entry cache — handlers repeatedly probe the same string within oneremend()call, so each probe becomes O(1) after one O(n) build. No signature changes; all six consuming handlers benefit.Same repro after the fix: ~0.4ms (benchmark added,
iterations: 10to keep CI fast).Behavior parity
__tests__/code-block-utils.test.tsincludes a verbatim copy of the previous implementation as a reference oracle and asserts equality at every position across mixed inputs (fences, inline code, escaped backticks, unclosed spans), plus a cache-correctness test with alternating texts.text.lengthresolve to the end-of-scan state, matching the old loop's behavior.Notes
pnpm check/check-typesclean.pnpm buildsucceeds for all library packages (apps/testprerender needs AI Gateway credentials I don't have as an external contributor).