-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Express ArrayList in ArrayListUnmanaged operations #6294
Conversation
items: Slice, | ||
capacity: usize, | ||
allocator: *Allocator, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be changed to
unmanaged: Unmanaged,
allocator: *Allocator,
to make it more consistent with ArrayHashMap
and HashMap
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this would be the most elegant solution, if the fields of the array list weren't part of the interface. Users are encouraged to directly refer to items in the ArrayList by using .items[index]
, and changing this would be a big breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This along with other use cases makes me think it might be useful for Zig to have a way to declare aliases. For example:
pub alias items = unmanaged.items;
This would mean code that references array_list.items
would be equivalent to array_list.unmanaged.items
.
My intuition is that this wouldn't be well optimized; e.g. you'd end up writing |
After discussing with Andrew a few days ago, it was decided that ArrayList/ArrayListUnmanaged should be one of the few places where code is copy pasted. The argumentation was that this yields a smaller call stack (which is nice considering these types are used fairly fundamentally), that the implementation of these types is fairly self-contained anyway, and @daurnimator's optimization point above. So instead of my solution, there should be some generic testing function (although i'm not quite sure how that would be implemented best). One idea is to have a mixin for the operations that don't have any allocations, although i don't think there are very many. Thoughts? |
Closing in favor of #6457 |
I came across several issues in ArrayListUnmanaged, namely:
zig/lib/std/array_list.zig
Line 374 in 7d487a4
zig/lib/std/array_list.zig
Line 422 in 7d487a4
This PR changes the implementation of ArrayList to use that of ArrayListUnmanaged wherever possible, which ensures that the implementation of ArrayListUnmanaged and ArrayList are the same. The original test cases now also test ArrayListUnmanaged. Furthermore, this reduces code duplication in the standard library.
Note that i've changed
ArrayList.deinit
to require*Self
like other std containers. I've changed constness of ArrayList wherever required.Currently, most calls to ArrayListUnmanaged are called like
I hope that this is optimization/result location friendly, i haven't done any benchmarking.