Skip to content

Commit 00eb63f

Browse files
committed
feat(pool, zc): try remap before resize
1 parent 019a81d commit 00eb63f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/core/pool.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ pub fn Pool(comptime T: type) type {
101101
const old_slice = self.items;
102102
const new_size = std.math.ceilPowerOfTwoAssert(usize, self.items.len + 1);
103103

104-
if (self.allocator.resize(self.items, new_size)) {
104+
if (self.allocator.remap(self.items, new_size)) |new_slice| {
105+
self.items = new_slice;
106+
} else if (self.allocator.resize(self.items, new_size)) {
105107
self.items = self.items.ptr[0..new_size];
106108
} else {
107109
const new_slice = try self.allocator.alloc(T, new_size);

src/core/zero_copy.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ pub fn ZeroCopy(comptime T: type) type {
5454
const old_slice = self.ptr[0..self.capacity];
5555
const new_size = try std.math.ceilPowerOfTwo(usize, self.capacity + size);
5656

57-
if (self.allocator.resize(self.ptr[0..self.capacity], new_size)) {
57+
if (self.allocator.remap(self.ptr[0..self.capacity], new_size)) |new| {
58+
self.ptr = new.ptr;
59+
self.capacity = new.len;
60+
} else if (self.allocator.resize(self.ptr[0..self.capacity], new_size)) {
5861
self.capacity = new_size;
5962
} else {
6063
const new_slice = try self.allocator.alloc(T, new_size);

0 commit comments

Comments
 (0)