Repository navigation
When collecting into a ThinVec, pass the size hint to with_capacity - #99
Conversation
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
left a comment
There was a problem hiding this comment.
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:
Line 2099 in 261cb50
That extend happens after first allocating an empty vector (so it then has to be grown), and the call to |
|
@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. |
|
@joshtriplett Sure, #100 has a release with both this and #98. |
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).
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).
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).
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).
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.