3 ms·
This is like the inverse of the normal C++ vtable. Normally the whole point of having a separate vtable (as opposed to just a bunch of function pointers directl
by int_19h 1y ago
This is like the inverse of the normal C++ vtable. Normally the whole point of having a separate vtable (as opposed to just a bunch of function pointers directly in the struct) is so that you can share a single one between all instances, at the cost of one extra indirection. But here a copy of the vtable is instead attached to one individual instance, and yet it still does this one extra indirection to get to the implementation object.
- ethan_smith 1y agoThe extra indirection is a reasonable tradeoff in Zig because it enables heterogeneous collections of implementations while maintaining type safety without requiring inheritance.
- ww520 1y agoYes. You're right. It's not the typical C++ vtable. I wrote a second blog for the C++ style vtable. https://williamw520.github.io/2025/07/17/memory-efficient-zig-interface.html https://williamw520.github.io/2025/07/17/memory-efficient-zi... The first version is faster (one pointer indirection) with more memory consumed. The second version is more memory efficient sharing a single vtable for all instances, a bit slower (two pointer indirections).