[RFC] Should we increase the small-object buffer in std::any?

Jonathan Wakely jwakely@redhat.com
Tue Oct 9 08:44:00 GMT 2018


On 09/10/18 09:41 +0100, Jonathan Wakely wrote:
>Before we declare C++17 support non-experimental we should consider
>some breaking changes to our C++17 types. For example, we could make
>std::any larger, so that it can store larger objects internally
>without heap allocation.
>
>Currently it has a buffer of sizeof(void*) bytes, which either stores
>a small object inline, or stores a pointer to an object on the heap.
>The std::any needs a pointer to the "manager function" as well, so
>sizeof(any) is two pointers. Libc++ has a 3*sizeof(void*) buffer, so
>sizeof(any) is four pointers. For MSVC sizeof(any) is eight pointers!

For MSVC I see 64 bytes for x86_64 and 40 bytes for 32-bit x86.

>Obviously there's a trade-off here, and I don't have any usage data to
>make a good decision.
>
>If we make the buffer bigger then we avoid the heap more often, but
>when we do still need to use the heap there's wasted space in the
>std::any (because we only need enough room for a void*).
>



More information about the Libstdc++ mailing list