Expert · Module 23 · Lesson 2 of 4
Struct of arrays vs array of structs
Laying out the same fields in two orders changes how many cache lines a one-field loop pulls in. Count the fills for both layouts and learn when each one wins.
By the end you can
- Count the line fills a single-field sum touches under both layouts
- Explain why field-by-field layout maps onto wide SIMD loads
- Pick a layout from the access pattern and name what it costs
Builds on Caches: why neighbors are fast, Cache lines and prefetching.
01 · A question first
A loop adds up one field of a million records. You then add two unrelated fields to each record, so the loop still reads the same field and does the same additions, but it now waits for about three times as many cache lines. Big-O did not move. What did?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- The record that got heavier
- Two layouts, same bytes
- Counting the fills
- Why one wide load beats four addresses
- Column stores do this at scale
- What the struct of arrays costs
- Where each layout wins
- Pseudocode & Multi-Language Implementations (C++, Java, Python)
03 · Practice
Something wrong with this lesson? Report a problem