authorgravatar for Modernwarfare3Minecraft64@gmail.comMCRusher <Modernwarfare3Minecraft64@gmail.com> 2019-11-23 23:08:33-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-11-23 23:08:33-05:00
logd49e0a7b90dabbf486902fa9d4a4306aede55087
treee2ac48ed25c54c6e3b13eaddb1876331cd020eec
parent10e6cde083cf9ddd1ae72850d45c25b1258d0d7e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Added initCapacity, capacity, and 2 tests.

Added Buffer.initCapcity() to buffer to allow preallocation of a block of memory to reduce future allocations. Uses the added ArrayList.initCapacity() function to achieve this. Added Buffer.capacity() to track current usable allocation size, not counting null byte, and returning 0 if empty or created with Buffer.initNull() Added a test for initCapacity() that shows that no further allocation is performed for an append of size smaller than or equal to capacity when initCapacity is used. Added a test for initSize(), since it did not exist already. Also added a comment to better explain the difference between initSize() and initCapacity() note: forgot in the first commit but thanks to mikdusan for helping me brainstorm, through the process, and for drawing up a draft diff which I tweaked.

1 files changed, 35 insertions(+), 1 deletions(-)

lib/std/buffer.zig+35-1
......@@ -16,13 +16,22 @@ pub const Buffer = struct {
1616 mem.copy(u8, self.list.items, m);
1717 return self;
1818 }
19
19
20 /// Initialize memory to size bytes of undefined values.
2021 /// Must deinitialize with deinit.
2122 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
2223 var self = initNull(allocator);
2324 try self.resize(size);
2425 return self;
2526 }
27
28 /// Initialize with capacity to hold at least num bytes.
29 /// Must deinitialize with deinit.
30 pub fn initCapacity(allocator: *Allocator, num: usize) !Buffer {
31 var self = Buffer{ .list = try ArrayList(u8).initCapacity(allocator, num + 1) };
32 self.list.appendAssumeCapacity(0);
33 return self;
34 }
2635
2736 /// Must deinitialize with deinit.
2837 /// None of the other operations are valid until you do one of these:
......@@ -98,6 +107,13 @@ pub const Buffer = struct {
98107 pub fn len(self: Buffer) usize {
99108 return self.list.len - 1;
100109 }
110
111 pub fn capacity(self: Buffer) usize {
112 return if (self.list.items.len > 0)
113 self.list.items.len - 1
114 else
115 0;
116 }
101117
102118 pub fn append(self: *Buffer, m: []const u8) !void {
103119 const old_len = self.len();
......@@ -156,3 +172,21 @@ test "simple Buffer" {
156172 try buf2.resize(4);
157173 testing.expect(buf.startsWith(buf2.toSlice()));
158174}
175
176test "Buffer.initSize" {
177 var buf = try Buffer.initSize(debug.global_allocator, 3);
178 testing.expect(buf.len() == 3);
179 try buf.append("hello");
180 testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
181}
182
183test "Buffer.initCapacity" {
184 var buf = try Buffer.initCapacity(debug.global_allocator, 10);
185 testing.expect(buf.len() == 0);
186 testing.expect(buf.capacity() >= 10);
187 const old_cap = buf.capacity();
188 try buf.append("hello");
189 testing.expect(buf.len() == 5);
190 testing.expect(buf.capacity() == old_cap);
191 testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));
192}