commit 5f31d54064b204910c18667d0b68b8bf2b57cffb (tree)
parent 73a8c9beaa63c48b6ddaeec8d2a67b239f0dec92
Author: LemonBoy <thatlemon@gmail.com>
Date: Wed, 2 Sep 2020 10:51:15 +0200
std: ArrayList.initCapacity now respects the specified cap
Don't use the user-supplied cap as starting point for a resize. Doing so
overallocates memory and thus negates the whole point of specifying a
precise cap value.
Diffstat:
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
@@ -46,7 +46,11 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
/// Deinitialize with `deinit` or use `toOwnedSlice`.
pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
var self = Self.init(allocator);
- try self.ensureCapacity(num);
+
+ const new_memory = try self.allocator.allocAdvanced(T, alignment, num, .at_least);
+ self.items.ptr = new_memory.ptr;
+ self.capacity = new_memory.len;
+
return self;
}
@@ -366,7 +370,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
/// Deinitialize with `deinit` or use `toOwnedSlice`.
pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
var self = Self{};
- try self.ensureCapacity(allocator, num);
+
+ const new_memory = try self.allocator.allocAdvanced(T, alignment, num, .at_least);
+ self.items.ptr = new_memory.ptr;
+ self.capacity = new_memory.len;
+
return self;
}