Skip to content

perf(vt): ring-buffer Scrollback eviction + cheap trailing-blank scan#888

Open
LXXero wants to merge 1 commit into
charmbracelet:mainfrom
LXXero:vt-scrollback-ring
Open

perf(vt): ring-buffer Scrollback eviction + cheap trailing-blank scan#888
LXXero wants to merge 1 commit into
charmbracelet:mainfrom
LXXero:vt-scrollback-ring

Conversation

@LXXero

@LXXero LXXero commented Jun 6, 2026

Copy link
Copy Markdown

Problem

Scrollback.Push runs for every line of output once the screen is full, and it had two per-line costs that dominate bulk-output profiles:

  1. Eviction via slices.Delete(s.lines, 0, 1) — once at capacity, every push memmoves the entire backing array. At the default 10,000-line cap that's ~240KB moved per line of output. Profiled at ~47% of total process CPU in a real emulator (xerotty) flooding seq 1 2000000 (perf, Linux).
  2. The trailing-blank trim scans with Cell.Equal, whose Style comparison unwraps three color.Color interfaces per cell — paid for every trailing blank of every pushed line.

Change

  • Storage becomes a ring once full: head indexes the oldest line, pushes overwrite in place — O(1) eviction. Public API unchanged: Line(i) stays allocation-free (one mod); Lines() linearizes (allocating) only when the ring has wrapped, documented.
  • The blank scan fast-paths structurally-blank cells (zero value or plain space) with whole-struct compares — one type-generated equality per cell; styled cells still fall through to the full Equal check.

Results

Combined with the companion line-rotation PR in ultraviolet (charmbracelet/ultraviolet#119): seq 1 2000000 into an 80×24 emulator went 31s → ~6–8s wall in xerotty (this PR's share: 13.5s → ~8s).

Existing vt tests pass. Related: #887 (same per-frame-cost theme, different layer).

Two hot spots in Scrollback.Push — which runs for EVERY line of
output once the screen is full:

- At capacity, evicting the oldest line used slices.Delete: an
  O(maxLines) memmove of the whole backing array per push. At the
  default 10k cap that's ~240KB moved per line of output; profiled
  at ~47% of process CPU during a 2M-line flood in a real emulator.
  Storage is now a ring: head indexes the oldest line and pushes
  overwrite in place. Public API unchanged; Lines() linearizes
  (allocating) only when the ring has wrapped — Line(i) remains
  allocation-free.

- The trailing-blank trim scanned with Cell.Equal, whose Style
  comparison unwraps three color.Color interfaces per cell. Blank
  cells are structurally zero (or a plain space) — a whole-struct
  equality check handles virtually all of them with one
  type-generated compare; oddly-styled cells still fall through to
  the full check.

Measured (with the companion ultraviolet line-rotation PR): seq 1
2000000 into an 80x24 emulator went 13.5s -> ~6-8s wall.
LXXero added a commit to LXXero/xerotty that referenced this pull request Jun 6, 2026
seq 1 2000000 into an 80x24 window took ~31s — 15-25x behind
ghostty (~1.5s) and xfce4-terminal (~2.7s). perf blamed upstream,
layer by layer:

- ultraviolet Buffer.DeleteLineArea shifted cells one struct
  assignment at a time on every scroll (~80% of CPU)
- vt Scrollback.Push evicted via slices.Delete — an O(10k) memmove
  per line of output at the default cap (~47% of the remainder)
- the trailing-blank trim paid interface-unwrapping color compares
  per cell, and whole-line clears ran wide-cell repair per cell

Fixes are upstream PRs (charmbracelet/ultraviolet#119,
charmbracelet/x#888; see also #887) — go.mod pins both modules to
the LXXero fork commits carrying them until they merge, then the
replaces drop. Result: ~6-10s wall, 3-5x faster, within ~3x of the
purpose-built terminals from ~20x behind.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant