Repository navigation
Description
Summary
Terminal._handle_print() in src/libvt/terminal.py (#2) fast-paths printable ASCII (0x20..0x7E) and routes every non-ASCII scalar through per-character Python step_cluster(), running 4x to 9x slower on Latin-1 (0.58 MiB/s), CJK (0.86 MiB/s), Devanagari (0.88 MiB/s), emoji (0.97 MiB/s), and box-drawing UI (1.10 MiB/s) than on ASCII (4.42 MiB/s on CPython 3.15.0). In src/libvt/parser.py (#2), OSC_STRING, DCS_PASSTHROUGH, and SOS_PM_APC_STRING scan payload interiors via a pure-Python while pos < n: byte loop instead of a compiled C regex search like GROUND.
Motivation
TUI frames (btop, htop, nvim, tmux split borders, localized logs, and CJK output) interleave box-drawing (U+2500..U+257F), accented Latin, and East Asian Wide characters with CSI sequences. Under Unicode 17.0.0 UAX #29, any two immediately adjacent scalars with Grapheme_Cluster_Break in {Other, LV, LVT} always break (0 joins across 1,089 representative pairs verified on Python 3.15.0 unicodedata.iter_graphemes()), because GB9c requires an intervening InCB=Linker (GCB=Extend) and GB11 requires ZWJ. Deferring clustering until row read is unsound because cell width governs autowrap, DSR 6n, and mid-line edits at write time.
Measured 64 KiB stream medians on CPython 3.15.0 (#2)
ASCII plain log:4.42 MiB/s(feed),3.18 MiB/s(feed+4 KiBcommitted)Box-drawing UI:1.10 MiB/s(feed),1.11 MiB/s(feed+4 KiBcommitted)Emoji ZWJ clusters:0.97 MiB/s(feed),0.93 MiB/s(feed+4 KiBcommitted)Devanagari GB9c:0.88 MiB/s(feed),0.85 MiB/s(feed+4 KiBcommitted)CJK wide BMP:0.86 MiB/s(feed),0.86 MiB/s(feed+4 KiBcommitted)Latin-1 accented(2-byteUTF-8):0.58 MiB/s(feed),0.57 MiB/s(feed+4 KiBcommitted)
Proposal
Invariants
The invariants below are load-bearing; everything under Intent is open.
- Runtime dependencies remain empty (
dependencies = []inpyproject.toml, shipping a universalpy3-none-any.whl). - Safe-run bulk writes produce cell contents,
WidthRoleflags,_cluster_anchorextendable state on the final cell,_last_printedforREP, and100%of Unicode 17.0.0GraphemeBreakTest.txtvectors bit-for-bit identical to per-scalarstep_cluster(). Limits.max_osc_bytes,max_dcs_bytes, andmax_apc_bytescaps andDCS tmux; ESC ESCunescaping behave identically across single-byte and multi-megabyte chunks.
Intent (non-binding)
- Replace the byte-by-byte
while pos < n:loops inParser.feed_to()(OSC_STRING,DCS_PASSTHROUGH,SOS_PM_APC_STRING) andTerminal.feed_until()with compiledre.Pattern[bytes]searches ([����]and alatin1C1 variant). - Pre-generate compiled stdlib
resafe-run patterns in_ucd.py(scripts/generate_ucd.py) for width-1 BMP (Latin,Greek,Cyrillic,Box DrawingexcludingVS16emoji bases whenvs16_always_wideis off) and width-2 BMP (CJK Unified Ideographs,Hiragana/Katakana, precomposedHangulLV/LVT), bulk-writing interior slices when carried cluster state is clean and leaving the final cell anchored for trailingExtend/ZWJ/SpacingMark/V/Tscalars. - Evaluate pooling
array('Q')attribute buffers across_Row.reset_blank()during styled scrollback churn (keepingScreen._rowsaslist[_Row]for O(1) indexed access and slice shifts) and add dual-tier A/B regression benchmarks (common-feature parity vs full-feature cost) inscripts/bench_vt.pyusing pyperf (repo, PyPI).
Not doing
- Deferring UAX Support
XTSAVE(CSI ? Pm s) andXTRESTORE(CSI ? Pm r) private mode save/restore #29 segmentation to row read time. - Shipping a mandatory Rust or Cython extension in
1.0.
Alternatives
- Using Python
3.15unicodedata.iter_graphemes()on the hot path was weighed and set aside for3.12–3.14compatibility and to avoid quadratic resegmentation when combining marks stream one byte at a time;iter_graphemes()remains useful as a3.15test oracle. - Replacing
Screen._rows: list[_Row]withcollections.dequewas weighed and rejected because24–50object pointers fit in 3–6 L1 cache lines whiledequebreaks slice assignment across 6 scroll-region sites and degrades O(1) row indexing on every printed character.
Activity
Parser(feat[scan]): Add regex and BMP fast paths (#11)
Parser(feat[scan]): Add regex and BMP fast paths (#11)
tony commentedon Nov 23, 2026
Resolved in #2 (
0c7ee41).