Sub topic 9
Splitting hot and cold data
This chapter assumes you know cache lines, struct padding, and the array-of-structs versus struct-of-arrays trade-off from earlier chapters. It also assumes you can read C.
The problem: cold fields ride along
A cache line is loaded whole. If a loop reads one 4-byte field from a 120-byte struct, the other 116 bytes are loaded too, and they displace useful data.
Hot data is read or written on most iterations of the performance-critical loop. Cold data is touched rarely, such as error messages, debug names, or configuration. Splitting means storing the two kinds in separate places so the hot loop only pulls hot bytes into cache.
Worked example
Suppose a game keeps a list of entities. Each frame, the update loop reads only the position and the alive flag.
#include <stddef.h>
#include <stdint.h>
/* Before: hot and cold fields share one struct (about 112 bytes). */
struct EntityMixed {
float x, y;
uint8_t alive;
char name[64];
char description[32];
uint32_t created_at;
};
float sum_x_mixed(const struct EntityMixed *e, size_t n) {
float s = 0.0f;
for (size_t i = 0; i < n; i++)
if (e[i].alive)
s += e[i].x;
return s;
}Each element is larger than a 64-byte line, so every element costs at least one fresh line, and the loop uses about 5 of its bytes.
Now split the struct. The hot part stays small and contiguous. The cold part lives in a parallel array, found by the same index.
#include <stddef.h>
#include <stdint.h>
struct EntityHot {
float x, y;
uint8_t alive;
}; /* 12 bytes with padding: 5 elements per 64-byte line */
struct EntityCold {
char name[64];
char description[32];
uint32_t created_at;
};
struct World {
struct EntityHot *hot;
struct EntityCold *cold;
size_t n;
};
float sum_x_split(const struct World *w) {
float s = 0.0f;
for (size_t i = 0; i < w->n; i++)
if (w->hot[i].alive)
s += w->hot[i].x;
return s;
}Now a line holds about five elements instead of less than one. Code that needs a name uses w->cold[i], and it pays the extra cache miss only when it does.
Variations
- Pointer to cold part. Keep one cold struct per object and store a pointer or index in the hot struct. This suits objects that are not in a flat array, but each hot struct grows by 4 or 8 bytes.
- Order by frequency. If you cannot split, put the fields used together first so they share the first cache line.
- Hot/cold by path. In a hash table, keep keys and small values in the probe array and put large values elsewhere.
Common mistakes
- Guessing which fields are hot. Measure with a profiler or counters first.
- Splitting fields that are always used together. You then pay two cache misses instead of one.
- Letting the two arrays drift out of sync when you insert, delete, or sort. Operate on both with the same index.
Exercise
A struct has an 8-byte key, an 8-byte count, and a 200-byte log message. A loop scans one million entries and sums the counts. Which fields belong in the hot struct, and why does this reduce cache misses?
Show answer
The key and count (16 bytes) go in the hot struct, if the loop also needs the key. Only the count is needed to sum. The log message goes in a cold array. Each 64-byte line then holds four 16-byte hot entries instead of less than one 216-byte entry, so the scan loads about 4 to 14 times fewer lines.