Repository navigation
Description
Summary
_Row.freeze() in src/libvt/_grid.py (#2) sets cells = tuple(self.chars[:t_used]) only when len(text) != t_used, dropping wide glyphs in RowView.runs and FrozenRow.char_at() whenever combining marks and SPACER_TAIL empty strings cancel out to len(text) == t_used, while FrozenRow.used returns len(self.cells) and truncates trailing BCE background spans. In addition, Snapshot.history and Terminal.history materialize tuple(RowView(fr) for fr in ...) on every call (547.27 us per snapshot() and 21,614.75 us for history[-1] at 50,000 scrollback lines on CPython 3.15.1).
Reproduction
- Feed a combining sequence (
e\u0301) followed by a wide CJK glyph (中) and a blueBCEEL 0(CSI 44 m CSI K), then scroll intohistory:
uv run python -c 'from libvt import Terminal; vt = Terminal(rows=2, cols=8, scrollback=10); vt.feed("e\u0301中\x1b[44m\x1b[K\r\n\n\n".encode()); h0 = vt.snapshot().history[0]; print("text:", repr(h0.text), "runs:", [r.text for r in h0.runs]); vt2 = Terminal(rows=2, cols=8, scrollback=10); vt2.feed("中\x1b[44m\x1b[K\r\n\n\n".encode()); print("bce runs:", [r.text for r in vt2.snapshot().history[0].runs])'
Expected
h0.runspreserves'e\u0301中'alongside the trailing blueBCEspan, andvt2.snapshot().history[0].runsincludes the6-column blueBCEtail across columns2..7.Snapshot.historyandTerminal.historyreturn acollections.abc.Sequence[RowView]view over256-row copy-on-writeFrozenRowblocks without allocatingO(scrollback)wrapper tuples per snapshot, whileRowView.runsiteratesFrozenRow.runsinO(R)andFrozenRow.attr_at()binary-searches run endpoints viabisect.bisect_right.
Actual
text: 'é中' runs: ['é', ' '] and bce runs: ['中'] — '中' disappears from h0.runs because len('e\u0301中') == 3 == t_used (+1 combining mark and -1 SPACER_TAIL cancel), and vt2 drops trailing BCE columns because FrozenRow.used returns len(self.cells) == 2.
Environment
Versions
libvt:#2(0.1.0a0)- Python:
3.15.1 - OS: Linux
x86_64
Evidence
CPython 3.15.1 50,000-line scrollback benchmark medians
Measured with timeit.repeat(repeat=7) on CPython 3.15.1 at 50,000 scrollback rows:
- Eager
tuple[RowView, ...]:snapshot()547.27 us,snap.history[-1]21,614.75 us. 256-row immutable tuple blocks behind a lazySequence[RowView]:snapshot()0.83 us(662.1xfaster),snap.history[-1]0.171 us(126,629.0xfaster).
Proposal
Invariants
The invariants below are load-bearing; everything under Intent is open.
- Every
FrozenRowpreserves exact per-columnchar_at(col),attr_at(col),ext_at(col), andusedcolumn width across narrow, wide, combining,SPACER_HEAD, and trailingBCEcells (FrozenRow.thaw()round-trips losslessly). Snapshot.history,Screen.history, andTerminal.historysatisfycollections.abc.Sequence[RowView](len, negative/positive indexing, slicing,reversed) withO(1)snapshot()capture andO(1)indexed row access independent of total scrollback depth.
Intent (non-binding)
- Track non-1:1 cell occupancy via
"" in chars[:t_used]alongsidelen(text) != t_used(or a write-time flag) in_Row.freeze(), store explicitused: intonFrozenRow, cache_frozen: FrozenRow | Noneon clean viewport_Rows, usebisect_rightinFrozenRow.attr_at/ext_at, and add aFrozenRowfast path inRowView.runs. - Store primary scrollback in
256-row immutable tuple blocks plus a bounded tail list/tuple (HistoryBuffer/HistoryView).
Not doing
- Exposing internal block size (
256) in public API signatures.
Activity
grid(fix[history]): Add CoW Sequence history (#20)
tony commentedon Nov 23, 2026
Resolved in #2 (
0c7ee41).