Skip to content

perf(desktop): 200-entry LRU cache for highlight.js output#3117

Open
HUQIANTAO wants to merge 1 commit into
esengine:main-v2from
HUQIANTAO:perf/highlight-lru
Open

perf(desktop): 200-entry LRU cache for highlight.js output#3117
HUQIANTAO wants to merge 1 commit into
esengine:main-v2from
HUQIANTAO:perf/highlight-lru

Conversation

@HUQIANTAO
Copy link
Copy Markdown
Contributor

highlight.highlight() runs a real lexer walk over the source. The same code block re-renders many times in normal use — a streaming update on the next block still re-runs the parent transcript, the React StrictMode double-invoke in dev re-renders every block, and the code-block toolbar's hover-reveal re-renders the block's wrapper.

Add a 200-entry LRU keyed on a djb2 hash of (lang + code). The cache stores the original source alongside the rendered HTML and verifies the source matches before serving — a hash collision returns a cache miss instead of the wrong highlight. Map iteration is insertion-order, so the oldest entry is at the head; eviction is a single delete(). Public API is unchanged.

highlight.highlight() runs a real lexer walk over the source. The same
code block re-renders many times in normal use — a streaming update
on the *next* block still re-runs the parent transcript, the React
StrictMode double-invoke in dev re-renders every block, and the
code-block toolbar's hover-reveal re-renders the block's wrapper.

Add a 200-entry LRU keyed on a djb2 hash of (lang + code). The cache
stores the original source alongside the rendered HTML and verifies
the source matches before serving — a hash collision (vanishingly
unlikely with djb2 on multi-KB code) returns a cache miss instead of
the wrong highlight. Map iteration is insertion-order, so the
oldest entry is at the head; eviction is a single delete().

Size 200 covers the visible viewport (~30 blocks) plus an overshoot
for the transcript's re-render window. Memory is bounded: each entry
is the (code, html) pair, so worst case is ~200 * average-block-size
bytes; in practice 50KB total for a busy transcript.

Public API is unchanged. tsc --noEmit passes.
@github-actions github-actions Bot added the v2 Go rewrite (1.x) — main-v2 branch, active development label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Go rewrite (1.x) — main-v2 branch, active development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant