authorgravatar for john@whatisaph.oneJohn Simon <john@whatisaph.one> 2023-07-03 13:58:03-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-03 13:58:03-04:00
logcb5a6be41ae0efc30d0b59a41b0763db966e5bf4
treede3aab2fde99c566c05865e57cbc0b4b7ad71471
parent28ad74e8a6ed3484d632a6a615f5d0935c03676a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Smaller memory footprint for BoundedArray (#16299)

Store BoundedArray's length using the smallest possible integer Co-authored-by: zooster <r00ster91@proton.me>

1 files changed, 22 insertions(+), 8 deletions(-)

lib/std/bounded_array.zig+22-8
......@@ -39,14 +39,16 @@ pub fn BoundedArrayAligned(
3939) type {
4040 return struct {
4141 const Self = @This();
42 const Len = std.math.IntFittingRange(0, buffer_capacity);
43
4244 buffer: [buffer_capacity]T align(alignment) = undefined,
43 len: usize = 0,
45 len: Len = 0,
4446
4547 /// Set the actual length of the slice.
4648 /// Returns error.Overflow if it exceeds the length of the backing array.
4749 pub fn init(len: usize) error{Overflow}!Self {
4850 if (len > buffer_capacity) return error.Overflow;
49 return Self{ .len = len };
51 return Self{ .len = @intCast(len) };
5052 }
5153
5254 /// View the internal array as a slice whose size was previously set.
......@@ -67,7 +69,7 @@ pub fn BoundedArrayAligned(
6769 /// Does not initialize added items if any.
6870 pub fn resize(self: *Self, len: usize) error{Overflow}!void {
6971 if (len > buffer_capacity) return error.Overflow;
70 self.len = len;
72 self.len = @intCast(len);
7173 }
7274
7375 /// Copy the content of an existing slice.
......@@ -163,7 +165,7 @@ pub fn BoundedArrayAligned(
163165 /// This operation is O(N).
164166 pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void {
165167 try self.ensureUnusedCapacity(items.len);
166 self.len += items.len;
168 self.len = @intCast(self.len + items.len);
167169 mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);
168170 @memcpy(self.slice()[i..][0..items.len], items);
169171 }
......@@ -193,7 +195,7 @@ pub fn BoundedArrayAligned(
193195 for (self.constSlice()[after_range..], 0..) |item, i| {
194196 self.slice()[after_subrange..][i] = item;
195197 }
196 self.len -= len - new_items.len;
198 self.len = @intCast(self.len - len + new_items.len);
197199 }
198200 }
199201
......@@ -244,7 +246,7 @@ pub fn BoundedArrayAligned(
244246 /// enough to store the new items.
245247 pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
246248 const old_len = self.len;
247 self.len += items.len;
249 self.len = @intCast(self.len + items.len);
248250 @memcpy(self.slice()[old_len..][0..items.len], items);
249251 }
250252
......@@ -260,8 +262,8 @@ pub fn BoundedArrayAligned(
260262 /// Asserts the capacity is enough.
261263 pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void {
262264 const old_len = self.len;
263 self.len += n;
264 assert(self.len <= buffer_capacity);
265 assert(self.len + n <= buffer_capacity);
266 self.len = @intCast(self.len + n);
265267 @memset(self.slice()[old_len..self.len], value);
266268 }
267269
......@@ -387,6 +389,18 @@ test "BoundedArray" {
387389 try testing.expectEqualStrings(s, a.constSlice());
388390}
389391
392test "BoundedArray sizeOf" {
393 // Just sanity check size on one CPU
394 if (@import("builtin").cpu.arch != .x86_64)
395 return;
396
397 try testing.expectEqual(@sizeOf(BoundedArray(u8, 3)), 4);
398
399 // `len` is the minimum required size to hold the maximum capacity
400 try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).len), u4);
401 try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).len), u5);
402}
403
390404test "BoundedArrayAligned" {
391405 var a = try BoundedArrayAligned(u8, 16, 4).init(0);
392406 try a.append(0);