Speed up parsing of CSI codes and erasing of lines

- Check for multicell chars in the erased range before looking for them
  cell by cell, with a branch free check that reads only the word of each
  cell containing the multicell flag. The common case is a line with no
  multicell chars.

- Add a fast path that parses CSI codes of the common form, an optional
  private marker, numeric params separated by ; or : and the final byte,
  in a single pass, falling back to the generic CSI parser for anything
  else.

- Do not call the UTF-8 decoder for no text when an escape code directly
  follows another one, and start parsing CSI codes right after ESC [
  rather than in the next iteration of the parse loop.

This speeds up the CSI benchmark by 25% to 30% and the truecolor
benchmarks by ~10%.

A differential test compares the fast path and the generic CSI parser on
random input, and a new test covers erasing parts of multicell chars,
which no existing test did.
This commit is contained in:
Kovid Goyal 2026-11-06 17:12:43 +05:30
parent 015d7b3868
commit b9ade2dd42
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 194 additions and 3 deletions

View file

@ -669,12 +669,41 @@ nuke_multiline_chars_torn_by_scroll(Screen *self, index_type top, index_type bot
#undef has_multiline
}
static bool
has_multicell_in_range(const CPUCell *cells, index_type x_start, index_type x_limit) {
// Lines rarely contain multicell chars, so check for them before looking for them
// cell by cell. This reads only the 32 bit word of each cell that contains the
// is_multicell flag, via memcpy() to avoid aliasing issues, into independent
// accumulators so that there is no dependency between consecutive cells.
static_assert(sizeof(CPUCell) == 3 * sizeof(uint32_t), "has_multicell_in_range() needs updating");
const CPUCell probe = {.is_multicell = true};
uint32_t mask[3];
memcpy(mask, &probe, sizeof(mask));
const unsigned word = mask[0] ? 0 : (mask[1] ? 1 : 2);
const uint8_t *p = (const uint8_t *)(cells + x_start) + word * sizeof(uint32_t);
const uint8_t *limit = (const uint8_t *)(cells + x_limit) + word * sizeof(uint32_t);
uint32_t a = 0, b = 0, c = 0, d = 0, v;
#define W(acc, i) \
memcpy(&v, p + (i) * sizeof(CPUCell), sizeof(v)); \
acc |= v;
for (; p + 4 * sizeof(CPUCell) <= limit; p += 4 * sizeof(CPUCell)) {
W(a, 0);
W(b, 1);
W(c, 2);
W(d, 3);
}
for (; p < limit; p += sizeof(CPUCell)) { W(a, 0); }
#undef W
return ((a | b | c | d) & mask[word]) != 0;
}
static void
nuke_multicell_char_intersecting_with(Screen *self, index_type x_start, index_type x_limit, index_type y_start, index_type y_limit, bool replace_with_spaces) {
for (index_type y = y_start; y < y_limit; y++) {
CPUCell *cp;
GPUCell *gp;
linebuf_init_cells(self->linebuf, y, &cp, &gp);
if (!has_multicell_in_range(cp, x_start, x_limit)) continue;
for (index_type x = x_start; x < x_limit; x++) {
if (cp[x].is_multicell) nuke_multicell_char_at(self, x, y, replace_with_spaces);
}

View file

@ -967,8 +967,71 @@ csi_parse_loop(PS *self, ParsedCSI *csi, const uint8_t *buf, size_t *pos, const
#undef COMMIT_PARAM
}
static bool
csi_parse_simple(ParsedCSI *csi, const uint8_t *buf, size_t *pos, const size_t sz) {
// Fast path for the common form of CSI codes: an optional private marker, then
// non-empty numeric params separated by ; or : and then the final byte, all already
// in the buffer. Parses them in one pass with no per byte state machine transitions.
// Leaves csi and *pos as csi_parse_loop() would, and returns true, only when the
// complete code has this form, otherwise returns false and the caller must fall
// back to csi_parse_loop(), which handles everything else, including reporting
// errors. Must only be called in the CSI_START state.
size_t p = *pos;
if (p >= sz) return false;
char primary = 0;
switch (buf[p]) {
case '?':
case '>':
case '<':
case '=': primary = buf[p++]; break;
}
unsigned num_params = 0;
while (true) {
if (p >= sz) return false;
uint8_t ch = buf[p];
if ((uint8_t)(ch - '0') <= 9) {
uint64_t accumulator = 0;
const size_t digits_start = p;
do {
accumulator = accumulator * 10 + (ch - '0');
if (++p >= sz) return false;
ch = buf[p];
} while ((uint8_t)(ch - '0') <= 9);
if (p - digits_start > MAX_CSI_DIGITS || num_params >= MAX_CSI_PARAMS) return false;
csi->params[num_params++] = (int64_t)accumulator;
if (ch == ';' || ch == ':') {
// An empty param, which is not of this form, is detected by the next iteration
if (num_params >= MAX_CSI_PARAMS) return false;
csi->is_sub_param[num_params] = ch == ':';
p++;
if (p >= sz || (uint8_t)(buf[p] - '0') > 9) return false;
continue;
}
} else if (num_params) return false;
switch (ch) {
case CSI_TRAILER:
csi->primary = primary;
csi->num_params = num_params;
csi->trailer = ch;
csi->is_valid = true;
if (primary || num_params) csi->state = CSI_BODY;
*pos = p + 1;
return true;
default: return false;
}
}
}
#ifdef DUMP_COMMANDS
extern bool csi_parse_simple_disabled_for_tests;
#else
bool csi_parse_simple_disabled_for_tests = false;
#endif
static bool
consume_csi(PS *self) {
if (self->csi.state == CSI_START && !csi_parse_simple_disabled_for_tests && csi_parse_simple(&self->csi, self->buf, &self->read.pos, self->read.sz))
return true;
return csi_parse_loop(self, &self->csi, self->buf, &self->read.pos, self->read.sz, self->read.consumed);
}
@ -1551,12 +1614,20 @@ consume_input(PS *self, PyObject *dump_callback UNUSED, id_type window_id UNUSED
switch (self->vte_state) {
case VTE_NORMAL:
consume_normal(self);
if (self->buf[self->read.pos] == ESC && self->utf8_decoder.state.cur == UTF8_ACCEPT) {
// Escape codes often follow each other directly, avoid calling the UTF-8
// decoder for no text. This is what it does on seeing ESC.
zero_at_ptr(&self->utf8_decoder.state);
self->read.pos++;
SET_STATE(ESC);
} else consume_normal(self);
self->read.consumed = self->read.pos;
break;
case VTE_ESC:
if (consume_esc(self)) { self->read.consumed = self->read.pos; }
break;
// Start parsing a CSI code right away rather than in the next call
if (self->vte_state != VTE_CSI || self->read.pos >= self->read.sz) break;
/* fallthrough */
case VTE_CSI:
if (consume_csi(self)) {
self->read.consumed = self->read.pos;
@ -1767,6 +1838,17 @@ static PyMethodDef methods[] = {
{NULL},
};
static PyObject *
test_set_csi_parse_simple(PyObject *self UNUSED, PyObject *val) {
csi_parse_simple_disabled_for_tests = !PyObject_IsTrue(val);
Py_RETURN_NONE;
}
static PyMethodDef module_methods[] = {
{"test_set_csi_parse_simple", test_set_csi_parse_simple, METH_O, ""},
{NULL},
};
PyTypeObject Parser_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "fast_data_types.Parser",
.tp_basicsize = sizeof(Parser),
@ -1811,7 +1893,8 @@ alloc_vt_parser(id_type window_id) {
#define EXTRA_INIT \
if (0 != PyModule_AddIntConstant(module, "VT_PARSER_BUFFER_SIZE", BUF_SZ)) return 0; \
if (0 != PyModule_AddIntConstant(module, "VT_PARSER_MAX_ESCAPE_CODE_SIZE", MAX_ESCAPE_CODE_LENGTH)) return 0; \
if (!init_simd(module)) return 0;
if (!init_simd(module)) return 0; \
if (PyModule_AddFunctions(module, module_methods) != 0) return 0;
INIT_TYPE(Parser)

View file

@ -1029,6 +1029,67 @@ class TestParser(BaseTest):
pb('\033[' + '1;' * 255 + '1:2m\033[31m', too_many, ('select_graphic_rendition', '31'))
pb('\033[' + '1;' * 256 + 'm\033[31m', ('select_graphic_rendition', ';'.join('1' * 256)), ('select_graphic_rendition', '31'))
def test_csi_parse_simple(self):
# The fast path for simple CSI codes must give the same results as the
# generic CSI parser, including for codes it does not handle
import random
from kitty.fast_data_types import test_set_csi_parse_simple
def param(rng):
r = rng.random()
if r < 0.7:
return str(rng.randint(0, 300))
if r < 0.8:
return ''
if r < 0.85:
return '9' * rng.randint(15, 20)
if r < 0.9:
return '-' + str(rng.randint(0, 9))
return rng.choice(('\n', '\x08', '\r', ' ', '$', '\x07')) + str(rng.randint(0, 9))
def csi(rng):
ans = '\x1b['
if rng.random() < 0.3:
ans += rng.choice('?><=')
n = rng.choice((0, 1, 1, 2, 3, 5, 8)) if rng.random() < 0.99 else rng.randint(250, 260)
for i in range(n):
if i:
ans += rng.choice(';;;:')
ans += param(rng)
if rng.random() < 0.05:
ans += rng.choice(' $"!')
return ans + rng.choice('mmmHJKABCDEGbrhlsun@`dXPLMSTx~q')
def data_for(seed):
rng = random.Random(seed)
return ''.join(csi(rng) if rng.random() < 0.7 else 'ab日' * rng.randint(0, 3) for _ in range(rng.randint(1, 60))).encode()
def run(data, chunk_sizes, dump):
s = self.create_screen(cols=20, lines=10, scrollback=10)
cd = CmdDump() if dump else None
pos = 0
for sz in chunk_sizes:
parse_bytes(s, data[pos : pos + sz], cd)
pos += sz
parse_bytes(s, data[pos:], cd)
lines = tuple(s.linebuf.line(i).as_ansi() for i in range(s.lines))
return (cd.get_result() if dump else None), lines, (s.cursor.x, s.cursor.y), s.cursor.fg, s.cursor.bg
try:
for seed in range(500):
data = data_for(seed)
rng = random.Random(seed)
chunk_sizes = [rng.randint(1, 16) for _ in range(rng.randint(0, 8))]
for dump in (True, False):
test_set_csi_parse_simple(True)
a = run(data, chunk_sizes, dump)
test_set_csi_parse_simple(False)
b = run(data, chunk_sizes, dump)
self.ae(a, b, f'seed={seed} dump={dump} data={data!r}')
finally:
test_set_csi_parse_simple(True)
def test_csi_code_rep(self):
s = self.create_screen(8)
pb = partial(self.parse_bytes_dump, s)

View file

@ -930,6 +930,24 @@ class TestScreen(BaseTest):
finally:
test_set_history_reuse(True)
def test_erase_in_line_removes_intersecting_multicell_chars(self):
for how, wide_expected in ((0, 'ab'), (1, ' cd'), (2, '')):
s = self.create_screen(cols=8, lines=3)
s.draw('ab日cd')
s.cursor.x = 3 # second cell of the wide char
parse_bytes(s, f'\x1b[{how}K'.encode())
self.ae(str(s.line(0)), wide_expected, f'how={how}')
self.assertFalse(any(c['mcd'] for c in s.cpu_cells(0)), f'how={how}')
# Erasing part of a char that spans two lines must remove all of it
s = self.create_screen(cols=8, lines=3)
s.draw('ab')
draw_multicell(s, 'X', scale=2)
s.draw('cd')
s.cursor.x, s.cursor.y = 3, 1
parse_bytes(s, f'\x1b[{how}K'.encode())
self.ae(str(s.line(0)), 'ab cd', f'how={how}')
self.assertFalse(any(c['mcd'] for y in range(2) for c in s.cpu_cells(y)), f'how={how}')
def test_batched_non_ascii_draw(self):
import random