Type erasing using a polymorphic type as base would mean that you're using an extra 8 bytes of space. For example, MSVC's std::any is 64 bytes in size, 8 bytes for a pointer, 56 bytes for small buffer optimization. If you use a polymorphic type, you effectively only have 48 bytes of SBO because 8 bytes are being taken by the vtable pointer.
The other difference, with libc++'s version there's 1 less level of indirection. Using a polymorphic base would be
Inside that function, it still redirects. Due to inlining, that might be less impactful. Due to code ordering, that could also be close together in the exe even if not inlined.
Another interesting difference is that you don't need vtables. Not just the 8 bytes are relevant here, though also the separate space where the table gets stored is no longer needed. It might sound less relevant, though it's an optimization towards the size of the executable. Especially relevant if you store many different types in it.
Assuming the typeid does not get inlined, it also would allow for COMDAT folding/ICF. (Common data folding?/Identical code folding?) That again would reduce the size of the executable.
A side effect of the size reductions above is that you also have a higher chance of getting a cache hit for the code to be executed.
It's all small stuff, though it might make a noticeable impact on performance when used at scale.
I remember some configurations where the vtables are put separately into some section such that they can add extra security on the memory access. That might make a difference. Though still it's all micro optimizations. The most important gain will be from having 8 extra bytes in the SBO
In the above, the _RTTI member is a pointer to a hand-rolled virtual table, and the _TypeData member is a tagged pointer (encoding whether SBO is in effect) to a type_info object. If the virtual-function approach were chosen, the _RTTI member could be eliminated, so the available SBO space would actually be the same in both approaches. So I think what matters more is the extra indirection involved in the virtual-function approach, as you mentioned.
Hmm... originally I thought you'd need to replace the _RTTI field with something like a any_impl_base*, but I guess if the implementation allows you, you can interpret and launder the start of the SBO address as a any_impl_base*, in which case, yea, you would still have the full 56 bytes of SBO, otherwise, you'd only have 48 bytes.
As I thought more about it, the vtable might not even occur any penalty because the pointer would be in the cache whether you use MSVC's hand rolled vtable or using C++'s virtual functions
View all comments
Show parent comment
6
u/XeroKimo Exception Enthusiast Mar 11 '26 edited Mar 11 '26
There's 2 differences.
Type erasing using a polymorphic type as base would mean that you're using an extra 8 bytes of space. For example, MSVC's std::any is 64 bytes in size, 8 bytes for a pointer, 56 bytes for small buffer optimization. If you use a polymorphic type, you effectively only have 48 bytes of SBO because 8 bytes are being taken by the vtable pointer.
The other difference, with libc++'s version there's 1 less level of indirection. Using a polymorphic base would be
libc++'s would just be