From ecb5d925fff4f90c683b90c531d09de7ca181833 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Nov 2026 19:20:07 +0530 Subject: [PATCH] Use a compact per char draw info table in the batched draw path Decoding the packed bitfields of CharProps took about half the instructions of the batched non-ASCII draw loop. Precompute, for each distinct CharProps value, the number of cells a char occupies and its grapheme segmentation property, and use that in the loop instead. This speeds up the unicode benchmark by ~8%. Also add test cases for a batched run that stops at an existing multicell char after an unpaired regional indicator, which the random test cases did not cover. --- kitty/char-props.c | 28 ++++++++++++++++++++++++++++ kitty/char-props.h | 11 +++++++++++ kitty/data-types.c | 1 + kitty/screen.c | 13 +++++-------- kitty_tests/screen.py | 17 +++++++++++++++++ 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/kitty/char-props.c b/kitty/char-props.c index af5a74817..6e64188d5 100644 --- a/kitty/char-props.c +++ b/kitty/char-props.c @@ -23,6 +23,26 @@ char_props_for(char_type ch) { return CharProps_t3[CharProps_t2[(CharProps_t1[ch >> CharProps_shift] << CharProps_shift) + (ch & CharProps_mask)]]; } +static CharDrawInfo char_draw_info_table[arraysz(CharProps_t3)]; + +void +init_char_props(void) { + for (size_t i = 0; i < arraysz(CharProps_t3); i++) { + const CharProps p = CharProps_t3[i]; + int width = wcwidth_std(p); + if (width < 0) width = 1; // East Asian ambiguous width + // Only chars of width one or two are drawn by the batched draw path + char_draw_info_table[i] = + (CharDrawInfo){.cells = (p.is_invalid || width > 2) ? 0 : (uint8_t)width, .grapheme_segmentation_property = p.grapheme_segmentation_property}; + } +} + +CharDrawInfo +char_draw_info_for(char_type ch) { + ch = ensure_char_in_range(ch); + return char_draw_info_table[CharProps_t2[(CharProps_t1[ch >> CharProps_shift] << CharProps_shift) + (ch & CharProps_mask)]]; +} + void grapheme_segmentation_reset(GraphemeSegmentationResult *s) { s->val = 0; @@ -36,3 +56,11 @@ grapheme_segmentation_step(GraphemeSegmentationResult r, CharProps ch) { // printf("state: %u gsp: %u -> key: %u t1: %u -> add_to_cell: %u\n", r.state, ch.grapheme_segmentation_property, key, t1, ans.add_to_current_cell); return ans; } + +GraphemeSegmentationResult +grapheme_segmentation_step_for_property(GraphemeSegmentationResult r, uint8_t grapheme_segmentation_property) { + // The step depends only on the grapheme segmentation property of the char + CharProps ch = {0}; + ch.grapheme_segmentation_property = grapheme_segmentation_property; + return grapheme_segmentation_step(r, ch); +} diff --git a/kitty/char-props.h b/kitty/char-props.h index a1d2174dd..39ec3c56b 100644 --- a/kitty/char-props.h +++ b/kitty/char-props.h @@ -163,6 +163,17 @@ typedef enum UnicodeCategory { CharProps char_props_for(char_type ch); void grapheme_segmentation_reset(GraphemeSegmentationResult *s); GraphemeSegmentationResult grapheme_segmentation_step(GraphemeSegmentationResult r, CharProps ch); +GraphemeSegmentationResult grapheme_segmentation_step_for_property(GraphemeSegmentationResult r, uint8_t grapheme_segmentation_property); + +// The parts of CharProps needed to draw a char, cheaper to look up and use than +// CharProps itself. cells is the number of cells the char occupies, with East Asian +// ambiguous width chars being narrow, or zero for chars that are invalid, zero width or +// wider than two cells. +typedef struct CharDrawInfo { + uint8_t cells, grapheme_segmentation_property; +} CharDrawInfo; +CharDrawInfo char_draw_info_for(char_type ch); +void init_char_props(void); static inline int wcwidth_std(CharProps ch) { return (int)ch.shifted_width - 4 /*=width_shift*/; diff --git a/kitty/data-types.c b/kitty/data-types.c index bd8059f9e..03e98ef28 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -946,6 +946,7 @@ PyInit_fast_data_types(void) { m = PyModule_Create(&module); if (m == NULL) return NULL; init_monotonic(); + init_char_props(); if (!init_logging(m)) return NULL; if (!init_LineBuf(m)) return NULL; diff --git a/kitty/screen.c b/kitty/screen.c index 4731b46d0..274e764f8 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1628,16 +1628,13 @@ draw_non_ascii_run(Screen *self, text_loop_state *s, const uint32_t *chars, size for (; i < num_chars; i++) { const char_type ch = chars[i]; if (ch < DEL || ch == IMAGE_PLACEHOLDER_CHAR) break; - const CharProps props = char_props_for(ch); - if (props.is_invalid) break; - // Negative widths are East Asian ambiguous width chars, which are drawn as narrow - int width = wcwidth_std(props); - if (width < 0) width = 1; - if (width == 0 || x + (index_type)width > columns || cp[x].is_multicell) break; + const CharDrawInfo info = char_draw_info_for(ch); + const index_type width = info.cells; + if (width == 0 || x + width > columns || cp[x].is_multicell) break; if (width == 2 && cp[x + 1].is_multicell) break; - const uint32_t seg_key = ((uint32_t)seg.val << 8) | props.grapheme_segmentation_property; + const uint32_t seg_key = ((uint32_t)seg.val << 8) | info.grapheme_segmentation_property; if (seg_key != cached_seg_key) { - cached_seg = grapheme_segmentation_step(seg, props); + cached_seg = grapheme_segmentation_step_for_property(seg, info.grapheme_segmentation_property); cached_seg_key = seg_key; } if (cached_seg.add_to_current_cell) break; diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 2c45432a0..3e9a4f058 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -1039,7 +1039,24 @@ class TestScreen(BaseTest): self.ae(a.linebuf.is_continued(i), b.linebuf.is_continued(i), f'{msg} line {i}') self.ae(a.linebuf.dirty_lines(), b.linebuf.dirty_lines(), msg) + # A run drawn up to an existing multicell char must leave the grapheme + # segmentation state as it is after the last char drawn: here an unpaired + # regional indicator precedes the run and another one is drawn onto the + # wide char by the scalar path + ri_a, ri_b = '\U0001f1fa', '\U0001f1f8' + fixed = [] + for prefix in ('', 'é', 'éü', '日'): + for x in range(1, 6): + fixed.append( + (10, 2, [('bytes', f'\x1b[1;{x + 1}H'.encode()), ('draw', '日'), ('bytes', b'\x1b[H'), ('draw', ri_a), ('draw', prefix + ri_b + 'é')]) + ) try: + for i, (cols, lines, ops) in enumerate(fixed): + test_set_batched_non_ascii_draw(True) + a = build(ops, cols, lines) + test_set_batched_non_ascii_draw(False) + b = build(ops, cols, lines) + compare(a, b, f'fixed={i} ops={ops}') for seed in range(400): rng = random.Random(seed) cols, lines = rng.randint(1, 14), rng.randint(1, 6)