Boost logo

Boost :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2024-05-22 06:45:28


El 22/05/2024 a las 6:37, Helmut Zeisel via Boost escribió:
>> Gesendet: Dienstag, 21. Mai 2024 um 23:18 Uhr
>> Von: "Ion Gaztañaga via Boost" <boost_at_[hidden]>
>
>
>> but it's explicitly documented.
>
> Where?

https://www.boost.org/doc/libs/master/doc/html/boost/container/small_vector.html

"Any change to the capacity of the vector, including decreasing its size
such as with the shrink_to_fit method, will cause the vector to
permanently switch to dynamically allocated storage"

>> Maybe shrink_to_fit can be fixed.
>
> Also clear and swap. Maybe also the move assignement.

There are several downsides:

A) "clear" in vectors does not reduce the capacity, I think that would
be surprising because many times a user does not expect new allocations
after clear() if the previous capacity was big enough.

B) Moving to internal storage when move assigning could make the
operation throwing, when moving the dynamic buffer can be faster and
nothrow. LLVM and Folly take the dynamic buffer of the source vector if
available:

+ Folly:
https://github.com/facebook/folly/blob/main/folly/container/small_vector.h#L626

+ LLVM: https://llvm.org/doxygen/SmallVector_8h_source.html#l01069

C) Swap is similar to a double move assignment.

So it's a trade-off, I think current boost::container::small_vector
follows the "existing practice" of "least surprising behavior"...

Best,

Ion


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk