mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-07-31 04:39:01 +00:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user