Skip to content

The agent that grows with you

perf(desktop): cache short markdown parses too

Brooklyn Nicholsonf64b719321c4
diff --git a/apps/desktop/src/lib/markdown-blocks.ts b/apps/desktop/src/lib/markdown-blocks.ts
index df6a473a274..8aac6a3a614 100644
--- a/apps/desktop/src/lib/markdown-blocks.ts
+++ b/apps/desktop/src/lib/markdown-blocks.ts
@@ -9,8 +9,15 @@ import { parseMarkdownIntoBlocks } from '@assistant-ui/react-streamdown'
* new string, so the stock splitter pays that O(full-text) cost ~30×/s on
* long replies. Two caches remove it:
*
- * 1. Exact-string LRU — a message that REMOUNTS with unchanged text
- * (virtualizer scroll, session switch) reuses its parse outright.
+ * 1. Exact-string cache — the same text always yields the SAME ARRAY. This is
+ * identity, not just cost: `parseMarkdownIntoBlocks` builds a fresh array
+ * every call, and Streamdown mirrors the block list into `useState`, so a
+ * new array identity for unchanged text makes every Streamdown re-render
+ * itself and re-render every Block under it. Short messages used to skip
+ * the cache on the theory that re-lexing them was cheap — the lex is, but
+ * the churn it caused was not (measured: ~105 self-renders of Streamdown
+ * across five idle tiles in six seconds, cascading into 800 Block renders
+ * with nothing streaming). Every length is cached now.
* 2. Streaming-append cache — when the new text starts with a recently parsed
* text (the token-append case), the previous parse's blocks are reused up
* to a settled boundary and only the suffix is lexed. The boundary drops
@@ -28,8 +35,7 @@ import { parseMarkdownIntoBlocks } from '@assistant-ui/react-streamdown'
* full lex, i.e. exactly the previous behavior.
*/
-const EXACT_CACHE_MAX = 64
-const EXACT_CACHE_MIN_LENGTH = 1024
+const EXACT_CACHE_MAX = 256
const exactCache = new Map<string, string[]>()
// Streaming messages grow monotonically, and only a handful stream at once
@@ -109,10 +115,6 @@ function lexIncrementally(text: string): null | string[] {
}
export function parseMarkdownIntoBlocksCached(markdown: string): string[] {
- if (markdown.length < EXACT_CACHE_MIN_LENGTH) {
- return parseMarkdownIntoBlocks(markdown)
- }
-
const hit = exactCache.get(markdown)
if (hit) {