perf(markdown): cache syntax highlighting results for code blocks#2119
Merged
dgageot merged 1 commit intodocker:mainfrom Mar 16, 2026
Merged
perf(markdown): cache syntax highlighting results for code blocks#2119dgageot merged 1 commit intodocker:mainfrom
dgageot merged 1 commit intodocker:mainfrom
Conversation
During streaming, the FastRenderer re-renders the full accumulated content on every incoming chunk. Code blocks that were already fully received were being re-tokenized through chroma's expensive regex engine on every render, causing O(n²) performance degradation. Add a package-level cache keyed by (lang, code) for syntax highlighting token results. Once a code block has been tokenized, subsequent renders reuse the cached tokens. The cache is cleared on theme changes via ResetStyles(). Streaming benchmark results (BenchmarkStreamingFastRenderer): Time: 3,311ms → ~350ms (~10x faster) Allocs: 26M → ~2.3M (~11x fewer) Memory: 2,497MB → ~906MB (~2.8x less) Assisted-By: docker-agent
gtardif
approved these changes
Mar 16, 2026
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.
During streaming, the FastRenderer re-renders the full accumulated content on every incoming chunk. Code blocks that were already fully received were being re-tokenized through chroma's expensive regex engine on every render, causing O(n²) performance degradation.
Add a package-level cache keyed by (lang, code) for syntax highlighting token results. Once a code block has been tokenized, subsequent renders reuse the cached tokens. The cache is cleared on theme changes via ResetStyles().
Streaming benchmark results (BenchmarkStreamingFastRenderer):
Time: 3,311ms → ~350ms (~10x faster)
Allocs: 26M → ~2.3M (~11x fewer)
Memory: 2,497MB → ~906MB (~2.8x less)
Assisted-By: docker-agent