Skip to content

Conversation

pmovzxdq
Copy link

Fixes #24886

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a better way to implement this, and arguably a unit test is the more valuable deliverable here anyway.

@pmovzxdq
Copy link
Author

I have some questions:

var list: ArrayList(void) = .empty;
try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == 0); // ???

I am not sure if this is right. I think it makes more sense if list.capacity is initialized to math.maxInt(usize):

var list: ArrayList(void) = .empty;
try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == math.maxInt(usize));

initCapacity() currently is like the second example:

var list = try ArrayList(void).initCapacity(testing.allocator, 200);
defer list.deinit(testing.allocator);

try testing.expect(list.items.len == 0);
try testing.expect(list.capacity == math.maxInt(usize));

Which one should be used?

You said that there is a better way to implement this.
I actually like the current fix because:

  • init_capacity is only used by growCapacity().
  • It makes sense, because init_capacity is equal to the number of T that can fit in a std.atomic.cache_line. An unlimited amount (math.maxInt(usize)) of 0 bytes (like @sizeOf(void) or @sizeOf(enum { one })) can fit into any amount of memory.

I am open to any suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

std.ArrayListUnamanged: init_capacity causes compile error when @sizeOf(T) == 0
2 participants