Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

Snektron
Copy link
Collaborator

@Snektron Snektron commented Sep 9, 2020

I came across several issues in ArrayListUnmanaged, namely:

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

var unmanaged = self.toUnmanaged();
defer self.* = unmanaged.toManaged(self.allocator);
return unmanaged.addOne(self.allocator);

I hope that this is optimization/result location friendly, i haven't done any benchmarking.

Comment on lines 30 to 32
items: Slice,
capacity: usize,
allocator: *Allocator,
Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Contributor

@marler8997 marler8997 Sep 22, 2020

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.

@daurnimator
Copy link
Contributor

Currently, most calls to ArrayListUnmanaged are called like

var unmanaged = self.toUnmanaged();
defer self.* = unmanaged.toManaged(self.allocator);
return unmanaged.addOne(self.allocator);

I hope that this is optimization/result location friendly, i haven't done any benchmarking.

My intuition is that this wouldn't be well optimized; e.g. you'd end up writing self.allocator needlessly each operation.

@Snektron
Copy link
Collaborator Author

Snektron commented Sep 22, 2020

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?

@Snektron
Copy link
Collaborator Author

Closing in favor of #6457

@Snektron Snektron closed this Sep 29, 2020
@Snektron Snektron deleted the managed-as-unmanaged branch October 1, 2020 14:52
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.

4 participants