Commit 0c1fbc4ea6
Changed files (4)
lib/std/Io/Writer.zig
@@ -2661,7 +2661,7 @@ pub const Allocating = struct {
pub fn ensureTotalCapacity(a: *Allocating, new_capacity: usize) Allocator.Error!void {
// Protects growing unnecessarily since better_capacity will be larger.
if (a.writer.buffer.len >= new_capacity) return;
- const better_capacity = ArrayList(u8).growCapacity(a.writer.buffer.len, new_capacity);
+ const better_capacity = ArrayList(u8).growCapacity(new_capacity);
return ensureTotalCapacityPrecise(a, better_capacity);
}
lib/std/array_list.zig
@@ -172,7 +172,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
// a new buffer and doing our own copy. With a realloc() call,
// the allocator implementation would pointlessly copy our
// extra capacity.
- const new_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_len);
+ const new_capacity = Aligned(T, alignment).growCapacity(new_len);
const old_memory = self.allocatedSlice();
if (self.allocator.remap(old_memory, new_capacity)) |new_memory| {
self.items.ptr = new_memory.ptr;
@@ -408,7 +408,7 @@ pub fn AlignedManaged(comptime T: type, comptime alignment: ?mem.Alignment) type
// Protects growing unnecessarily since better_capacity will be larger.
if (self.capacity >= new_capacity) return;
- const better_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_capacity);
+ const better_capacity = Aligned(T, alignment).growCapacity(new_capacity);
return self.ensureTotalCapacityPrecise(better_capacity);
}
@@ -1160,7 +1160,7 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) 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.ensureTotalCapacityPrecise(gpa, growCapacity(self.capacity, new_capacity));
+ return self.ensureTotalCapacityPrecise(gpa, growCapacity(new_capacity));
}
/// If the current capacity is less than `new_capacity`, this function will
@@ -1359,17 +1359,12 @@ pub fn Aligned(comptime T: type, comptime alignment: ?mem.Alignment) type {
return self.getLast();
}
- const init_capacity = @as(comptime_int, @max(1, std.atomic.cache_line / @sizeOf(T)));
+ const init_capacity: comptime_int = @max(1, std.atomic.cache_line / @sizeOf(T));
/// Called when memory growth is necessary. Returns a capacity larger than
/// minimum that grows super-linearly.
- pub fn growCapacity(current: usize, minimum: usize) usize {
- var new = current;
- while (true) {
- new +|= new / 2 + init_capacity;
- if (new >= minimum)
- return new;
- }
+ pub fn growCapacity(minimum: usize) usize {
+ return minimum +| (minimum / 2 + init_capacity);
}
};
}
lib/std/deque.zig
@@ -56,7 +56,7 @@ pub fn Deque(comptime T: type) type {
/// Invalidates element pointers if additional memory is needed.
pub fn ensureTotalCapacity(deque: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void {
if (deque.buffer.len >= new_capacity) return;
- return deque.ensureTotalCapacityPrecise(gpa, growCapacity(deque.buffer.len, new_capacity));
+ return deque.ensureTotalCapacityPrecise(gpa, std.ArrayList(T).growCapacity(new_capacity));
}
/// If the current capacity is less than `new_capacity`, this function will
@@ -243,18 +243,6 @@ pub fn Deque(comptime T: type) type {
return index - head_len;
}
}
-
- const init_capacity: comptime_int = @max(1, std.atomic.cache_line / @sizeOf(T));
-
- /// 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;
- }
- }
};
}
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.