Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Data-oriented design: AoS, SoA, and cache locality

Choose a layout around how your program uses its data: compare whole records with separate columns, then measure the effect on a representative workload.

Version: ISO C++23; implementation support may vary

Syntax

// ISO C++23 — alternative layouts for N records
// T and U are field types; N is a compile-time element count.
#include <array>
#include <cstddef>

template<class T, class U>
struct Record { T x; U y; };

template<class T, class U, std::size_t N>
using AoS = std::array<Record<T, U>, N>;

template<class T, class U, std::size_t N>
struct SoA {
    std::array<T, N> x;
    std::array<U, N> y;
};

// Record i: aos[i].x and aos[i].y, or soa.x[i] and soa.y[i].

Data-oriented design means choosing how to represent data based on the operations the program performs. Start with the loops that matter: identify which fields they read or write, in what order, and how often. AoS and SoA are storage layouts, not keywords or distinct C++23 features.

An array of structures (AoS) stores complete records contiguously: aos[i].energy and aos[i].id belong to the same object. A structure of arrays (SoA) stores each field in its own contiguous array: soa.energy[i] and soa.id[i] describe the same logical record. Keep column lengths and record order aligned, including when sorting, inserting, or deleting.

Cache locality improves when successive accesses use data already brought into the cache. When a loop reads only the frequently used (hot) energy column, SoA can fit more useful values in a cache line and reduce the amount of unused data fetched. AoS may work better when an operation usually needs most fields of each record together.

Neither layout guarantees a speedup. In this example, conversion copies both fields for every record; keeping both layouts uses extra memory and requires keeping updates in sync. Compare optimized builds on target hardware with representative data sizes and access patterns. Include conversion in the measurement if it is part of the real workload.

Examples

#include <array>
#include <cstddef>
#include <iostream>

struct Particle {
    int energy;
    int id;
};

struct ParticleColumns {
    std::array<int, 3> energy;
    std::array<int, 3> id;
};

int main() {
    const std::array<Particle, 3> aos{{{2, 101}, {4, 102}, {6, 103}}};
    ParticleColumns soa{};
    for (std::size_t i = 0; i < aos.size(); ++i) {
        soa.energy[i] = aos[i].energy;
        soa.id[i] = aos[i].id;
    }

    int aosSum = 0;
    for (const Particle& particle : aos) {
        aosSum += particle.energy;
    }

    int soaSum = 0;
    for (int energy : soa.energy) {
        soaSum += energy;
    }

    std::cout << "AoS sum: " << aosSum << '\n'
              << "SoA sum: " << soaSum << '\n';
}

Result

AoS sum: 12
SoA sum: 12

Both loops sum only `energy` and print `12` for their respective layouts. The matching results demonstrate the same operation on two layouts, not a performance advantage.

Related lessons

  • C++ · Lesson 21 · Advanced · Version: 3

Sources