Skip to content

perf: scroll-optimised flush() in cursedRenderer#1725

Open
luispabon wants to merge 2 commits into
charmbracelet:mainfrom
luispabon:perf/scroll-optimization-clean
Open

perf: scroll-optimised flush() in cursedRenderer#1725
luispabon wants to merge 2 commits into
charmbracelet:mainfrom
luispabon:perf/scroll-optimization-clean

Conversation

@luispabon

Copy link
Copy Markdown

Closes #1724

Depends on charmbracelet/ultraviolet#130.

This optimises cursedRenderer.flush() for vertical scrolling by detecting string-level line shifts, trimming identical fixed chrome, shifting the cell buffer in place, and using ultraviolet HardScroll plus DrawOver only for newly exposed lines. When no scroll pattern is detected, it falls back to the original Clear + Draw path.

Benchstat (sec/op)

Benchmark Before After Delta
FlushScroll-16 833.3µs 160.2µs -80.78%
FlushScrollPartial-16 871.7µs 202.1µs -76.82%
FlushScrollWithSuffix-16 873.0µs 181.0µs -79.27%

FlushStatic-16 and FlushFullChange-16 were regression-free in sec/op (p=0.183 and p=1.000, respectively).

Files

New files:

  • cursed_renderer_bench_test.go
  • cursed_renderer_scroll.go
  • cursed_renderer_scroll_test.go

Modified files:

  • cursed_renderer.go
  • go.mod
  • go.sum

Detect content scroll at the string level by comparing previous and
current frame lines. When a vertical shift is found:

1. Strip identical leading/trailing chrome lines (status bars, padding)
2. Shift cell buffer rows in-place instead of clearing + full reparse
3. Emit terminal scroll sequences via HardScroll (DECSTBM + scroll ops)
4. Use DrawOver only on the N new/changed lines

This reduces scroll frame cost from ~800us to ~260us for typical TUI
layouts with fixed chrome and a scrolling content region. Falls back to
the original Clear+Draw path when no scroll pattern is detected.

Includes comprehensive tests (scroll detection, cell buffer shifting,
prefix matching) and benchmarks (scroll, static, full-change, partial
chrome, scrollbar suffix scenarios).
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.

perf: cursedRenderer v2 scroll frames significantly slower than v1

1 participant