diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-09-21 04:55:39 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-21 04:55:39 -0700 |
| commit | 594cb38fcb6c2edccee6b2fd74adec81b3be547a (patch) | |
| tree | c8a29f062fac286cfef622737affaaadc4ff4d73 /lib/std/multi_array_list.zig | |
| parent | 010d9a63f20d8a4bd14cff0ada690b2d127a0371 (diff) | |
| parent | 3cc0fc601af62adebf6b9f3bb1a241fa8a501d0b (diff) | |
| download | zig-594cb38fcb6c2edccee6b2fd74adec81b3be547a.tar.gz zig-594cb38fcb6c2edccee6b2fd74adec81b3be547a.zip | |
Merge pull request #25302 from ziglang/growCapacity
std: remove loop from growCapacity
Diffstat (limited to 'lib/std/multi_array_list.zig')
| -rw-r--r-- | lib/std/multi_array_list.zig | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index e4eb60bd93..ec4b0b72e1 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -457,24 +457,19 @@ pub fn MultiArrayList(comptime T: type) type { /// Invalidates element pointers if additional memory is needed. pub fn ensureTotalCapacity(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void { if (self.capacity >= new_capacity) return; - return self.setCapacity(gpa, growCapacity(self.capacity, new_capacity)); + return self.setCapacity(gpa, growCapacity(new_capacity)); } - const init_capacity = init: { - var max = 1; - for (fields) |field| max = @as(comptime_int, @max(max, @sizeOf(field.type))); - break :init @as(comptime_int, @max(1, std.atomic.cache_line / max)); + const init_capacity: comptime_int = init: { + var max: comptime_int = 1; + for (fields) |field| max = @max(max, @sizeOf(field.type)); + break :init @max(1, std.atomic.cache_line / max); }; - /// Called when memory growth is necessary. Returns a capacity larger than - /// minimum that grows super-linearly. - fn growCapacity(current: usize, minimum: usize) usize { - var new = current; - while (true) { - new +|= new / 2 + init_capacity; - if (new >= minimum) - return new; - } + /// Given a lower bound of required memory capacity, returns a larger value + /// with super-linear growth. + pub fn growCapacity(minimum: usize) usize { + return minimum +| (minimum / 2 + init_capacity); } /// Modify the array so that it can hold at least `additional_count` **more** items. |
