diff --git a/kitty/screen.c b/kitty/screen.c index 5309c47bb..3c74a30e4 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1343,9 +1343,12 @@ static void reset_style_table_if_unused(Screen *self) { // When the scrollback is empty and no cell has a style, empty the style // table, which frees the memory used by styles from, for example, a burst - // of output with many colors. Checking is cheap as only the screen - // buffers need to be scanned, so this is done on resets and erasing. - if (self->historybuf->count || self->style_table->count < 2) return; + // of output with many colors. Done only on hard resets and clearing of the + // scrollback, not on erasing the screen, as full screen programs erase the + // screen on every redraw, and resetting would then force all their styles + // to be interned again and the style texture to be uploaded again every + // frame. Small tables are not worth emptying for the same reason. + if (self->historybuf->count || !style_table_is_worth_resetting(self->style_table)) return; LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, self->paused_rendering.linebuf}; for (unsigned i = 0; i < arraysz(bufs); i++) { if (bufs[i] && cells_use_styles(bufs[i]->cpu_cell_buf, (size_t)bufs[i]->xnum * bufs[i]->ynum)) return; @@ -3420,6 +3423,7 @@ screen_clear_scrollback(Screen *self) { } } self->linebuf = orig; + reset_style_table_if_unused(self); } static void @@ -3521,7 +3525,7 @@ screen_erase_in_display(Screen *self, unsigned int how, bool private) { if (how == 1) linebuf_clear_attrs_and_dirty(self->linebuf, self->cursor->y); } if (how == 3 && self->linebuf == self->main_linebuf) { screen_clear_scrollback(self); } - if (how >= 2) reset_style_table_if_unused(self); + if (how == 3) reset_style_table_if_unused(self); } void diff --git a/kitty/style-table.c b/kitty/style-table.c index 5e9b57f5a..640ccdb4c 100644 --- a/kitty/style-table.c +++ b/kitty/style-table.c @@ -154,6 +154,12 @@ style_table_reset(CellStyleTable *pub) { self->degraded = false; } +bool +style_table_is_worth_resetting(const CellStyleTable *pub) { + const StyleTableImpl *self = (const StyleTableImpl *)pub; + return self->pub.count > MAX(1u, self->max_count / 64); +} + void style_table_set_max_count(CellStyleTable *pub, uint32_t max_count) { StyleTableImpl *self = (StyleTableImpl *)pub; diff --git a/kitty/style-table.h b/kitty/style-table.h index 9d585ea1f..82ddd8bf4 100644 --- a/kitty/style-table.h +++ b/kitty/style-table.h @@ -28,6 +28,9 @@ uint32_t style_table_intern(CellStyleTable *self, const CellStyle *style); // Remove all styles except the default style, invalidating all indices and freeing memory. // Must only be called when no cells use a style other than the default style. void style_table_reset(CellStyleTable *self); +// Whether the table has enough styles that emptying it frees a worthwhile amount of +// memory, a small fraction of the maximum number of styles +bool style_table_is_worth_resetting(const CellStyleTable *self); // For testing void style_table_set_max_count(CellStyleTable *self, uint32_t max_count); uint32_t style_table_max_count(const CellStyleTable *self); diff --git a/kitty_tests/styles.py b/kitty_tests/styles.py index 6bfaf40cb..f8ef4d37d 100644 --- a/kitty_tests/styles.py +++ b/kitty_tests/styles.py @@ -328,16 +328,30 @@ class TestStyles(BaseTest): def test_style_table_reset_when_unused(self): s = self.create_screen(cols=6, lines=3, scrollback=4) - for i in range(5): - parse_bytes(s, f'\x1b[38;5;{i + 1}m'.encode()) - s.draw('x') + + def add_styles(): + for i in range(5): + parse_bytes(s, f'\x1b[38;5;{i + 1}m'.encode()) + s.draw('x') + parse_bytes(s, RESET) + + # Small tables are not worth resetting + add_styles() + s.erase_in_display(3) + self.ae(s.style_table_info()['count'], 6) + # Lower the threshold, a small fraction of the limit + s.test_set_style_table_limit(64) info = s.style_table_info() - self.ae(info['count'], 6) - # Erasing with a style still in use on screen does not reset + # Erasing the screen never resets, as full screen programs do it on every redraw s.erase_in_display(2) self.ae(s.style_table_info()['count'], 6) + # Erasing with a style still in use on screen does not reset + add_styles() + parse_bytes(s, RED_BOLD) + s.erase_in_display(3) + self.ae(s.style_table_info()['count'], 7) parse_bytes(s, RESET) - s.erase_in_display(2) + s.erase_in_display(3) info2 = s.style_table_info() self.ae(info2['count'], 1) self.assertNotEqual(info2['generation'], info['generation']) @@ -348,8 +362,12 @@ class TestStyles(BaseTest): s.carriage_return(), s.linefeed() parse_bytes(s, RESET) s.erase_in_display(2) + # In the alternate screen, erasing does not clear the scrollback + parse_bytes(s, b'\x1b[?1049h\x1b[3J') self.assertGreater(s.style_table_info()['count'], 1) - s.erase_in_display(3) + parse_bytes(s, b'\x1b[?1049l') + # Clearing the scrollback resets + s.clear_scrollback() self.ae(s.style_table_info()['count'], 1) # A hard reset parse_bytes(s, RED_BOLD)