Skip to content

Add C-regex string scans and GCB safe-run non-ASCII slice fast path #11

Closed

Description

@tony

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 KiB committed)
  • Box-drawing UI: 1.10 MiB/s (feed), 1.11 MiB/s (feed + 4 KiB committed)
  • Emoji ZWJ clusters: 0.97 MiB/s (feed), 0.93 MiB/s (feed + 4 KiB committed)
  • Devanagari GB9c: 0.88 MiB/s (feed), 0.85 MiB/s (feed + 4 KiB committed)
  • CJK wide BMP: 0.86 MiB/s (feed), 0.86 MiB/s (feed + 4 KiB committed)
  • Latin-1 accented (2-byte UTF-8): 0.58 MiB/s (feed), 0.57 MiB/s (feed + 4 KiB committed)

Proposal

Invariants

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

  • Runtime dependencies remain empty (dependencies = [] in pyproject.toml, shipping a universal py3-none-any.whl).
  • Safe-run bulk writes produce cell contents, WidthRole flags, _cluster_anchor extendable state on the final cell, _last_printed for REP, and 100% of Unicode 17.0.0 GraphemeBreakTest.txt vectors bit-for-bit identical to per-scalar step_cluster().
  • Limits.max_osc_bytes, max_dcs_bytes, and max_apc_bytes caps and DCS tmux; ESC ESC unescaping behave identically across single-byte and multi-megabyte chunks.

Intent (non-binding)

  • Replace the byte-by-byte while pos < n: loops in Parser.feed_to() (OSC_STRING, DCS_PASSTHROUGH, SOS_PM_APC_STRING) and Terminal.feed_until() with compiled re.Pattern[bytes] searches ([����] and a latin1 C1 variant).
  • Pre-generate compiled stdlib re safe-run patterns in _ucd.py (scripts/generate_ucd.py) for width-1 BMP (Latin, Greek, Cyrillic, Box Drawing excluding VS16 emoji bases when vs16_always_wide is off) and width-2 BMP (CJK Unified Ideographs, Hiragana/Katakana, precomposed Hangul LV/LVT), bulk-writing interior slices when carried cluster state is clean and leaving the final cell anchored for trailing Extend/ZWJ/SpacingMark/V/T scalars.
  • Evaluate pooling array('Q') attribute buffers across _Row.reset_blank() during styled scrollback churn (keeping Screen._rows as list[_Row] for O(1) indexed access and slice shifts) and add dual-tier A/B regression benchmarks (common-feature parity vs full-feature cost) in scripts/bench_vt.py using pyperf (repo, PyPI).

Not doing

Alternatives

  • Using Python 3.15 unicodedata.iter_graphemes() on the hot path was weighed and set aside for 3.12–3.14 compatibility and to avoid quadratic resegmentation when combining marks stream one byte at a time; iter_graphemes() remains useful as a 3.15 test oracle.
  • Replacing Screen._rows: list[_Row] with collections.deque was weighed and rejected because 24–50 object pointers fit in 3–6 L1 cache lines while deque breaks slice assignment across 6 scroll-region sites and degrades O(1) row indexing on every printed character.

References

Activity

  1. added
    enhancement
    on Nov 22, 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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions