Master the fundamental concepts of cache optimization through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksArray of Structures (AoS) stores all fields per object together. Structure of Arrays (SoA) stores each field in its own contiguous array. Game engines and SIMD kernels prefer SoA when loops touch one field across many entities. AoS wins when you always load whole records together.
Updating positions for 100k particles: SoA lets you vectorize x[], y[], z[] without gathering scattered fields. Printing full records: AoS avoids three pointer chases.
cLoading…
Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will benchmark AoS vs SoA for a field-heavy update loop and report speedup. This exercise asks you to tie the winner to which fields the inner loop reads.
A loop that updates only positions and velocities still drags every field of an array of structures (AoS) through the cache. A structure of arrays (SoA), or a hot/cold split, loads only the bytes the loop touches. Model all three layouts for a particle system, lay them out exactly as a C compiler would, and run the loop's address stream through a small LRU cache to see how many bytes each layout actually fetches.
cLoading…
i * sizeof(struct).N * size(f) bytes, rounded up to a whole number of lines.For each particle i = 0..N-1, the loop touches its fields in the order given. Each layout starts with an empty cache (set-associative, LRU, as in a real L1). Useful bytes = N times the sum of the touched fields' sizes.
cLoading…
Bytes fetched = misses * LINE. The percentage is used / fetched, rounded half up to 1 decimal with integer arithmetic. The cold struct size is 0 when every field is hot. Errors: field NAME: size must be 1, 2, 4 or 8, loop: unknown field NAME, and run: need fields and a loop.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover mixed field sizes with padding, a loop that touches every field (AoS catches up), a single hot field, a small direct-mapped cache where the SoA arrays conflict with each other, and invalid field sizes and names.