-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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::into_boxed_slice assumes len() == capacity() #29847
Comments
This is correct behaviour as far as I'm concerned. It's relying on implementation details that we aren't externally promising. |
I think the correct resolution to this issue is to guarantee that when using the global allocator, shrink_to_fit and with_capacity are precise for Vec. This is significantly more useful to users of Vec than acquiring whatever loose capacity (particularly since they usually don't need or want it when using these interfaces). |
I'm nominating this issue for discussion at the next libs meeting. I think we should hammer out our allocator vagueness story. In particular, our docs are being highly conservative by claiming that Vec::with_capacity, Vec::shrink_to_fit, and Vec::reserve_exact may produce a larger capacity than requested. To my knowledge, this is in anticipation of:
The original reservations here were, I believe, established by strcat long long ago, before I even started working on the project. strcat obviously understands allocators a lot, so I'm assuming there was some important insight here, and not just a vague reservation. That said I might be mistaken. If anyone knows the background here, I'd really appreciate chiming in. |
I'm also curious if people consider it more important for Vec to behave in a concrete way (perfect capacity) or use as much bonus space as possible. Personally I think the former is more valuable. Particularly since determining the bonus capacity isn't a trivial operation (so this will bloat up all calls to grow a Vec). |
Just for reference, mozillas nsTArray rounds to the new power-of-two for small and the next MiB for large allocations: https://dxr.mozilla.org/mozilla-central/source/xpcom/glue/nsTArray-inl.h#148-166 |
Sounds like the bug here is that |
@gankro I'm sure strcat was accounting for usable_size as well, it's certainly been mentioned before |
There is also the counterpart to Also,
|
I vote for "bonus space". Otherwise, it would be necessary to provide an API to calculate a capacity that doesn't waste the "bonus space." This eliminates unnecessary reallocations. I see no concrete advantage to having |
I assume that the documentation for I think a good compromise might be to have |
I've created #29931 to discuss the broader problem of whether we should take advantage of the slack space or not. This issue now just tracks the fact that we're relying on implementation details in a fragile way. (discussion was getting blended up too much over the two issues) |
Vec::into_boxed_slice
callsshrink_to_fit
internally.That method explicitly states in the docs that it may leave some excess capacity, which the current implementation however does not.
I have a PR coming soon that will actually make use of
alloc::usable_size
to actually use excess capacity.But that fails, because
into_boxed_slice
calls out toRawVec::into_box
which actually assumeslen() == capacity()
and thus leads to segfaults.The text was updated successfully, but these errors were encountered: