From 060740196752fbb393fe546678bc843ab697274d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 9 Oct 2026 10:48:50 +0530 Subject: [PATCH] Make history consumption on resize an explicit parameter resize_screen_buffers() moves the storage of the source history buffer into the result, both when reusing unchanged lines and in the same size fast path. Replace the global used to opt out of this with an explicit HistoryResizeMode argument so callers that must keep their history (HistoryBuf.rewrap) state it, and make the same size fast path copy rather than steal in that mode. Document that nothing may fail after the history has been consumed. --- kitty/history.c | 25 +++++++++++++++---------- kitty/history.h | 2 +- kitty/line-buf.c | 2 +- kitty/resize.c | 22 +++++++++++++--------- kitty/resize.h | 14 ++++++++++++-- kitty/screen.c | 16 +++++++++++++--- kitty_tests/datatypes.py | 1 + 7 files changed, 56 insertions(+), 26 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index 22077c914..76fd36508 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -738,13 +738,22 @@ historybuf_finish_rewrap(HistoryBuf *dest, HistoryBuf *src) { } void -historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count) { - SWAP(dest->segments, src->segments); - SWAP(dest->num_segments, src->num_segments); +historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count, bool steal) { + // dest must have been created by historybuf_alloc_for_rewrap() with the same width as src dest->count = MIN(count, src->count); dest->start_of_data = src->start_of_data; - src->count = 0; - src->start_of_data = 0; + if (steal) { + SWAP(dest->segments, src->segments); + SWAP(dest->num_segments, src->num_segments); + src->count = 0; + src->start_of_data = 0; + } else { + 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)); + } + } } @@ -758,11 +767,7 @@ 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; + ResizeResult r = resize_screen_buffers(dummy, self, HISTORY_PRESERVE, 8, xnum, &as_ansi_buf, cursors); 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 057bf803c..54cfb8def 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, index_type count); +void historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count, bool steal); 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/line-buf.c b/kitty/line-buf.c index 8689043b4..5cddb2715 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -628,7 +628,7 @@ rewrap(LineBuf *self, PyObject *args) { if (!PyArg_ParseTuple(args, "II", &lines, &columns)) return NULL; TrackCursor cursors[1] = {{.is_sentinel = true}}; ANSIBuf as_ansi_buf = {0}; - ResizeResult r = resize_screen_buffers(self, NULL, lines, columns, &as_ansi_buf, cursors); + ResizeResult r = resize_screen_buffers(self, NULL, HISTORY_PRESERVE, lines, columns, &as_ansi_buf, cursors); free(as_ansi_buf.buf); if (!r.ok) return PyErr_NoMemory(); return Py_BuildValue("NII", r.lb, r.num_content_lines_before, r.num_content_lines_after); diff --git a/kitty/resize.c b/kitty/resize.c index be0782dfc..94445a70a 100644 --- a/kitty/resize.c +++ b/kitty/resize.c @@ -29,6 +29,7 @@ typedef struct Rewrap { index_type num_content_lines_before, src_x_limit; bool prev_src_line_ended_with_wrap, current_src_line_has_multline_cells, current_dest_line_has_multiline_cells; bool dest_line_from_linebuf, src_is_in_linebuf; + bool consume_src_hb; } Rewrap; @@ -275,7 +276,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, r->src.hb->count); + if (r->dest.hb && r->src.hb) historybuf_fast_rewrap(r->dest.hb, r->src.hb, r->src.hb->count, r->consume_src_hb); r->dest.y = r->src.lb->ynum - 1; return; } @@ -297,8 +298,6 @@ 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 @@ -334,7 +333,8 @@ clear_trailing_blanks(HistoryBuf *hb, index_type count) { } ResizeResult -resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type columns, ANSIBuf *as_ansi_buf, TrackCursor *cursors) { +resize_screen_buffers( + LineBuf *lb, HistoryBuf *hb, HistoryResizeMode hb_mode, index_type lines, index_type columns, ANSIBuf *as_ansi_buf, TrackCursor *cursors) { ResizeResult ans = {0}; ans.lb = alloc_linebuf(lines, columns, lb->text_cache); if (!ans.lb) return ans; @@ -348,7 +348,7 @@ resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type 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); + if (hb_mode == HISTORY_CONSUME && columns == lb->xnum && lines != lb->ynum) 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) { @@ -363,10 +363,13 @@ resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type } 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); - } + } + // Nothing below this point may fail, as hb is modified and, with + // HISTORY_CONSUME, its storage is moved into ans.hb, both here and in the + // fast path of rewrap(). All allocations must be done above. + if (reused) { + clear_trailing_blanks(hb, reused); + historybuf_fast_rewrap(ans.hb, hb, reused, true); } RAII_PyObject(raii_nhb, (PyObject *)ans.hb); (void)raii_nhb; @@ -376,6 +379,7 @@ resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, index_type lines, index_type .as_ansi_buf = as_ansi_buf, .cursors = cursors, .sb = sb, + .consume_src_hb = hb_mode != HISTORY_PRESERVE, }; for (TrackCursor *t = cursors; !t->is_sentinel; t++) { t->dest_x = t->x; diff --git a/kitty/resize.h b/kitty/resize.h index ea38484f2..def0c1979 100644 --- a/kitty/resize.h +++ b/kitty/resize.h @@ -22,6 +22,16 @@ 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); +typedef enum HistoryResizeMode { + // hb is never modified + HISTORY_PRESERVE, + // on success the storage of hb may be moved into the result, leaving hb + // empty, so hb must be discarded. On failure hb is left untouched. + HISTORY_CONSUME, + // as HISTORY_CONSUME but never reuse unchanged lines, used in tests + HISTORY_CONSUME_NO_REUSE, +} HistoryResizeMode; + +ResizeResult +resize_screen_buffers(LineBuf *lb, HistoryBuf *hb, HistoryResizeMode hb_mode, 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 0d883ab71..7973da553 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -297,6 +297,8 @@ typedef struct CursorTrack { } temp; } CursorTrack; +static bool history_reuse_disabled_for_tests = false; + static bool rewrap( Screen *screen, @@ -310,7 +312,8 @@ rewrap( bool main_is_active) { TrackCursor cursors[3]; cursors[2].is_sentinel = true; - // alt first to keep history intact on failure + // alt first as resizing main consumes the history buffer, so it must be + // the last step that can fail 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; @@ -330,7 +333,14 @@ rewrap( 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); + ResizeResult mr = resize_screen_buffers( + screen->main_linebuf, + screen->historybuf, + history_reuse_disabled_for_tests ? HISTORY_CONSUME_NO_REUSE : HISTORY_CONSUME, + lines, + columns, + &screen->as_ansi_buf, + cursors); if (!mr.ok) { Py_DecRef((PyObject *)ar.lb); PyErr_NoMemory(); @@ -7269,7 +7279,7 @@ PyTypeObject Screen_Type = { static PyObject * test_set_history_reuse(PyObject *self UNUSED, PyObject *val) { - resize_disable_history_reuse = !PyObject_IsTrue(val); + history_reuse_disabled_for_tests = !PyObject_IsTrue(val); Py_RETURN_NONE; } diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index 44631f2f8..2f37fc797 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -867,6 +867,7 @@ class TestDataTypes(BaseTest): hb = filled_history_buf(5, 5) hb2 = hb.rewrap(hb.xnum) + self.ae((hb.count, hb2.count), (hb.ynum, hb.ynum)) for i in range(hb.ynum): self.ae(hb2.line(i), hb.line(i)) hb = filled_history_buf(5, 5)