Skip to content

perf: rotate line slices in Insert/DeleteLineArea full-width fast path - #119

Open
LXXero wants to merge 10 commits into
charmbracelet:mainfrom
LXXero:scroll-fastpath
Open

LXXero wants to merge 10 commits into
charmbracelet:mainfrom
LXXero:scroll-fastpath

Conversation

@LXXero

@LXXero LXXero commented Jun 6, 2026

Copy link
Copy Markdown

Problem

Buffer.DeleteLineArea (the screen-scroll path — every newline at the bottom of the scroll region) shifts cells one struct assignment at a time: O(rows×cols) Cell copies per scrolled line. Profiling a real terminal emulator (xerotty) flooding seq 1 2000000 into an 80×24 screen, this single function + the memmoves it generates was ~80% of total process CPU (perf, Linux). At ~100 bytes per Cell that's hundreds of gigabytes moved to scroll two million lines.

Change

  • Full-width areas (the overwhelmingly common case — whole-screen scroll regions): rotate the line slice headers instead of copying cells — O(rows) slice-header moves, with the displaced lines' storage recycled as the cleared lines. Same trick for InsertLineArea (scroll down).
  • Cleared lines fill by direct assignment — Line.Set's wide-cell repair has nothing to repair when every cell in the line is replaced.
  • Partial-width areas (DECSLRM margins): keep the shift semantics but use copy() per row instead of per-cell assignment.

Results

End-to-end seq 1 2000000 wall time in xerotty: 31s → 13.5s from this change alone (→ ~6–8s combined with a scrollback ring buffer in x/vt, PR incoming there).

Existing tests pass; behavior is identical — lines land in the same places with the same contents, only the storage shuffling changed.

Scrolling (DeleteLineArea at the top of the scroll region) shifted
cells one by one — O(rows*cols) Cell struct copies per scrolled
line. During bulk output every newline pays this, and it profiled
at ~80% of total process CPU in a real terminal emulator (xerotty)
flooding 2M lines.

Full-width areas (the common case — whole-screen scroll regions)
now rotate the line slice headers instead: O(rows) slice-header
moves plus clearing the n recycled lines, reusing their storage.
Cleared lines are filled by direct assignment — Line.Set's
wide-cell repair has nothing to repair when every cell is replaced.
Partial-width areas keep the shift but use copy() per row instead
of per-cell assignment.

Measured end-to-end (seq 1 2000000 into an 80x24 emulator): 31s ->
13.5s wall just from this change; combined with a scrollback ring
buffer in x/vt it reaches ~6-8s.
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>
LXXero and others added 9 commits September 10, 2026 19:32
InsertLineArea and DeleteLineArea indexed Lines by the caller's
rectangle without checking it against the buffer. A terminal emulator
handing in a scroll region that outlived a shrink (DECSTBM arriving
before the app's SIGWINCH) made that an index-out-of-range panic in
the middle of PTY output. Intersect the region with the buffer bounds
first; clamping rather than returning keeps the in-bounds part of the
region scrolling.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014zjoZEYPz9aD6X59gArQnj
Brings in upstream through 8786532: renderer resize/repaint and inline
frame fixes, the reworked touch-list bookkeeping (13b333e, 6d7982b,
7c6028e, 2e490d9, 4117f7c), conformance fuzzing, poll reader and event
loop fixes, and the go 1.26 bump.

The merge applied with no conflicts. buffer.go auto-merged: the fork's
full-width line-rotation fast path (840b40d) and stale-region clamp
(efd5e1f) are kept as they were. The clamp is still needed, because
upstream InsertLineArea/DeleteLineArea only check y >= Height(), not
the region against the buffer.

Touch bookkeeping with the fast path: RenderBuffer.InsertLineArea and
DeleteLineArea still touch every row of the region through TouchLine,
which upstream now bounds-checks against len(Lines) and backs with
growTouched. After a render, resetTouched leaves {-1,-1} records
instead of nil. A later TouchLine then sets LastCell to the region
width, which passes Render's repaint test. The renderer reads the
touch list by screen row only, so rotating line slices does not
affect it.

New tests in buffer_area_test.go:
- TestRenderBufferShrinkUnderScrollRegion: shrinks a RenderBuffer below
  the bottom margin of a scroll region, then deletes and inserts lines
  in that region on the fast path, the fast path after a width shrink,
  and the margin (shift) path. Checks contents, that every remaining
  region row is touched, and that no touched row is past the buffer.
  With the clamp removed, it panics with index out of range [7] with
  length 5.
- TestLineAreaOpsStaleRegion: Buffer insert/delete with a region left
  over from a larger size, including counts past the bottom margin,
  left/right margins, and a cursor row below the new bottom.
- TestBulkNewlineFastPath: 28 lines of newline batches (1 to 7 per
  write, 7 being more than the region holds) scrolled through a
  full-width region below a fixed status row, rendered through
  TerminalRenderer after each batch. Each scroll touches exactly the
  region rows, at full width. The renderer's buffer matches the frame
  after every render, and no two rows share storage.

ultraviolet: go build, go vet, go test ./... pass (go1.27.1); go test
-race on the root package passes; examples build. internal/conformance
was not run: it needs the native libghostty-vt library, which is not
installed here (upstream CI builds it).

Dependents, built and tested in a clone with go.mod replace pointed at
this merge:
- xerotty (7f3fb13): go build ./... ok; go vet ./... ok; go test ./...
  ok on the second run (21 packages ok, 12 with no test files). The
  first run failed TestSnapshotScrollbackRangeWire ("window starts at
  0; cap not taking effect") in internal/daemonsource. That test is
  flaky and the flake is not caused by this merge: it failed 3 of 8
  isolated runs against the merge and 3 of 8 against xerotty's current
  pinned fork (efd5e1f).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Brings in 6b8d4ba "feat: OSC 7501 helpers (charmbracelet#207)". The merge was clean;
no conflicts, so every fork change on scroll-fastpath is kept as-is.

ultraviolet: go build ./... ok; go test ./... ok (all 5 packages pass).

xerotty (shared clone, replace github.com/charmbracelet/ultraviolet => this
worktree): go build ./... ok; go test ./... ok (22 packages pass, 0 fail).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Upstream 6b8d4ba bumped the root go.mod to github.com/charmbracelet/x/ansi
v0.11.9 but left examples/ and internal/conformance/ on the older
v0.11.9-0.20261008082244-a4a2f9b961a0 pre-release. Both replace
ultraviolet with ../, so the root's higher requirement made
`go test ./...` in examples/ fail with "updates to go.mod needed".
Ran go mod tidy in both modules.

examples: go build ./... ok; go test ./... ok.
root: go build ./... ok; go test ./... ok.
internal/conformance: still cannot build here, because the cgo package
go.mitchellh.com/libghostty needs libghostty-vt-static via pkg-config,
which is not installed on this machine. That has nothing to do with this
change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Brings in a45b7c8 "go: mody tidy to clear outdated pins". It bumps
examples/ and internal/conformance/ to github.com/charmbracelet/x/ansi
v0.11.9, the same change the fork already made in 00bd1c1, so the merge
had no conflicts and leaves the tree unchanged. It records the upstream
commit so the fork is no longer behind.

ultraviolet: go build ./... ok; go test ./... ok (root and examples).
xerotty (f81688d, replace pointed at this merge): go build ./... ok;
go test ./... ok, all packages pass.

Co-Authored-By: Claude Opus 5.5 (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