Skip to content

perf(markdown): cache syntax highlighting results for code blocks#2119

Merged
dgageot merged 1 commit intodocker:mainfrom
dgageot:perf-tui-3
Mar 16, 2026
Merged

perf(markdown): cache syntax highlighting results for code blocks#2119
dgageot merged 1 commit intodocker:mainfrom
dgageot:perf-tui-3

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented Mar 16, 2026

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

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
@dgageot dgageot requested a review from a team as a code owner March 16, 2026 11:01
docker-agent[bot]

This comment was marked as outdated.

@dgageot dgageot merged commit 4f6407e into docker:main Mar 16, 2026
8 checks passed
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