Skip to content

Fix _Row.freeze() wide-glyph loss and back history with 256-row CoW Sequence #20

Closed

Description

@tony

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

  1. Feed a combining sequence (e\u0301) followed by a wide CJK glyph (中) and a blue BCE EL 0 (CSI 44 m CSI K), then scroll into history:
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.runs preserves 'e\u0301中' alongside the trailing blue BCE span, and vt2.snapshot().history[0].runs includes the 6-column blue BCE tail across columns 2..7.
  • Snapshot.history and Terminal.history return a collections.abc.Sequence[RowView] view over 256-row copy-on-write FrozenRow blocks without allocating O(scrollback) wrapper tuples per snapshot, while RowView.runs iterates FrozenRow.runs in O(R) and FrozenRow.attr_at() binary-searches run endpoints via bisect.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 lazy Sequence[RowView]: snapshot() 0.83 us (662.1x faster), snap.history[-1] 0.171 us (126,629.0x faster).

Proposal

Invariants

The invariants below are load-bearing; everything under Intent is open.

  • Every FrozenRow preserves exact per-column char_at(col), attr_at(col), ext_at(col), and used column width across narrow, wide, combining, SPACER_HEAD, and trailing BCE cells (FrozenRow.thaw() round-trips losslessly).
  • Snapshot.history, Screen.history, and Terminal.history satisfy collections.abc.Sequence[RowView] (len, negative/positive indexing, slicing, reversed) with O(1) snapshot() capture and O(1) indexed row access independent of total scrollback depth.

Intent (non-binding)

  • Track non-1:1 cell occupancy via "" in chars[:t_used] alongside len(text) != t_used (or a write-time flag) in _Row.freeze(), store explicit used: int on FrozenRow, cache _frozen: FrozenRow | None on clean viewport _Rows, use bisect_right in FrozenRow.attr_at / ext_at, and add a FrozenRow fast path in RowView.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.

References

Activity

  1. added
    bug
    on Nov 23, 2026
  2. tony commented on Nov 23, 2026

    @tony
    MemberAuthor

    Resolved in #2 (0c7ee41).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions