appendix-a
Appendix A. Data-Oriented Design in action
In Chapter 1, we learned how data locality can improve code performance. We learned that grouping data used together can improve performance by limiting cache misses, and that placing data in arrays is an easy, efficient way to group it. But how much performance improvement can we expect? This appendix shows a real-world example of how much performance we can expect from 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 many 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;
}
}