-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
shrink_to_fit shrinks in place #50742
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
Conversation
|
(rust_highfive has picked a reviewer for you, use r? to override) |
|
r? @sfackler cc: @glandium @SimonSapin The current implementation falls back to I find it very weird that we have both Then, even when |
|
Why doesn't realloc shrink the allocation as much as possible? |
|
|
Do those implementations exist? |
EDIT: ~~~Of course, that's what for example jemalloc does. ~~! Wait, no, it only does that if the sizes are similar enough: https://github.com/jemalloc/jemalloc/blob/0fadf4a2e3e629b9fa43888f9754aea5327d038f/src/arena.c#L1652 Also, glibc does a similar thing, avoiding the reallocation only if the difference between the sizes isn't large enough: https://github.com/bminor/glibc/blob/master/malloc/malloc.c#L3206 We could do some sort of small size different optimization in the default implementation of the |
This PR updates the documentation of
Vec::shrink_to_fitto indicate that the request to shrink the vector's capacity is non-binding.It used to say that "it shrinks the capacity as much as possible" but that was a lie since it also said "the allocator might inform the vector that there is space for a few more elements". Implementation-wise it called
reallocwhich does not shrink the capacity as much as possible.The only way to really shrink the capacity "as much as possible", is to perform a new allocation of the desired capacity, and move the elements over to it. This is not what was intended by the previous documentation.
The new docs more clearly state the "spirit" of
shrink_to_fit, which is to reduce the unused capacity of the vector in whichever way it makes sense. This will depend, among other things, on the vector's allocatorThe way this is now implemented in
RawVecis to useshrink_in_place. Once PR #50738 is merged this can be updated to useshrink_in_place_excess.