Appendix A. Data-Oriented Design in action

 

In Chapter 1, we learned how data locality can improve the performance of our code. We learned that grouping data that is used together can improve performance by limiting cache misses and that placing our data in arrays is an easy and efficient way to group data. But how much performance improvement can we expect? This appendix is designed to show a real-world example of how much performance we can expect by using data locality.

A.1 OOP without data locality

Let's implement a simple test of moving enemies around to test how much of a performance boost we get from data locality. For our first test, we will allocate a bunch of Enemy objects, move them around, and time how long it takes.

Listing A.1 shows what a typical OOP Enemy class could look like:

Listing A.1 OOP Enemy class
Public class Enemy
{
    Vector3 m_position;
    string m_enemyName;
    int m_enemyType;
    int m_hp;
    float m_shieldRadius;
    int m_currentShieldStrength;
    int m_maxShieldStrength;
    Color m_currentColor;
    Vector3 m_direction;
    float m_dodge;
    int m_primaryWeaponType;
    int m_secondaryWeaponType;
    int m_primaryWeaponDamage;
    int m_secondaryWeaponDamage;
    int m_primaryWepaonAmmo;
    int m_secondaryWeaponAmmo;
    float m_shieldRechargeTime;
    float m_velocity;
    int m_armorType;
    // etc...
    
    public void Move()
    {
        m_position += m_direction * m_velocity;
    }
}

A.2 OOP with data locality

A.3 Optimized OOP

A.3.1 Optimization caveat

A.4 DOD and arrays

A.5 Conclusion