Speed up drawing of cells with many combining chars

- Add runs of plain combining chars to a cell with a single lookup in
  the text cache, rather than one for each char, which also added the
  intermediate texts of the cell to the cache.

- Also collect garbage in the text cache when starting a new line, not
  just when starting to draw text, as a single draw can add a great many
  entries, making the cache slower to use the larger it grows.

- Size the hash map of the text cache for the expected number of entries
  after garbage collection, to avoid repeatedly rehashing it as it grows.

This makes the unique multi-codepoint cells benchmark ~80% faster.

The differential test of batched drawing now compares cells by content,
since cells with multiple codepoints store indices into the text cache,
which change as intermediate texts are no longer added to it.
This commit is contained in:
Kovid Goyal 2026-11-06 21:53:08 +05:30
parent c69b7fa19c
commit 81990718c6
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 84 additions and 8 deletions

View file

@ -1103,6 +1103,11 @@ init_segmentation_state(Screen *self, text_loop_state *s) {
static void
init_text_loop_line(Screen *self, text_loop_state *s) {
// Also collect garbage in the text cache when starting a new line, not just
// when starting to draw text, as a single draw can add a great many entries,
// for example, when drawing a long stream of unique multi-codepoint cells,
// making the cache slower to use the larger it grows.
if (tc_should_gc(self->text_cache)) screen_garbage_collect_text_cache(self);
linebuf_init_cells(self->linebuf, self->cursor->y, &s->cp, &s->gp);
clear_intersecting_selections(self, self->cursor->y);
linebuf_mark_line_dirty(self->linebuf, self->cursor->y);
@ -1300,7 +1305,8 @@ screen_garbage_collect_text_cache(Screen *self) {
}
static bool
add_combining_char(Screen *self, char_type ch, index_type x, index_type y) {
add_combining_chars(Screen *self, const char_type *chars, size_t num_chars, index_type x, index_type y) {
// Adds num_chars combining chars to the cell at x, y with a single lookup in the text cache
CPUCell *cpu_cells = linebuf_cpu_cells_for_line(self->linebuf, y);
CPUCell *cell = cpu_cells + x;
if (!cell_has_text(cell) || (cell->is_multicell && cell->y)) return false; // don't allow adding combining chars to a null cell
@ -1308,8 +1314,10 @@ add_combining_char(Screen *self, char_type ch, index_type x, index_type y) {
if (self->lc->count >= MAX_NUM_CODEPOINTS_PER_CELL) return false; // don't allow too many combining chars to prevent DoS attacks
// must be computed before the char is changed
const MulticellRows rows = cell->is_multicell ? multicell_char_rows(self, x, y) : (MulticellRows){0};
ensure_space_for_chars(self->lc, self->lc->count + 1);
self->lc->chars[self->lc->count++] = ch;
num_chars = MIN(num_chars, MAX_NUM_CODEPOINTS_PER_CELL - self->lc->count);
ensure_space_for_chars(self->lc, self->lc->count + num_chars);
memcpy(self->lc->chars + self->lc->count, chars, num_chars * sizeof(chars[0]));
self->lc->count += num_chars;
cell->ch_or_idx = tc_get_or_insert_chars(self->text_cache, self->lc);
cell->ch_is_idx = true;
if (cell->is_multicell) {
@ -1326,6 +1334,11 @@ add_combining_char(Screen *self, char_type ch, index_type x, index_type y) {
return true;
}
static bool
add_combining_char(Screen *self, char_type ch, index_type x, index_type y) {
return add_combining_chars(self, &ch, 1, x, y);
}
static bool
has_multiline_cells_in_span(const CPUCell *cells, const index_type start, const index_type count) {
@ -1531,6 +1544,51 @@ map_char(Screen *self, const uint32_t ch) {
return UNLIKELY(self->charset.current && ch < 256) ? self->charset.current[ch] : ch;
}
static bool batched_non_ascii_draw_disabled_for_tests = false;
static bool
is_plain_combining_char(char_type ch, GraphemeSegmentationResult seg, CharProps props) {
// A combining char that only adds to the text of a cell, see draw_combining_char()
return ch != VS15 && ch != VS16 && !(seg.grapheme_break == GBP_SpacingMark && wcwidth_std(props) > 0);
}
static size_t
draw_combining_char_run(Screen *self, text_loop_state *s, char_type ch, CharProps props, const uint32_t *rest, size_t num_rest) {
// ch has been through grapheme segmentation, which added it to the previous
// cell. Plain combining chars directly following it are added along with it,
// with a single lookup in the text cache, rather than one for each char.
// Returns the number of chars from rest that were drawn.
#define MAX_RUN 32u
char_type run[MAX_RUN];
size_t n = 0;
GraphemeSegmentationResult seg = s->seg;
if (!batched_non_ascii_draw_disabled_for_tests && is_plain_combining_char(ch, seg, props)) {
run[n++] = ch;
for (size_t k = 0; k < num_rest && n < MAX_RUN; k++) {
const char_type c = map_char(self, rest[k]);
const CharProps p = char_props_for(c);
if (p.is_invalid) break;
const GraphemeSegmentationResult next = grapheme_segmentation_step(seg, p);
if (!next.add_to_current_cell || !is_plain_combining_char(c, next, p)) break;
seg = next;
run[n++] = c;
}
}
#undef MAX_RUN
if (n < 2) {
draw_combining_char(self, s, ch);
return 0;
}
CPUCell *cp;
GPUCell *gp;
linebuf_init_cells(self->linebuf, s->prev.y, &cp, &gp);
index_type xpos = s->prev.x;
while (xpos && cp[xpos].is_multicell && cp[xpos].x) xpos--;
add_combining_chars(self, run, n, xpos, s->prev.y);
s->seg = seg;
return n - 1;
}
static void
draw_control_char(Screen *self, text_loop_state *s, uint32_t ch) {
switch (ch) {
@ -1574,8 +1632,6 @@ draw_control_char(Screen *self, text_loop_state *s, uint32_t ch) {
}
}
static bool batched_non_ascii_draw_disabled_for_tests = false;
static_assert(sizeof(CPUCell) == 3 * sizeof(uint32_t), "draw_non_ascii_run() needs updating");
static inline uint32_t
@ -1793,7 +1849,7 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_
}
s->seg = grapheme_segmentation_step(s->seg, cp);
if (UNLIKELY(s->seg.add_to_current_cell && s->prev.cc)) {
draw_combining_char(self, s, ch);
i += draw_combining_char_run(self, s, ch, cp, chars + i + 1, num_chars - i - 1);
continue;
}
char_width = wcwidth_std(cp);

View file

@ -220,6 +220,10 @@ tc_gc_begin(TextCache *self) {
zero_at_ptr(&self->arena);
vt_cleanup(&self->map);
vt_init(&self->map);
// Avoid rehashing repeatedly as the live entries are re-inserted, the live
// set of the previous collection is the best estimate of their number.
// Failing to do so is harmless, the map grows as needed.
if (self->adds_between_gcs) (void)vt_reserve(&self->map, self->adds_between_gcs);
return gc;
}
@ -241,6 +245,10 @@ tc_gc_end(TextCache *self, TextCacheGCData *gc) {
self->adds_since_last_gc = 0;
// scale with the live set so remapping cost stays amortized
self->adds_between_gcs = self->array.count;
// Size the map for the entries that will be added before the next
// collection, to avoid rehashing repeatedly as it grows. Failing to do so
// is harmless, the map grows as needed.
(void)vt_reserve(&self->map, self->array.count + MAX(TEXT_CACHE_ADDS_BETWEEN_GCS, self->array.count));
free(gc->map);
free(gc->old_items);
Chars_free_all(&gc->old_arena);

View file

@ -975,6 +975,15 @@ class TestScreen(BaseTest):
'\x9b',
'￿',
'\U000e0001',
# runs of combining chars, including more than fit in a cell
'a\u0300\u0301',
'e\u0301\u0302\u0303\u0304',
'x' + '\u0300\u0310\u0320' * 10,
'日\u0300\u0301',
'\u0301\u0302',
'\u0300\ufe0f\u0301',
'👍\u0300\u200d',
'ก\u0e33\u0300',
)
def ops_for(seed, cols):
@ -1027,13 +1036,16 @@ class TestScreen(BaseTest):
return s
def compare(a, b, msg):
# Cells are compared by content rather than raw value, as cells with
# multiple codepoints store indices into the text cache, which can
# differ, since batched drawing does not add the intermediate texts
# of runs of combining chars to the cache.
self.ae((a.cursor.x, a.cursor.y), (b.cursor.x, b.cursor.y), msg)
self.ae(a.historybuf.count, b.historybuf.count, msg)
for i in range(a.historybuf.count):
self.assertTrue(a.historybuf.line(i) == b.historybuf.line(i), f'{msg} history line {i}')
self.ae(a.historybuf.line(i).as_ansi(), b.historybuf.line(i).as_ansi(), f'{msg} history line {i}')
self.ae(a.cpu_cells(-1 - i), b.cpu_cells(-1 - i), f'{msg} history line {i}')
for i in range(a.lines):
self.assertTrue(a.linebuf.line(i) == b.linebuf.line(i), f'{msg} line {i}')
self.ae(a.linebuf.line(i).as_ansi(), b.linebuf.line(i).as_ansi(), f'{msg} line {i}')
self.ae(a.cpu_cells(i), b.cpu_cells(i), f'{msg} line {i}')
self.ae(a.linebuf.is_continued(i), b.linebuf.is_continued(i), f'{msg} line {i}')