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

std.ArrayList: add ensureTotalCapacityPrecise and update doc comments #10077

Merged
merged 2 commits into from Nov 1, 2021

Conversation

squeek502
Copy link
Collaborator

See #9775 (comment) for more context. If merged, closes #9775.

From the commit messages:

initCapacity did and still does use the ensureTotalCapacityPrecise logic because the initial capacity of an ArrayList is not important in terms of how it grows, so allocating a more exact slice up-front allows for saving memory when the array list never exceeds that initial allocation size. There are use cases where this precise capacity is useful outside of the initCapacity function, though, like in instances where the user does not call the init function themselves but otherwise knows that an ArrayList is empty so calling ensureTotalCapacityPrecise can give the same memory savings that initCapacity would have.


These [ensureTotalCapacity] calls are all late-initialization of ArrayList's that were initialized outside the current scope. [Replacing them with ensureTotalCapacityPrecise] allows us to still get the potential memory-saving benefits of the 'precision' of initCapacity.

initCapacity did and still does use the ensureTotalCapacityPrecise logic because the initial capacity of an ArrayList is not important in terms of how it grows, so allocating a more exact slice up-front allows for saving memory when the array list never exceeds that initial allocation size. There are use cases where this precise capacity is useful outside of the `init` function, though, like in instances where the user does not call the `init` function themselves but otherwise knows that an ArrayList is empty so calling `ensureTotalCapacityPrecise` can give the same memory savings that `initCapacity` would have.

Closes ziglang#9775
…es sense

These calls are all late-initialization of ArrayList's that were initialized outside the current scope. This allows us to still get the potential memory-saving benefits of the 'precision' of initCapacity.
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.ArrayList: difference between initCapacity and ensureTotalCapacity
2 participants