How do I write EBO-friendly classes (empty base optimization) for game development?

In game development, achieving optimal performance is crucial. One way to optimize memory usage for classes in C++ is by applying the Empty Base Optimization (EBO). EBO takes advantage of the fact that if a class does not contain any data members, the compiler can optimize it, often eliminating the overhead of an empty base class.

When designing EBO-friendly classes, it’s important to keep in mind how inheritance is structured. Using interfaces and creating classes that are small and lightweight can help in utilizing EBO effectively.

class Base {};
    
    class DerivedA : public Base {
    public:
        void someFunctionA() {}
    };

    class DerivedB : public Base {
    public:
        void someFunctionB() {}
    };

    class Wrapper : public DerivedA, public DerivedB {
        // No additional members here, hence enabling EBO
    };
    
    Wrapper w;
    // The size of Wrapper may not include the size of Base 
    // if empty base optimization is applied.
    

EBO Empty Base Optimization C++ game development performance optimization class design