Skip to content

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

Merged
emilio merged 1 commit into
mozilla:mainfrom
joshtriplett:collect-size-hint
Oct 5, 2026
Merged

emilio merged 1 commit into
mozilla:mainfrom
joshtriplett:collect-size-hint

Conversation

@joshtriplett

Copy link
Copy Markdown
Contributor

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> {

@joshtriplett

Copy link
Copy Markdown
Contributor Author

@nicoburns

Also, apparently .extend is already applying this in most cases

That extend happens after first allocating an empty vector (so it then has to be grown), and the call to .reserve with the hint value will over-allocate (e.g. allocate 4 elements even if the size hint was 1).

@emilio
emilio merged commit 35206b7 into mozilla:main Oct 5, 2026
5 checks passed
@joshtriplett
joshtriplett deleted the collect-size-hint branch October 5, 2026 20:25
@joshtriplett

Copy link
Copy Markdown
Contributor Author

@emilio Thanks!

When you get a chance, would it be possible to get a new release with this in it? I'd love to use it in rustc.

@emilio

emilio commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

@joshtriplett Sure, #100 has a release with both this and #98.

rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 8, 2026
This includes mozilla/thin-vec#99 , which makes
thin-vec respect size_hint when collecting.

In particular, this causes collections of 1-3 items to not be grown to a
capacity of 4. The compiler has a lot of `TinyVec`s in the 1-3 range, so
this substantially helps memory, and seems neutral on instructions.

For aws-sdk-ec2 full check, this saves 30.69 MiB max-rss (0.55% of peak
RSS).
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 8, 2026
Upgrade `thin-vec` to 0.2.21, including fix for `.collect()` capacity

This includes mozilla/thin-vec#99 , which makes thin-vec respect size_hint when collecting.

In particular, this causes collections of 1-3 items to not be grown to a capacity of 4. The compiler has a lot of `TinyVec`s in the 1-3 range, so this substantially helps memory, and seems neutral on instructions.

For aws-sdk-ec2 full check, this saves 30.69 MiB max-rss (0.55% of peak RSS).

---

Merging this avoids the need to work around it in other work (e.g. by replacing `.collect()` with something calling `ThinVec::with_capacity(...)` first).
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 8, 2026
Upgrade `thin-vec` to 0.2.21, including fix for `.collect()` capacity

This includes mozilla/thin-vec#99 , which makes thin-vec respect size_hint when collecting.

In particular, this causes collections of 1-3 items to not be grown to a capacity of 4. The compiler has a lot of `TinyVec`s in the 1-3 range, so this substantially helps memory, and seems neutral on instructions.

For aws-sdk-ec2 full check, this saves 30.69 MiB max-rss (0.55% of peak RSS).

---

Merging this avoids the need to work around it in other work (e.g. by replacing `.collect()` with something calling `ThinVec::with_capacity(...)` first).
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Oct 10, 2026
Upgrade `thin-vec` to 0.2.21, including fix for `.collect()` capacity

This includes mozilla/thin-vec#99 , which makes thin-vec respect size_hint when collecting.

In particular, this causes collections of 1-3 items to not be grown to a capacity of 4. The compiler has a lot of `TinyVec`s in the 1-3 range, so this substantially helps memory, and seems neutral on instructions.

For aws-sdk-ec2 full check, this saves 30.69 MiB max-rss (0.55% of peak RSS).

---

Merging this avoids the need to work around it in other work (e.g. by replacing `.collect()` with something calling `ThinVec::with_capacity(...)` first).
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.

3 participants