Beating Cache Misses: Transforming POJOs into Cache-Friendly SoA-Style classes
Description and use
Transforming POJOs into SoA classes with the Flock library improves data locality and eliminates cache misses, enabling thousands of birds to be simulated without frame‑rate stutters.
Problem
Simulating thousands of birds in Raylib using an OO approach caused cache misses and stuttered performance.
Outcome
The transformation reduced cache misses, allowing the simulation to scale from hundreds to thousands of birds without frame‑rate stutters.
Workflow
- 1. Write a POJO class for the entity (e.g., Bird).
- 2. Annotate the class with @DataOriented.
- 3. Compile the project; the Flock annotation processor generates a companion SoA class.
- 4. Replace the List
<Bird>with an instance of the generated SoA class for heavy‑weight updates. - 5. Iterate over the primitive arrays to update positions and other attributes.
- 6. Use the original POJO for high‑level game state or debugging.
Evidence
If I wanted to simulate thousands of birds moving independently, the standard Object‑Oriented (OO) approach would fail.
The CPU has to jump around different locations in memory to find each object. This causes cache misses.
The Flock annotation processor then generates a companion class during compilation. Instead of a List of objects, it creates a "Struct" of Arrays (SoA).
By using primitive arrays, the data is stored contiguously in memory. When the CPU fetches the x coordinate for bird #1, it also pre‑fetches the x coordinates for birds #2, #3, and #4 into the cache.
- Record date: 2026-10-01T15:55:41.767Z