Skip to content

Commit 10e6ae2

Browse files
andrewrkricherfu
authored andcommitted
Revert "Smaller memory footprint for BoundedArray (ziglang#16299)"
This reverts commit cb5a6be. I deeply apologize for the churn. This change is problematic given that we do not have ranged integers (yet? see ziglang#3806). In the meantime, this type needs to be `usize`, matching the length and index types for all std lib data structures. Users who want to save memory should not use heap-allocated BoundedArray values, since it is inherently memory-inefficient. Use a different memory layout instead. If ziglang#3806 is accepted and implemented, the length value can become an integer with the appropriate range, without the footgun. If that proposal is not accepted, len type will remain a usize.
1 parent 210c381 commit 10e6ae2

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

lib/std/bounded_array.zig

+8-22
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ pub fn BoundedArrayAligned(
3939
) type {
4040
return struct {
4141
const Self = @This();
42-
const Len = std.math.IntFittingRange(0, buffer_capacity);
43-
4442
buffer: [buffer_capacity]T align(alignment) = undefined,
45-
len: Len = 0,
43+
len: usize = 0,
4644

4745
/// Set the actual length of the slice.
4846
/// Returns error.Overflow if it exceeds the length of the backing array.
4947
pub fn init(len: usize) error{Overflow}!Self {
5048
if (len > buffer_capacity) return error.Overflow;
51-
return Self{ .len = @intCast(len) };
49+
return Self{ .len = len };
5250
}
5351

5452
/// View the internal array as a slice whose size was previously set.
@@ -69,7 +67,7 @@ pub fn BoundedArrayAligned(
6967
/// Does not initialize added items if any.
7068
pub fn resize(self: *Self, len: usize) error{Overflow}!void {
7169
if (len > buffer_capacity) return error.Overflow;
72-
self.len = @intCast(len);
70+
self.len = len;
7371
}
7472

7573
/// Remove all elements from the slice.
@@ -178,7 +176,7 @@ pub fn BoundedArrayAligned(
178176
/// This operation is O(N).
179177
pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void {
180178
try self.ensureUnusedCapacity(items.len);
181-
self.len = @intCast(self.len + items.len);
179+
self.len += items.len;
182180
mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]);
183181
@memcpy(self.slice()[i..][0..items.len], items);
184182
}
@@ -208,7 +206,7 @@ pub fn BoundedArrayAligned(
208206
for (self.constSlice()[after_range..], 0..) |item, i| {
209207
self.slice()[after_subrange..][i] = item;
210208
}
211-
self.len = @intCast(self.len - len + new_items.len);
209+
self.len -= len - new_items.len;
212210
}
213211
}
214212

@@ -259,7 +257,7 @@ pub fn BoundedArrayAligned(
259257
/// enough to store the new items.
260258
pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void {
261259
const old_len = self.len;
262-
self.len = @intCast(self.len + items.len);
260+
self.len += items.len;
263261
@memcpy(self.slice()[old_len..][0..items.len], items);
264262
}
265263

@@ -275,8 +273,8 @@ pub fn BoundedArrayAligned(
275273
/// Asserts the capacity is enough.
276274
pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void {
277275
const old_len = self.len;
278-
assert(self.len + n <= buffer_capacity);
279-
self.len = @intCast(self.len + n);
276+
self.len += n;
277+
assert(self.len <= buffer_capacity);
280278
@memset(self.slice()[old_len..self.len], value);
281279
}
282280

@@ -406,18 +404,6 @@ test BoundedArray {
406404
try testing.expectEqualStrings(s, a.constSlice());
407405
}
408406

409-
test "BoundedArray sizeOf" {
410-
// Just sanity check size on one CPU
411-
if (@import("builtin").cpu.arch != .x86_64)
412-
return;
413-
414-
try testing.expectEqual(@sizeOf(BoundedArray(u8, 3)), 4);
415-
416-
// `len` is the minimum required size to hold the maximum capacity
417-
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).len), u4);
418-
try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).len), u5);
419-
}
420-
421407
test "BoundedArrayAligned" {
422408
var a = try BoundedArrayAligned(u8, 16, 4).init(0);
423409
try a.append(0);

0 commit comments

Comments
 (0)