perf(vt): ring-buffer Scrollback eviction + cheap trailing-blank scan#888
Open
LXXero wants to merge 1 commit into
Open
perf(vt): ring-buffer Scrollback eviction + cheap trailing-blank scan#888LXXero wants to merge 1 commit into
LXXero wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Scrollback.Pushruns for every line of output once the screen is full, and it had two per-line costs that dominate bulk-output profiles: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) floodingseq 1 2000000(perf, Linux).Cell.Equal, whoseStylecomparison unwraps threecolor.Colorinterfaces per cell — paid for every trailing blank of every pushed line.Change
headindexes 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.Equalcheck.Results
Combined with the companion line-rotation PR in ultraviolet (charmbracelet/ultraviolet#119):
seq 1 2000000into 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).