fix(elements): dedupe in-flight code block tokenization - #455
Open
ephraimduncan wants to merge 2 commits into
Open
fix(elements): dedupe in-flight code block tokenization#455ephraimduncan wants to merge 2 commits into
ephraimduncan wants to merge 2 commits into
Conversation
An uncached code/language pair launched two Shiki jobs on first render: CodeBlockContent calls highlightCode from both its render-time memo and its passive effect before the token cache is warm. Track in-flight keys so a cache miss with a pending job only subscribes instead of starting duplicate tokenization. Also evict rejected createHighlighter promises from the highlighter cache so a transient failure no longer makes a language permanently unhighlightable.
Contributor
|
@ephraimduncan 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
The first render of any uncached code/language pair launches two Shiki tokenization jobs for the same input.
CodeBlockContentcallshighlightCodefrom two paths in the same render cycle:highlightCode(code, language) ?? rawTokens)highlightCode(code, language, callback))Before the first job can populate
tokensCache, the effect's cache miss starts a secondgetHighlighter(...).then(codeToTokens). Both executecodeToTokens; the later one overwrites the same cache entry. During streaming, where every chunk is a new uncached string, this doubles the expensive part of the hot path.Fix
pendingHighlightsset keyed by the token cache key. A cache miss with a pending job subscribes the callback and returnsnullinstead of starting duplicate tokenization. The set is cleared on both success and failure so a rejected highlight permits a later retry.getHighlightercached rejectedcreateHighlighterpromises per language forever, making a language permanently unhighlightable after one transient failure. Rejected promises are now evicted fromhighlighterCache.Immediate raw-token rendering while Shiki loads is unchanged, as is the subscriber fan-out.
Verification
Regression test (
describe("highlightCode")incode-block.test.tsx, shiki mocked at the module boundary with a countingcodeToTokensspy):All 3 tests fail against the pre-fix source and pass with the fix; existing
code-blockandsnippetsuites pass;ultracite checkclean on touched files.