From 5caf20e3e580dbda86fb3bba73211194843af757 Mon Sep 17 00:00:00 2001 From: Uzair Aftab Date: Sun, 26 Jul 2026 14:06:27 +0200 Subject: [PATCH] terminal: avoid reallocating tabstop storage Resizing tabstops to an already-supported width previously allocated and copied an equally sized dynamic buffer because the capacity check excluded equality. Treat an exactly sized buffer as sufficient, avoiding the temporary allocation and copy. Add a fixed-buffer regression test so an unnecessary second allocation fails the test. --- src/terminal/Tabstops.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/terminal/Tabstops.zig b/src/terminal/Tabstops.zig index 8dbe67893..8955447f2 100644 --- a/src/terminal/Tabstops.zig +++ b/src/terminal/Tabstops.zig @@ -140,7 +140,7 @@ pub fn resize( // What we need in the dynamic size const size = cols - prealloc_columns; - if (size < self.dynamic_stops.len) { + if (size <= self.dynamic_stops.len) { self.cols = cols; return; } @@ -221,6 +221,15 @@ test "Tabstops: dynamic allocations" { try testing.expect(!t.get(5)); } +test "Tabstops: resize to existing capacity does not allocate" { + var backing: [prealloc_columns]Unit = undefined; + var fixed = std.heap.FixedBufferAllocator.init(&backing); + var t: Tabstops = .{}; + + try t.resize(fixed.allocator(), prealloc_columns * 2); + try t.resize(fixed.allocator(), prealloc_columns * 2); +} + test "Tabstops: interval" { var t: Tabstops = try init(testing.allocator, 80, 4); defer t.deinit(testing.allocator);