shrink_to_fit shrinks in place #50742
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR updates the documentation of
Vec::shrink_to_fit
to 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
realloc
which 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
RawVec
is to useshrink_in_place
. Once PR #50738 is merged this can be updated to useshrink_in_place_excess
.