Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Cache locality in C++: AoS and SoA

Choose a data layout based on what the program actually reads. AoS groups the fields of a record; SoA groups values of the same field across records.

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

Syntax

// AoS: one contiguous array of records
std::array<Record, N> rows;  // access: rows[i].score

// SoA: one contiguous array per field
struct Columns {
    std::array<int, N> score;
    std::array<int, N> id;
};
Columns cols;              // access: cols.score[i]
// Record and N must be defined; initialize values before reading.

Data-oriented design starts with the operations that run most often: which fields are read together, in what order, and how often? Cache locality means accessing nearby memory locations (spatial locality) or reusing the same data soon (temporal locality). Data layout directly affects spatial locality.

An array of structures (AoS) stores consecutive records in one contiguous sequence; rows[i].score is a field of record i. Successive score fields are sizeof(Record) bytes apart, including any padding. A structure of arrays (SoA) stores each field in its own contiguous column; cols.score[i] and cols.id[i] belong to the same logical record. Keep indices aligned across all columns when resizing or reordering.

A sequential pass over only the frequently used, or hot, score column in SoA can fit more useful values in each cache line and reduce the amount of unused data fetched. AoS can be a better fit when an operation uses many fields of each record together. C++23 guarantees contiguous std::array elements, not a cache-line size or a speedup.

Measure both layouts with representative data sizes and access patterns in an optimized build on the target hardware. Include or separately report conversion costs: copying and extra storage can offset gains from later passes.

Examples

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

struct Record {
    int score;
    int id;
};

template<std::size_t N>
struct Columns {
    std::array<int, N> score;
    std::array<int, N> id;
};

int main() {
    const std::array<Record, 3> rows{Record{3, 101}, Record{5, 102}, Record{7, 103}};
    Columns<3> cols{};
    for (std::size_t i = 0; i < rows.size(); ++i) {
        cols.score[i] = rows[i].score;
        cols.id[i] = rows[i].id;
    }

    int aos_sum = 0;
    for (const Record& row : rows) {
        aos_sum += row.score;
    }

    int soa_sum = 0;
    for (int score : cols.score) {
        soa_sum += score;
    }

    std::cout << "AoS sum: " << aos_sum << '\n'
              << "SoA sum: " << soa_sum << '\n';
}

Result

AoS sum: 15
SoA sum: 15

The program copies both fields from `rows` into `cols`, then sums only `score` in each layout. Both totals are 15. This is a layout demonstration, not a benchmark.

Related lessons

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

Sources