r/cpp • • Mar 10 '26

Deriving Type Erasure

https://david.alvarezrosa.com/posts/deriving-type-erasure/
59 Upvotes

34 comments sorted by

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

  1. Deref object pointer
  2. Deref vtable pointer
  3. Invoke function

libc++'s would just be

  1. Deref function pointer
  2. Invoke function

3

u/JVApen Clever is an insult, not a compliment. - T. Winters Mar 11 '26

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.

2

u/CornedBee Mar 11 '26

Wouldn't the code size of switching on the action parameter be about the same as the vtable? The switch might even be implemented as a jump table!

OTOH, 1 function means 1 prologue/epilogue pair...

2

u/JVApen Clever is an insult, not a compliment. - T. Winters Mar 11 '26

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

3

u/chengfeng-xie Mar 11 '26

For MSVC's implementation of std::any specifically, its class layout is (any#L348-L371):

struct _Small_storage_t {
    unsigned char _Data[_Any_small_space_size];
    const _Any_small_RTTI* _RTTI;
};
static_assert(sizeof(_Small_storage_t) == _Any_trivial_space_size);

struct _Big_storage_t {
    // Pad so that _Ptr and _RTTI might share _TypeData's cache line
    unsigned char _Padding[_Any_small_space_size - sizeof(void*)];
    void* _Ptr;
    const _Any_big_RTTI* _RTTI;
};
static_assert(sizeof(_Big_storage_t) == _Any_trivial_space_size);

struct _Storage_t {
    union {
        unsigned char _TrivialData[_Any_trivial_space_size];
        _Small_storage_t _SmallStorage;
        _Big_storage_t _BigStorage;
    };
    uintptr_t _TypeData;
};
static_assert(sizeof(_Storage_t) == _Any_trivial_space_size + sizeof(void*));
static_assert(is_standard_layout_v<_Storage_t>);

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.

2

u/XeroKimo Exception Enthusiast Mar 12 '26 edited Mar 12 '26

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