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.
This commit is contained in:
Uzair Aftab
2026-07-26 14:06:27 +02:00
parent 2de5e7d38e
commit 5caf20e3e5

View File

@@ -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);