Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upVec::into_boxed_slice assumes len() == capacity() #29847
Comments
Swatinem
referenced this issue
Nov 15, 2015
Closed
alloc: make RawVec round up allocation sizes to next power-of-two #29848
This comment has been minimized.
This comment has been minimized.
|
This is correct behaviour as far as I'm concerned. It's relying on implementation details that we aren't externally promising. |
This comment has been minimized.
This comment has been minimized.
|
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). |
Gankro
added
I-nominated
T-libs
labels
Nov 15, 2015
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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). |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
Sounds like the bug here is that |
This comment has been minimized.
This comment has been minimized.
|
@Gankro I'm sure strcat was accounting for usable_size as well, it's certainly been mentioned before |
This comment has been minimized.
This comment has been minimized.
|
There is also the counterpart to Also,
|
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Nov 19, 2015
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 |
This comment has been minimized.
This comment has been minimized.
|
I assume that the documentation for I think a good compromise might be to have |
Gankro
removed
the
I-nominated
label
Nov 19, 2015
This comment has been minimized.
This comment has been minimized.
|
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) |
Swatinem commentedNov 15, 2015
Vec::into_boxed_slicecallsshrink_to_fitinternally.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_sizeto actually use excess capacity.But that fails, because
into_boxed_slicecalls out toRawVec::into_boxwhich actually assumeslen() == capacity()and thus leads to segfaults.