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.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?