mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
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.
This commit is contained in:
parent
5a5ab55c76
commit
ecb5d925ff
5 changed files with 62 additions and 8 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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*/;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue