diff --git a/kitty/history.c b/kitty/history.c index 488d41cf6..22077c914 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -711,10 +711,9 @@ historybuf_next_dest_line(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *src_line index_type idx = historybuf_push(self, as_ansi_buf, &needs_clear); *attrptr(self, idx) = src_line->attrs; init_line(self, idx, dest_line); - if (needs_clear) { - zero_at_ptr_count(dest_line->cpu_cells, dest_line->xnum); - zero_at_ptr_count(dest_line->gpu_cells, dest_line->xnum); - } + // the slot may hold a stale line from stolen segments + zero_at_ptr_count(dest_line->cpu_cells, dest_line->xnum); + zero_at_ptr_count(dest_line->gpu_cells, dest_line->xnum); return dest_y + 1; } @@ -739,14 +738,13 @@ historybuf_finish_rewrap(HistoryBuf *dest, HistoryBuf *src) { } void -historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src) { - for (index_type i = 0; i < src->num_segments; i++) { - memcpy(dest->segments[i].cpu_cells, src->segments[i].cpu_cells, SEGMENT_SIZE * src->xnum * sizeof(CPUCell)); - memcpy(dest->segments[i].gpu_cells, src->segments[i].gpu_cells, SEGMENT_SIZE * src->xnum * sizeof(GPUCell)); - memcpy(dest->segments[i].line_attrs, src->segments[i].line_attrs, SEGMENT_SIZE * sizeof(LineAttrs)); - } - dest->count = src->count; +historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count) { + SWAP(dest->segments, src->segments); + SWAP(dest->num_segments, src->num_segments); + dest->count = MIN(count, src->count); dest->start_of_data = src->start_of_data; + src->count = 0; + src->start_of_data = 0; } @@ -760,7 +758,11 @@ rewrap(HistoryBuf *self, PyObject *args) { RAII_PyObject(cleanup, (PyObject *)dummy); (void)cleanup; TrackCursor cursors[1] = {{.is_sentinel = true}}; + // self must keep its segments + bool orig = resize_disable_history_reuse; + resize_disable_history_reuse = true; ResizeResult r = resize_screen_buffers(dummy, self, 8, xnum, &as_ansi_buf, cursors); + resize_disable_history_reuse = orig; free(as_ansi_buf.buf); if (!r.ok) return PyErr_NoMemory(); Py_CLEAR(r.lb); diff --git a/kitty/history.h b/kitty/history.h index 9c7cf2f58..057bf803c 100644 --- a/kitty/history.h +++ b/kitty/history.h @@ -40,7 +40,7 @@ typedef struct { HistoryBuf *alloc_historybuf(unsigned int, unsigned int, unsigned int, TextCache *tc); HistoryBuf *historybuf_alloc_for_rewrap(unsigned int columns, HistoryBuf *self); void historybuf_finish_rewrap(HistoryBuf *dest, HistoryBuf *src); -void historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src); +void historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count); index_type historybuf_next_dest_line(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *src_line, index_type dest_y, Line *dest_line, bool continued); bool historybuf_is_line_continued(HistoryBuf *self, index_type lnum); void historybuf_delete_newest_lines(HistoryBuf *self, index_type count); diff --git a/kitty/resize.c b/kitty/resize.c index 5ef1777f0..be0782dfc 100644 --- a/kitty/resize.c +++ b/kitty/resize.c @@ -275,7 +275,7 @@ rewrap(Rewrap *r) { memcpy(r->dest.lb->cpu_cell_buf, r->src.lb->cpu_cell_buf, (size_t)r->src.lb->xnum * r->src.lb->ynum * sizeof(CPUCell)); memcpy(r->dest.lb->gpu_cell_buf, r->src.lb->gpu_cell_buf, (size_t)r->src.lb->xnum * r->src.lb->ynum * sizeof(GPUCell)); r->num_content_lines_before = r->src.lb->ynum; - if (r->dest.hb && r->src.hb) historybuf_fast_rewrap(r->dest.hb, r->src.hb); + if (r->dest.hb && r->src.hb) historybuf_fast_rewrap(r->dest.hb, r->src.hb, r->src.hb->count); r->dest.y = r->src.lb->ynum - 1; return; } @@ -297,6 +297,42 @@ rewrap(Rewrap *r) { } } +bool resize_disable_history_reuse = false; + +static index_type +reusable_history_lines(HistoryBuf *hb) { + // count of oldest lines that rewrap to themselves at the same width + index_type xnum = hb->xnum, ans = 0; + bool prev_wrapped = false, prev_full = false; + Line l = {.xnum = xnum, .text_cache = hb->text_cache}; + for (index_type y = 0; y + 1 < hb->count; y++) { + historybuf_init_line(hb, hb->count - y - 1, &l); + CPUCell *c = l.cpu_cells; + index_type limit = xnum; + while (limit && c[limit - 1].ch_and_idx == BLANK_CHAR) limit--; + if (prev_wrapped && (!prev_full || !limit)) return ans; + if (limit && c[limit - 1].is_multicell && c[limit - 1].x != mcd_x_limit(c + limit - 1) - 1) return ans; + for (index_type x = 0; x < limit; x++) + if (c[x].is_multicell && c[x].scale > 1) return ans; + prev_wrapped = c[xnum - 1].next_char_was_wrapped; + prev_full = limit == xnum; + if (!prev_wrapped) ans = y + 1; + } + return ans; +} + +static void +clear_trailing_blanks(HistoryBuf *hb, index_type count) { + Line l = {.xnum = hb->xnum, .text_cache = hb->text_cache}; + for (index_type y = 0; y < count; y++) { + historybuf_init_line(hb, hb->count - y - 1, &l); + index_type limit = l.xnum; + while (limit && l.cpu_cells[limit - 1].ch_and_idx == BLANK_CHAR) limit--; + zero_at_ptr_count(l.cpu_cells + limit, l.xnum - limit); + zero_at_ptr_count(l.gpu_cells + limit, l.xnum - limit); + } +} + ResizeResult resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type columns, ANSIBuf *as_ansi_buf, TrackCursor *cursors) { ResizeResult ans = {0}; @@ -304,22 +340,43 @@ resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type if (!ans.lb) return ans; RAII_PyObject(raii_nlb, (PyObject *)ans.lb); (void)raii_nlb; + LineBuf *sb = alloc_linebuf(SCALE_BITS << 1, columns, lb->text_cache); + if (!sb) return ans; + RAII_PyObject(scratch, (PyObject *)sb); + (void)scratch; + HistoryBuf *src_hb = hb; + index_type reused = 0; + RAII_PyObject(raii_tail, NULL); if (hb) { + if (columns == lb->xnum && lines != lb->ynum && !resize_disable_history_reuse) reused = reusable_history_lines(hb); + // copying a long tail would cost more than a plain rewrap + if (reused < hb->count - reused) reused = 0; + if (reused) { + src_hb = alloc_historybuf(hb->count - reused, columns, 0, hb->text_cache); + if (!src_hb) return ans; + raii_tail = (PyObject *)src_hb; + Line l = {.xnum = columns, .text_cache = hb->text_cache}; + for (index_type y = reused; y < hb->count; y++) { + historybuf_init_line(hb, hb->count - y - 1, &l); + historybuf_add_line(src_hb, &l, as_ansi_buf); + } + } ans.hb = historybuf_alloc_for_rewrap(columns, hb); if (!ans.hb) return ans; + if (reused) { + clear_trailing_blanks(hb, reused); + historybuf_fast_rewrap(ans.hb, hb, reused); + } } RAII_PyObject(raii_nhb, (PyObject *)ans.hb); (void)raii_nhb; Rewrap r = { - .src = {.lb = lb, .hb = hb}, - .dest = {.lb = ans.lb, .hb = ans.hb}, + .src = {.lb = lb, .hb = src_hb}, + .dest = {.lb = ans.lb, .hb = ans.hb, .y = reused}, .as_ansi_buf = as_ansi_buf, .cursors = cursors, + .sb = sb, }; - r.sb = alloc_linebuf(SCALE_BITS << 1, columns, lb->text_cache); - if (!r.sb) return ans; - RAII_PyObject(scratch, (PyObject *)r.sb); - (void)scratch; for (TrackCursor *t = cursors; !t->is_sentinel; t++) { t->dest_x = t->x; t->dest_y = t->y; diff --git a/kitty/resize.h b/kitty/resize.h index a2879552c..ea38484f2 100644 --- a/kitty/resize.h +++ b/kitty/resize.h @@ -22,5 +22,6 @@ typedef struct ResizeResult { index_type num_content_lines_before, num_content_lines_after; } ResizeResult; +extern bool resize_disable_history_reuse; ResizeResult resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type columns, ANSIBuf *as_ansi_buf, TrackCursor *cursors); ResizeResult resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type columns, TrackCursor *cursors); diff --git a/kitty/screen.c b/kitty/screen.c index 92e34f623..0d883ab71 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -310,21 +310,7 @@ rewrap( bool main_is_active) { TrackCursor cursors[3]; cursors[2].is_sentinel = true; - cursors[0] = (TrackCursor){.x = main_saved_cursor->before.x, .y = main_saved_cursor->before.y}; - if (main_is_active) cursors[1] = (TrackCursor){.x = cursor->before.x, .y = cursor->before.y}; - else cursors[1].is_sentinel = true; - ResizeResult mr = resize_screen_buffers(screen->main_linebuf, screen->historybuf, lines, columns, &screen->as_ansi_buf, cursors); - if (!mr.ok) { - PyErr_NoMemory(); - return false; - } - main_saved_cursor->temp.x = cursors[0].dest_x; - main_saved_cursor->temp.y = cursors[0].dest_y; - if (main_is_active) { - cursor->temp.x = cursors[1].dest_x; - cursor->temp.y = cursors[1].dest_y; - } - + // alt first to keep history intact on failure cursors[0] = (TrackCursor){.x = alt_saved_cursor->before.x, .y = alt_saved_cursor->before.y}; if (!main_is_active) cursors[1] = (TrackCursor){.x = cursor->before.x, .y = cursor->before.y}; else cursors[1].is_sentinel = true; @@ -340,6 +326,22 @@ rewrap( cursor->temp.x = cursors[1].dest_x; cursor->temp.y = cursors[1].dest_y; } + + cursors[0] = (TrackCursor){.x = main_saved_cursor->before.x, .y = main_saved_cursor->before.y}; + if (main_is_active) cursors[1] = (TrackCursor){.x = cursor->before.x, .y = cursor->before.y}; + else cursors[1].is_sentinel = true; + ResizeResult mr = resize_screen_buffers(screen->main_linebuf, screen->historybuf, lines, columns, &screen->as_ansi_buf, cursors); + if (!mr.ok) { + Py_DecRef((PyObject *)ar.lb); + PyErr_NoMemory(); + return false; + } + main_saved_cursor->temp.x = cursors[0].dest_x; + main_saved_cursor->temp.y = cursors[0].dest_y; + if (main_is_active) { + cursor->temp.x = cursors[1].dest_x; + cursor->temp.y = cursors[1].dest_y; + } Py_CLEAR(screen->main_linebuf); Py_CLEAR(screen->alt_linebuf); Py_CLEAR(screen->historybuf); @@ -7265,10 +7267,17 @@ PyTypeObject Screen_Type = { .tp_getset = getsetters, }; +static PyObject * +test_set_history_reuse(PyObject *self UNUSED, PyObject *val) { + resize_disable_history_reuse = !PyObject_IsTrue(val); + Py_RETURN_NONE; +} + static PyMethodDef module_methods[] = { {"is_emoji_presentation_base", (PyCFunction)screen_is_emoji_presentation_base, METH_O, ""}, {"truncate_point_for_length", (PyCFunction)screen_truncate_point_for_length, METH_VARARGS, ""}, {"test_ch_and_idx", test_ch_and_idx, METH_O, ""}, + {"test_set_history_reuse", test_set_history_reuse, METH_O, ""}, {NULL} /* Sentinel */ }; diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 136b3d4c9..e9b5aee7c 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -710,6 +710,90 @@ class TestScreen(BaseTest): s.resize(s.lines + 2, s.columns) assert_lines('xxxxx', 'xxxxx', 'xxxxx', 'xxxxx', 'xxxxx', 'abcde', '>') + def test_height_resize_reuses_history(self): + import random + + from kitty.fast_data_types import test_set_history_reuse + + def ops_for(seed, cols): + rng = random.Random(seed) + chars = 'abc 日😀' if seed % 2 else 'abcdef ' + ops = [] + for i in range(rng.randint(20, 80)): + r = rng.random() + if r < 0.35: + ops.append(('draw', ''.join(rng.choice(chars) for _ in range(rng.randint(0, (3 if seed % 3 else 1) * cols))))) + elif r < 0.45: + ops.append(('bytes', f'\x1b[4{rng.randint(1, 7)}m{rng.choice("xyz")}\x1b[K\x1b[m'.encode())) + elif r < 0.5: + if seed % 4 == 0: + ops.append(('mc', rng.choice('AB日'), rng.randint(2, 3))) + elif r < 0.55: + ops.append(('bytes', rng.choice((b'\x1b]133;A\x07', b'\x1b]133;A;k=s\x07', b'\x1b]133;C\x07')))) + elif r < 0.6: + ops.append(('bytes', b'\x1b7')) + elif r < 0.65: + ops.append(('bytes', f'\x1b[{rng.randint(1, 3)}C'.encode())) + else: + ops.append(('nl',)) + if rng.random() < 0.5: + ops.append(('draw', 'x' * rng.randint(0, cols))) + return ops + + def build(ops, cols, lines, scrollback, options): + s = self.create_screen(cols=cols, lines=lines, scrollback=scrollback, options=options) + for op in ops: + if op[0] == 'draw': + s.draw(op[1]) + elif op[0] == 'bytes': + parse_bytes(s, op[1]) + elif op[0] == 'mc': + draw_multicell(s, op[1], scale=op[2]) + else: + s.carriage_return(), s.linefeed() + return s + + def compare(a, b, msg): + self.ae((a.lines, a.columns, a.cursor.x, a.cursor.y), (b.lines, b.columns, 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.historybuf.is_continued(i), b.historybuf.is_continued(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.linebuf.is_continued(i), b.linebuf.is_continued(i), f'{msg} line {i}') + self.ae(a.linebuf.dirty_lines(), b.linebuf.dirty_lines(), msg) + self.ae(a.historybuf.pagerhist_as_text(), b.historybuf.pagerhist_as_text(), msg) + parse_bytes(a, b'\x1b8'), parse_bytes(b, b'\x1b8') + self.ae((a.cursor.x, a.cursor.y), (b.cursor.x, b.cursor.y), f'{msg} saved cursor') + + try: + for seed in range(300): + rng = random.Random(seed) + cols, lines = rng.randint(3, 12), rng.randint(2, 8) + scrollback = rng.choice((4, 8, 30, 200)) + options = { + 'scrollback_fill_enlarged_window': rng.random() < 0.5, + 'scrollback_pager_history_size': rng.choice((0, 1024)), + } + ops = ops_for(seed, cols) + heights = [rng.randint(1, 12) for _ in range(4)] + test_set_history_reuse(True) + a = build(ops, cols, lines, scrollback, options) + test_set_history_reuse(False) + b = build(ops, cols, lines, scrollback, options) + for h in heights: + msg = f'seed={seed} height={h}' + test_set_history_reuse(True) + a.resize(h, cols) + test_set_history_reuse(False) + b.resize(h, cols) + compare(a, b, msg) + finally: + test_set_history_reuse(True) + def test_tab_stops(self): # Taken from vttest/main.c s = self.create_screen(cols=80, lines=2)