Repository navigation
rio-vt: compressed scrollback (byte planes + LZ4, decoded on read) - #2131
Open
raphamorim wants to merge 17 commits into
Open
raphamorim wants to merge 17 commits into
raphamorim wants to merge 17 commits into
Conversation
Crosswords::compress_history(max_blocks) packs cold history (rows above the viewport, and the rows the ring keeps for a later grow) in blocks of 256 rows. Cells are split into byte planes; a plane that's one byte throughout is kept as that byte, the others LZ4-compressed (lz4_flex, pure Rust, safe). A compressed row keeps its place in the ring and its flags, and points at its block. - Reading (Index) decodes the row's block once into the block's cache; writing (IndexMut) makes the row plain again. Compressing again drops the caches without re-encoding. - Rows about to be overwritten (scrolled into place, erased) are blanked without decoding (index_mut_discard). - A column resize decompresses first. - The style and extras sweeps read a block's ids instead of its cells. - history_memory() reports compressed/plain rows and bytes. 10k styled lines: 9.4 MB of rows in 179 KB, compressed in ~5 ms; a snapshot reading all of it back takes 2.2 ms instead of 1.1. Tests compare a compressing terminal with a plain twin through random output, compression, resizes, scrolling and screen switches; the formatter's random round trip compresses every fourth terminal.
The hand-rolled word loop assumed Row's size in words: 5 before this branch (already wrong on 32-bit targets, where Row is 6 words), 6 with the compressed-row fields on 64-bit but 7 on 32-bit (wasm), so it would swap only part of each row there. slice::swap makes the same copies on every target; vt_input is unchanged.
…per-row scans - snapshot_visible read and cleared row dirty bits through the grid: a compressed row read as its decoded copy (always dirty), and clearing the bit decompressed it, so scrolling back unpacked the history. Storage::is_dirty/set_dirty touch the stored row's own bit. - compress_from builds no index of the whole ring and finds a row's block from the last one looked up; memory_from likewise, and counts an encoding two blocks share once.
… reads rows decompresses them - A block whose rows were mostly written (made plain) is unpacked whole on the next compression and compressed again with them: one row left in it no longer keeps all of it. - damage_range_rows (selections, highlights) and Grid::scroll_down mark the stored rows dirty; scroll_to_prompt reads the stored rows' marks; writing over a wide character at the top left checks the row above before taking it to change. - Tests: accounting, idempotent and stepwise compression, editing, clearing and reset, row grows over compressed history, resizes while scrolled back, a pinned viewport, the alt screen, ids reused after sweeps, search, selections, vi motions and prompt jumps over blocks.
…uts isn't taken for a sparse one The live-row counts and the decoded-copy replacements were found by a linear scan per new block, quadratic in the blocks per call; both are hash maps now, so a call is linear in the ring. Live rows are counted over the whole ring, the viewport's too, so a block cut by its edge isn't unpacked and split as one mostly written. Storage::rows (rows as stored, a compressed one empty) is crate-internal.
…imited call shares its limit across the screens A call limited to a few blocks walked the whole ring again from the oldest row, and counted the live rows of every block first: a pass in one-block steps cost the ring once per block. The grid keeps where it stopped, and a call on the same ring (nothing scrolled, resized or scrolled back meanwhile) goes on from there without the first walk. The inactive screen's history is compressed once the active one's is done, so a limited call does about its limit, not twice it.
…nce compressed A row's cells were a Vec of their own: a small allocation that, freed when the row was compressed, stayed with the allocator (on macOS even a freed 16 MB malloc stays on the footprint; an munmap goes at once). 8 terminals of 10k lines took 40 MB of footprint compressed (25 MB later) and 44 MB after a column resize, for 10 MB of live heap. - Row::inner is a Cells: a Vec, or a slot of a Slab, 256 rows' cells in one mmap (VirtualAlloc on Windows, the allocator elsewhere) unmapped once all its rows let go of their slots. A slot reads and writes as a Vec; whatever would reallocate it moves the cells to a Vec first. Only cells without drop glue go in slabs. - The storage hands its rows slots in order: a growing history, output recycling compressed rows (no allocation per row any more), pooled rows taken back, unpacking for a column resize, and a reflow's rows, moved to slots of the new width before they grow and after they're cut. - A decoded block's rows are in a slab of their own, unmapped with the decoded copy; encode and decode work in buffers kept per thread. A few unmapped slabs are kept for reuse (on macOS marked reusable, off the footprint), so a read of the history doesn't fault in fresh pages each time. Now 14-20 MB, also after reading it all back and after a resize. The input and frame benches are unchanged (within 2%), compressing is 6% faster, and a snapshot of compressed history takes 2.45 ms per 10k rows instead of 2.22.
…rows were made plain The resume cursor was kept by the ring's shape alone, so a write into compressed history or a column grow that kept the shape (nothing soft wrapped) left the rows before the cursor plain until the next full pass. It now lives with the storage and anything that makes compressed rows plain forgets it; a sparse block's rows are unpacked into slab slots.
… viewport What's drawn reads a little past the viewport's top (a search's matches running into it). A pass in steps dropped that block's decoded copy, and the next frame decoded it again, pass after pass. A whole pass (`compress_history(None)`) still drops it.
…es nothing decoded behind Bulk reads decoded every block they touched into the block's cache, kept until a compression pass dropped it. Now the formatter (snapshots, the text with the scrollback), bounds_to_string (copying a selection) and a search over more than 512 lines (search_next, or regex_search over such a span) read inside `transient_reads`: a block no one decoded yet is decoded once into a scratch of the read's own, freed when it returns. Blocks the viewport decoded are read from their caches. Short searches (the hints around the viewport, every frame) still read the caches, so a frame doesn't decode the same block again.
…s scope; block selections and both passes of a match in one - format_to/snapshot_to ran the caller's io::Write inside the scope, so a writer holding the terminal could read a row there, keep it, and use it after the scratch was freed. Out now writes to the sink through outside_transient_reads (the scope's scratch set aside, reads going through the caches), and plain text is built in its scope and written after. - selection_to_string is one scope (a block selection copied a line at a time, each decoding into the caches). - regex_search_left/right open one scope for both of a match's passes, and tell a search round the end from a short one by position, not by rows alone.
This branch has not been deployed
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.
Stacked on #2130. Adds compressed scrollback to rio-vt:
Crosswords::compress_history(max_blocks)packs history rows that aren't on screen, and an embedder calls it while output is quiet.How it works
u64each) are split into eight byte planes, so every cell's lowest byte comes first, then every cell's next byte, and so on. A plane that is one byte throughout is stored as that byte, which most are: the codepoint's high bytes in ASCII text, and the extras id where nothing is linked. Each remaining plane is LZ4-compressed on its own withlz4_flex, which is pure Rust and uses the safe encoder and decoder.Arc, so rotations, swaps and re-zeroing are unchanged.Reads and writes
Reading (
Index, so rendering, the cursor, vi motions, hints) decodes the row's whole block once into the block's cache and hands out that copy. Callingcompress_historyagain drops those copies without re-encoding anything.Bulk reads leave nothing decoded behind. These read inside
transient_reads:bounds_to_stringandselection_to_string, so copying a selection, block selections included;search_next, orregex_searchover such a span).A block that isn't decoded yet is decoded once into a scratch owned by that read, and freed when the read returns. Blocks the viewport already decoded are read from their caches. Short searches, like the hints around the viewport on every frame, still use the caches, so frames don't decode the same block every time. A search counts as long when it runs more than 512 lines or wraps around the end, and both passes of a match share one scope. Code that isn't rio-vt's own never runs inside a scope. When a snapshot is streamed to the caller's
io::Write, each write runs outside it, with reads going through the caches, so whatever the writer keeps stays valid. While a bulk read runs, its scratch holds every block it has touched; it doesn't drop them one at a time as it goes.Writing (
IndexMut) turns the row back into a plain row with its cells. The check is a test of one field on every write, with the work done out of line.Overwritten rows: a row about to be blanked, such as one coming back to the bottom as a new line once the ring wraps, is reset without being decoded (
index_mut_discard).Column resizes decompress everything first, since they rewrite every row.
The style and extras sweeps read the ids each block's cells carry from a list kept beside the block, so nothing is decoded.
Numbers (
benches/vt_snapshot.rs, 10k lines of styled 120-column history)compress_history(None)No path that doesn't compress got measurably slower. I ran
vt_inputandvt_snapshoton this branch and the base alternately. The differences stayed within this machine's run-to-run spread of ±5–10%, with no consistent direction.Tests
grid::compress::testscompares a terminal that compresses against a plain twin fed the same output, through random output, compression (all at once and in steps), resizes, scrolling back, and alt-screen switches. Snapshot (with palette) and text must stay identical.cargo test -p rio-vtpasses (796), as doescargo test -p rioterm(310). Clippy is clean, and librio builds.Also
Storage::swapisslice::swapnow. The hand-rolled loop assumedRowwas a fixed number of machine words. That was already wrong on 32-bit targets such as wasm, whereRowwas 6 words against the loop's 5, and the new fields would have made a partial swap there.vt_inputis unchanged.Memory the OS gets back: rows in slabs
Compressing freed each row's cell buffer, a small allocation. The allocator kept those: on macOS even a freed 16 MB malloc stays on the process's footprint, while an
munmapis reflected at once. So the heap fell 50x but the footprint only about 2x. With 8 terminals of 10k lines at 120 columns: 82 MB filled, 40 MB compressed (25 MB later), and 44 MB after a column resize, for 10 MB of live heap.Row::inneris now aCells(grid/cells.rs): aVec, or a slot of aSlab. A slab holds 256 rows' cells in one mapping from the OS (mmap;VirtualAllocon Windows; the allocator on other targets), unmapped once all its rows let go of their slots.Vec. Anything that would reallocate it moves the cells to aVecfirst. Only cells without drop glue go in slabs.MADV_FREE_REUSABLE, which takes them off the footprint.Benches against the previous commit (minimum of two interleaved runs):
compress_10k: 6% faster;In rio itself (#2134 turns compression on),
catof a 272 MB log into a 100k-line history leaves the process at 109–112 MB of footprint, against 230–233 MB on main, with the same throughput. The full numbers are in #2134.Windows'
VirtualAllocpath couldn't be built here (no cross toolchain for this repo's pinned Rust); CI's Windows job covers it.Possible follow-ups