-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Vec::reserve should use alloc_excess #71383
Comments
Simply allocating for |
For clarity: This is about writing via mutable slice, not by pushing items one-by-one, so the application code benefits from knowing the rounded-to-allocation-bucket capacity and it's not just a matter of |
Seems like a reasonable change to me, let's cc @Amanieu who has been working on the allocator APIs recently I believe. I suspect a PR would be the best way to fix this, though. |
I believe that this has already been done in #70362: |
Is there any chance of this getting fixed? glibc has |
The proper solution to this is to deprecate If you are looking for a short-term solution then we could add an unstable |
Can confirm, that this was changed in #70362.
|
When calling
reserve()
on aVec
whose capacity is zero, afterwards thecapacity()
method always returns the exact argument that was passed toreserve()
. Even though the contract ofreserve()
allowsVec
to make the capacity larger than requested, if the previous capacity is zero,Vec
does not make use of this. When the underlying allocator is bucketed, rounding to the bucket size never wastes memory, soVec
should usealloc_excess
to make the slop available to the user.Use case: We're going to write at least
best_case
items and at mostworst_case
items, such thatbest_case < worst_case
, but we don't know how many items exactly. The common case can be expected to be close tobest_case
but often at least one more. It would make sense to first allocate forbest_case
rounded up to allocator bucket size, then start writing, then if not everything fits, resize toworst_case
, write the rest, and finally resize to the size actually written andshrink_to_fit()
.This would guarantee at most three allocations and in the common case one.
This issues comes up often with low-level string code.
The text was updated successfully, but these errors were encountered: