#1032·ink

Output caches are rebuilt on every render, so each frame re-tokenizes and re-measures the whole screen

Author: Whyyy00Created Sep 24, 2026Updated Sep 24, 2026

Summary

The per-render output caches (OutputCaches, added in #835) do not survive from one frame to the next: Output owns its caches instance, and the renderer creates a new Output on every render. Every frame therefore re-tokenises every line and re-measures every grapheme on screen. For a full-screen UI that repaints at a fixed rate, this dominates CPU.

Two smaller per-frame costs add to it:

  1. Lines that already fit entirely inside a horizontal clip are still passed through the slicing path. In 7.1.1 that is sliceAnsi; on main it is sliceLineToColumns, src/output.ts:301.
  2. render-node-to-output calls widestLine(text) uncached for every text node on every render (src/render-node-to-output.ts:148 on main).

(The unbounded measureText / wrapText caches from #986 are fixed on main, thanks. As of 7.1.1 that fix is not in a release yet. Our data matched that report: in a 300 s run with a 15 fps spectrum, measure-text's map reached 4526 entries / 7.1 MB of keys, about 3.6 MB/min of growth.)

Environment

  • ink 7.1.1 (current latest on npm), react 19.3.0
  • NODE_ENV=production, incrementalRendering: true, maxFps: 60
  • Node.js 22, macOS arm64
  • I also read the current main sources for the lines referenced above. The measurements below are on 7.1.1.

What we measured

The workload is a 120x40 dashboard redrawn at 15 fps: a spectrum drawn as one pre-coloured string of block characters (▁…█) in a single <Text>, a small chart, a status line and two bordered panels. TUI-only benchmark, no other work in the process, production React:

change (applied to 7.1.1) CPU (% of one core)
baseline 41.5 %
one shared OutputCaches across renders (bounded, see below) 39 %
+ return a line unchanged when it already lies inside the clip, and route render-node-to-output's widestLine through the shared per-line width cache 25 %

Output is identical before and after. The CPU profile before the change was dominated by string-width (grapheme segmentation and the ^\p{RGI_Emoji}$ test per character), ANSI tokenising, and slice-ansi, all reached from Output.get() and render-node-to-output.

The patch we are running

  • A single module-level OutputCaches instance shared by every Output. The three maps are bounded with oldest-first eviction (styled chars 512 lines, widths 4096 entries, block widths 512), so text that changes every frame cannot grow them. The cached values are pure functions of the input string, and the per-frame code only reassigns cells in its own buffers, so sharing across frames should be safe.
  • In the horizontal-clip path: if (from === 0 && to >= width) return line;
  • In render-node-to-output: widestLine(text) → sharedCaches.getWidestLine(text)

I'm happy to open a PR against main if this approach looks acceptable, and to adapt it (for example, reusing the quick-lru dependency from #986).