Skip to content

vt: reuse scrollback line buffers#822

Open
nmelo wants to merge 1 commit into
charmbracelet:mainfrom
nmelo:scrollback-buffer-pooling
Open

vt: reuse scrollback line buffers#822
nmelo wants to merge 1 commit into
charmbracelet:mainfrom
nmelo:scrollback-buffer-pooling

Conversation

@nmelo

@nmelo nmelo commented Apr 2, 2026

Copy link
Copy Markdown

Summary

Reuse scrollback line buffers instead of allocating a fresh slice on every Scrollback.Push.

This keeps the current copy semantics, but once scrollback is full it reuses the evicted line buffer when capacity fits and falls back to a sync.Pool when it does not.

Closes #821.

What changed

  • replace slices.Clone-on-every-push with buffer reuse in vt/scrollback.go
  • recycle dropped buffers from SetMaxLines and Clear
  • add tests covering source-line alias safety and evicted-buffer reuse

Why

I profiled this through initech, which embeds x/vt for multiple panes.
Using the same 11-pane synthetic workload before and after this patch:

inuse_space

  • before: 320.14 MB total heap
  • after: 236.02 MB total heap
  • delta: -84.12 MB (-26.3%)

alloc_space

  • before: 3237.93 MB cumulative heap allocations
  • after: 243.97 MB
  • delta: -2993.96 MB (-92.5%)

The dominant hotspot moved from:

  • before: slices.Clone at 3064.29 MB alloc_space (94.64%)
  • after: Scrollback.acquireLine at 170.39 MB alloc_space (69.84%)

Validation

  • go test ./... in vt
  • go test ./internal/tui ./cmd in initech with a local replace to this fork
  • make test in initech

wblech added a commit to wblech/x that referenced this pull request Apr 17, 2026
Cherry-picked from charmbracelet/x PR charmbracelet#822 (nmelo:scrollback-buffer-pooling).
Reduces allocations when scrollback is full by reusing evicted line buffers
via sync.Pool.
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.

vt: reduce per-line allocation churn in Scrollback.Push

1 participant