Optimizable Code (2013)
deplinenoise.wordpress.com·2w·
Discuss: Hacker News
Flag this post

You don’t need to be a low-level programming expert to write fast code. The trick is to write code that CAN be optimized. (And the trick to doing that is to worry about memory accesses.)

For example, consider this piece of code:

class GameObject {
float m_Pos[2];
float m_Velocity[2];
char m_Name[32];
Model* m_Model;
// ... other members ...
float m_Foo;

void UpdateFoo(float f)
{
float mag = sqrtf(
m_Velocity[0] * m_Velocity[0] +
m_Velocity[1] * m_Velocity[1]);
m_Foo += mag * f;
}
};

Let’s say you have a 100 or so of these game objects, and they’re spread out randomly in memory. Every frame you do this:

for each game object pointer p:
p->UpdateFoo(f)

It’s starting to show up in the profiler as a hot-spot. What’s going on?

First, we need to understand where th…

Similar Posts

Loading similar posts...