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
3
u/chengfeng-xie Mar 11 '26
For MSVC's implementation of
std::anyspecifically, its class layout is (any#L348-L371):In the above, the
_RTTImember is a pointer to a hand-rolled virtual table, and the_TypeDatamember is a tagged pointer (encoding whether SBO is in effect) to atype_infoobject. If the virtual-function approach were chosen, the_RTTImember 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.