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 {...@@ -16,13 +16,22 @@ pub const Buffer = struct {
16 mem.copy(u8, self.list.items, m);16 mem.copy(u8, self.list.items, m);
17 return self;17 return self;
18 }18 }
1919
20 /// Initialize memory to size bytes of undefined values.
20 /// Must deinitialize with deinit.21 /// Must deinitialize with deinit.
21 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {22 pub fn initSize(allocator: *Allocator, size: usize) !Buffer {
22 var self = initNull(allocator);23 var self = initNull(allocator);
23 try self.resize(size);24 try self.resize(size);
24 return self;25 return self;
25 }26 }
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
27 /// Must deinitialize with deinit.36 /// Must deinitialize with deinit.
28 /// None of the other operations are valid until you do one of these:37 /// None of the other operations are valid until you do one of these:
...@@ -98,6 +107,13 @@ pub const Buffer = struct {...@@ -98,6 +107,13 @@ pub const Buffer = struct {
98 pub fn len(self: Buffer) usize {107 pub fn len(self: Buffer) usize {
99 return self.list.len - 1;108 return self.list.len - 1;
100 }109 }
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
102 pub fn append(self: *Buffer, m: []const u8) !void {118 pub fn append(self: *Buffer, m: []const u8) !void {
103 const old_len = self.len();119 const old_len = self.len();
...@@ -156,3 +172,21 @@ test "simple Buffer" {...@@ -156,3 +172,21 @@ test "simple Buffer" {
156 try buf2.resize(4);172 try buf2.resize(4);
157 testing.expect(buf.startsWith(buf2.toSlice()));173 testing.expect(buf.startsWith(buf2.toSlice()));
158}174}
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}