Skip to content

rioterm: compress the scrollback of terminals no one is using (scrollback-compression) - #2134

Open
raphamorim wants to merge 18 commits into
rio-vt-history-compressionfrom
rioterm-scrollback-compression
Open

raphamorim wants to merge 18 commits into
rio-vt-history-compressionfrom
rioterm-scrollback-compression

Conversation

@raphamorim

@raphamorim raphamorim commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Builds on #2131 (compressed scrollback in rio-vt). Rio now compresses the scrollback of terminals no one is using.

Config

# On by default. Set to false to keep the history as written.
scrollback-compression = true

Turning it off at runtime stops compression; what is already compressed stays that way until it is written to. Turning it on picks up at the next look. scrollback-history-limit still counts lines either way.

When it runs. rioterm decides, not the PTY reader: it knows when someone is using a terminal.

  • Used: a terminal counts as used when input is written to it (keys, a paste, mouse reports; Messenger::send_write), or when keys, the mouse, the wheel, IME or touch reach its window while it's on screen (Screen::mark_in_use, for every pane of the tab shown, since the wheel scrolls the pane under the pointer). Both set Messenger::last_use. Scrolling back is wheel or keys, so it counts. Output doesn't, even under a scrolled-back view, where it moves the viewport's offset to keep the view in place.
  • Looking: once a second, an app-wide timer looks at every terminal in every window.
  • Compressing: one unused for 5 s (IDLE_COMPRESS_AFTER) has its history compressed. Work stops after about 1 ms each look, across all terminals; once that's spent, the rest start nothing. If there's more, the next look comes 10 ms later.
  • The lock: each look only try-locks the terminal (TerminalLock::try_lock, which doesn't ask the reader to make way). If the PTY reader or a frame has it, the next look a second later tries again.
  • Output while unused: output may keep coming: a build, tail -f, an agent in a background tab. While rows keep arriving between looks, a look compresses at most 16 blocks (4096 lines). That costs about 2 ms of the terminal's lock a second, so a flood isn't slowed, and rows about to leave the history aren't compressed for nothing. Once rows stop arriving it runs at full speed.
  • Nothing new: once a pass finds nothing left, the terminal isn't looked at again until something changes: rows in the history or dropped from it, the width (a column resize decompresses it all to reflow), or a new use (a scroll back decodes what it shows).
  • Using it again: typing, clicking or scrolling puts compression off for another 5 s. Scrolling back decodes what it shows as before. The next pass drops that copy, except the block just above the viewport, which frames read.

Searches through the history and snapshots leave nothing decoded behind (#2131), so they need no pass afterwards.

PTY path. The PTY reader does no compression work. rio-vt here is #2131's plus two additions: TerminalLock::try_lock and a RioEvent::CompressScrollback for the application's timer.

Stress test: rio itself. A release build with scrollback-history-limit = 100000 ran cat on a 38 MB log of 500k colored build-output lines. Nothing was typed into the window. The process's footprint (macOS footprint) was sampled every 0.5 s.

after cat (about 1 s) 5 s after launch 8 s 20 s
scrollback-compression = false 213–216 MB 213 MB 213 MB 213 MB
on 212 MB 212 MB 101 MB 100 MB
  • Timing: compression started at the first look after 5 s without input, and finished in about 1.5 s.
  • A one-off spike: in one of three runs, the footprint briefly rose to 199 MB about 17 s after cat, then settled at 99 MB. I didn't find the cause, and it didn't happen in the other runs.

With #2131's slabs, the history's rows live in OS mappings, unmapped once compressed, so the footprint follows. 8 terminals of 10k lines go from 82 MB to 14–20 MB, also after a full read or a column resize.

Tests

  • an_idle_terminal_has_its_scrollback_compressed:
    • not while just used;
    • all of it once unused for 5 s;
    • nothing more until the history changes;
    • input puts it off.
  • scrolling_puts_compression_off_and_flowing_output_gets_a_few_blocks:
    • a scroll waits another 5 s;
    • with a whole history of new lines between looks, one look compresses 16 blocks and leaves the rest. Without the cap, the test fails.
  • what_changes_the_history_without_rows_is_compressed_again: a column resize, and a scroll up and back between two looks (it fails if the done-key leaves out the last use).
  • output_under_a_scrolled_back_view_isnt_someone_using_it: output moving a scrolled-back view's offset leaves last_use alone.
  • test_scrollback_compression: the option's default and false.

…mpression)

The thread reading the PTY compresses the history 250 ms after the
last output (5 s at most while output never pauses), four blocks a
step, a millisecond apart, taking the terminal only when it's free.
A resize starts it too (a column change decompresses the history to
reflow it). Crosswords::set_scrollback_compression turns it on, off
by default for embedders; rioterm sets it from the new
scrollback-compression option, on by default, and again on reload.
… time; scrolling back schedules a pass

- No more deadline 5 s after output started: under output that never
  pauses, a step ran on every read, compressing the oldest rows, the
  ones about to be dropped, on the thread that parses.
- A step compresses 256K cells (a block at least), not 4 blocks, so a
  wide terminal holds the lock as briefly as a narrow one.
- The terminal tells the reader when the viewport moves over the
  history (Msg::Compress, once per pass): what showing it decoded is
  dropped once things are quiet.
- A reloaded scrollback-compression reaches tabs and splits opened
  afterwards too.
… a block per step; a step every 100 ms while rows keep coming

- The idle wait starts over when rows arrive in the history (either
  screen's size or evicted count changes), not on any output: a
  spinner or a redraw on screen, or a program on the alt screen, kept
  pushing it back, so a session with one never had its history
  compressed.
- A step compresses one block and goes on where the last stopped
  (about 0.1 ms at 120 columns, against about 1 ms), so a keystroke's
  echo waits as little behind one.
- While rows keep arriving (a long build log), a block is compressed
  every 100 ms all the same, a fraction of a percent of the parsing
  thread, instead of none until the output pauses.
… needed

The loop read it on every wakeup to see if a step was due, compression
on or not; it does only while a pass is scheduled. After a burst it's
read only when rows arrived in the history (the timer is armed).
…t is kept

- A pass waited 100 ms from the first change whatever came after: the
  backstop was shorter than the quiet interval. It is now 1 s, so output
  that pauses gets its 250 ms.
- Only scrolling asked for the decoded copies to go. A search or a
  snapshot over the history left them until rows next arrived; now a
  block's first decode asks (once until a pass starts), and that pass
  looks over the whole ring.
- Turning compression on asks for a pass for what came in while off.
…pression

# Conflicts:
#	rio-vt/src/crosswords/grid/compress.rs
#	rio-vt/src/crosswords/grid/mod.rs
#	rio-vt/src/crosswords/grid/storage.rs
…ut a pass off until they stop

- The backstop's pass compressed one block a second: each step after
  the first was put off again by the next output. It now compresses
  COMPRESS_FLOWING_BLOCKS (16) a step at a time before output can.
- Scrolling over the history restarts the quiet wait each time (not
  only the first), so a pass doesn't drop copies the user is still
  scrolling through. Reads don't count toward the backstop.
- A block's first decode signals after decoding, not inside the cell's
  initialisation.
…pression

# Conflicts:
#	rio-vt/src/crosswords/grid/compress.rs
…om the PTY reader

The PTY reader no longer schedules compression: its timer, backstop,
Msg::Compress and the read signal are gone, and rio-vt is back to the
compress_history API (plus TerminalLock::try_lock and a
CompressScrollback event for the application's timer).

rioterm looks at every terminal once a second. One with no input
written to it and its viewport not moved for 5 s has its history
compressed a millisecond at a time, only when no one holds its lock:
at full speed once rows stop arriving, at 16 blocks a second while
they keep coming (a background build, tail -f), so a flood isn't slowed
and rows about to leave the history aren't compressed for nothing.
Typing or scrolling in it puts it off for another 5 s.
@raphamorim raphamorim changed the title rioterm: compress the scrollback while output is quiet (scrollback-compression) rioterm: compress the scrollback of terminals no one is using (scrollback-compression) Nov 27, 2026
… its budget, and output under a scrolled-back view isn't use

- What a pass leaves is keyed by the history's rows, the width (a
  column resize decompresses it all) and the viewport's moves, counted
  by rio-vt's scroll_display (Crosswords::viewport_moves): a scroll up
  and back between two looks is seen, and output that moves the offset
  to keep a scrolled-back view still isn't taken for someone scrolling.
- A look while rows keep coming stops at the 1 ms budget too, and a
  terminal starts nothing once the budget is spent.
- A busy terminal waits for the next look a second later instead of
  asking for one every 10 ms.
- The timers' ids are out of window and route ids' range
  (unschedule_window drops timers by id).
… (input, keys, mouse, wheel on its window), not by viewport moves

This branch has not been deployed

No deployments
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