Cache Lines Touched: AoS vs SoA
Prerequisites: Cache Lines, Locality, and Pointer Chasing, Memory Layout of Vectors and Linked Lists, The Cost Model: What Big-O Hides
The cost model's MarketUpdate loop reads only a few fields of each update. Decide how to lay out records for such a loop by counting the cache lines each of two layouts makes it touch.
- Array of structs (AoS): the records one after another in a single array, the way
std::vector<MarketUpdate>does. - Struct of arrays (SoA): one dense array per field, such as a
pricearray and aqtyarray. Recordr's fields are elementrof each array.
// Array of structs: one array of whole records.
std::vector<MarketUpdate> updates; // record r's price is updates[r].price
// Struct of arrays: one array per field.
struct MarketUpdates {
std::vector<std::uint64_t> sequence;
std::vector<std::int64_t> price; // record r's price is price[r]
std::vector<std::int32_t> qty;
};
What to implement
count_cache_lines(fields, n, commands)returns one line perSCAN,STRIDE, orGATHER, in command order.fieldslists the record's fields in declaration order, each with a size in bytes. Both layouts holdnrecords, numbered0ton - 1.- On stdin: the field count, then one
name sizeline per field, thenn, then the command count and one command per line. - The problem has three parts. Each part adds commands, and one solution must pass all eleven tests.
| Command | Loop it describes | Prints |
|---|---|---|
SCAN fields | Reads fields of every record, r = 0, 1, ..., n - 1 | aos soa winner |
STRIDE fields k | Reads fields of records 0, k, 2k, ... below n (Part 2) | aos soa winner |
GATHER fields i1 i2 ... | Reads fields of the listed records (Part 2) | aos soa winner |
OFFSET m | Moves the start of every array m bytes past a line boundary (Part 3) | nothing |
fields in a command is a comma-separated list of declared field names with no spaces, such as price,qty. Each name appears at most once, in any order, and the order does not change what the loop reads.
Memory model
- Memory is divided into 64-byte cache lines. Byte address
alies in linea / 64(integer division). - The record is packed: the first field starts at offset 0 and each later field starts where the previous one ends. The record size
Ris the sum of the field sizes. - A record that needs padding lists it as a field of its own that no loop reads, as
padis in the example.
| Layout | Field f of record r is the size(f) bytes starting at |
|---|---|
| AoS | r * R + offset(f) (the array starts at address 0, record r at r * R) |
| SoA | r * size(f) bytes after the start of field f's own array |
Every array starts at the first byte of a cache line. The arrays lie far apart, so no two of them share a line.
Part 1: Full scans
Part 1 uses only SCAN, which reads the named fields of record 0, then record 1, and so on through record n - 1.
- Reading a field touches every line that holds at least one of its bytes. A field that crosses a line boundary touches two lines, and one longer than a line can touch more.
- A loop's count is the number of distinct lines it touches. A line touched several times counts once, as if the cache kept every line it loaded.
- Each loop command is counted on its own, starting from no lines touched.
- The SoA count is the sum over the field arrays.
When R does not divide 64, records straddle line boundaries, so a field can cross a boundary even though every array starts on one.
Output: one line per loop.
aos soa winner
aosis the AoS count;soais the SoA count.winnerisAOSwhen the AoS count is smaller,SOAwhen the SoA count is smaller, andTIEwhen they are equal.
Limits
- Field names are distinct and use lowercase letters and underscores.
- At most 16 fields, each between 1 and 256 bytes long.
nis between 1 and 100,000. At most 100 commands. AGATHERlists between 1 and 1,000 record indices.- Every address stays below 231, though the reference solutions compute addresses in 64-bit integers (
std::int64_t).
Tests 1–3 cover this part.
Part 2: Strides and gathers
STRIDE fields k reads every record whose index is a multiple of k and below n.
kis between 1 and 109.STRIDE fields 1is the same loop asSCAN fields. A stride ofnor more reads only record 0.
GATHER fields i1 i2 ... reads the named fields of the listed records, in the listed order.
- Each index is between
0andn - 1. Indices may repeat and come in any order. - A record read a second time adds no lines, because its lines are already counted.
Tests 4–7 and 11 cover this part.
Part 3: A misaligned base
OFFSET m, with m between 0 and 63, moves the start of every array (the AoS array and each SoA array) to m bytes past a line boundary.
| Layout | Field f of record r then starts at |
|---|---|
| AoS | address m + r * R + offset(f) |
| SoA | m + r * size(f) bytes after the line boundary where its array begins |
- The SoA arrays still never share a line.
- The offset starts at 0 and applies to every later loop until the next
OFFSET.OFFSET 0restores the aligned layout.
Tests 8–10 cover this part.
5 sequence 8 symbol 8 price 8 qty 4 pad 4 8 4 SCAN price SCAN qty,price SCAN sequence,symbol,price,qty SCAN sequence,symbol,price,qty,pad
4 1 SOA 4 2 SOA 4 4 TIE 4 5 AOS
The record is the course's MarketUpdate with its 4 bytes of tail padding declared as pad. R = 32, so two records fill each line.
AoS: the 8 records fill 256 bytes, lines 0 to 3, and every loop reads a field in each of them. Every AoS count is 4.
SoA: the 8 price values fill exactly one line, and the 8 qty values use 32 bytes of one. So price touches 1 line and qty,price touches 2.
The four real fields touch one line each, a tie. Adding pad costs SoA a fifth line, while the AoS lines already held those bytes.