Skip to content

When collecting into a ThinVec, pass the size hint to with_capacity - #99

Open
joshtriplett wants to merge 1 commit into
mozilla:mainfrom
joshtriplett:collect-size-hint
Open

joshtriplett wants to merge 1 commit into
mozilla:mainfrom
joshtriplett:collect-size-hint

Conversation

@joshtriplett

Copy link
Copy Markdown

This ensures that collecting from an iterator with a good size hint will avoid creating a ThinVec with excess capacity.

In particular, collecting 1-3 items into a ThinVec will no longer create a ThinVec with capacity 4, wasting proportionally significant memory. This can represent a large amount of overall memory usage in code that has a large number of small ThinVecs.

This ensures that collecting from an iterator with a good size hint will
avoid creating a ThinVec with excess capacity.

In particular, collecting 1-3 items into a ThinVec will no longer create
a ThinVec with capacity 4.

@nicoburns nicoburns left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems reasonable to me. Although std Vec's implementation is quite a bit more sophisticated. The one thing we might consider doing here is only applying this is the iterator is non-empty. Also, apparently .extend is already applying this in most cases:

impl<T> Extend<T> for ThinVec<T> {

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.

2 participants