vt: reuse scrollback line buffers#822
Open
nmelo wants to merge 1 commit into
Open
Conversation
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.
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.
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.Poolwhen it does not.Closes #821.
What changed
slices.Clone-on-every-push with buffer reuse invt/scrollback.goSetMaxLinesandClearWhy
I profiled this through
initech, which embedsx/vtfor multiple panes.Using the same 11-pane synthetic workload before and after this patch:
inuse_space
320.14 MBtotal heap236.02 MBtotal heap-84.12 MB(-26.3%)alloc_space
3237.93 MBcumulative heap allocations243.97 MB-2993.96 MB(-92.5%)The dominant hotspot moved from:
slices.Cloneat3064.29 MBalloc_space (94.64%)Scrollback.acquireLineat170.39 MBalloc_space (69.84%)Validation
go test ./...invtgo test ./internal/tui ./cmdininitechwith a local replace to this forkmake testininitech