With some metaclass / reflection magic maybe one could come up with an additional scheme that would save the per-object vtable pointer through creation of a "duplicate" structure - e.g. given
struct Event {
virtual ~Event();
virtual bool whateverAPI();
pair<double,double> globalPosition();
pair<double,double> localPosition();
private:
double x{}, y{};
Widget& widget;
};
struct MyEvent : Event {
// or whatever, just an example
bool whateverAPI() override;
private:
MouseButtons button{};
};
one would get a sub-array of things with a layout similar as
("potentially UB" being something like copying all the non-vtable things into a stack-allocated concrete MyEvent and calling the virtual function on that which could be worth it for small objects in terms of cache usage if the vtable is 8 bytes and the custom data something like 16 bytes and there's 350000 objects)
With some metaclass / reflection magic maybe one could come up with an additional scheme that would save the per-object vtable pointer through creation of a "duplicate" structure - e.g. given
one would get a sub-array of things with a layout similar as ("potentially UB" being something like copying all the non-vtable things into a stack-allocated concrete MyEvent and calling the virtual function on that which could be worth it for small objects in terms of cache usage if the vtable is 8 bytes and the custom data something like 16 bytes and there's 350000 objects)