Day 317 — Memory layout, and why one loop is four times faster

Saturday, 19 June 2027 · Week 46 of 52 · Production · Study day · 60 min

The timing difference you found last night has nothing to do with the language and everything to do with what the cache line contains, which is the single most useful performance idea in this week.

What you should be able to do tonight

  • Explain the array-of-structs against struct-of-arrays result in terms of cache lines and prefetching.
  • Say what that implies for how a tick database should be laid out, and connect it to your Parquet work.

Tasks 60 min

30 min Explain your measurement: a cache line brings in 64 bytes, so iterating over one field of a struct array wastes most of every line, while a columnar layout uses all of it. Then vary the struct size and plot the timing ratio against it, which makes the mechanism visible rather than asserted.

20 min Connect it back: Parquet is struct-of-arrays on disk for the same reason, and your day 311 measurement of bytes read is the same effect one level up the hierarchy. Write the connection down, since it is the answer to the tick database question you will be asked.

10 min Add the second effect: measure a random access pattern over the same array against a sequential one, and report the ratio. Prefetching only helps when the access is predictable.

Resources

Deliverable

Timing ratio plotted against struct size, plus a random-against-sequential access comparison and the written connection to Parquet.

Checkpoint

Why is a columnar layout faster for a scan, and how does that answer the question about storing a tick database?

Prepare for tomorrow

Ten minutes tonight so Day 318, Sunday, 20 June 2027 starts clean. This is what the evening email sends you.

Read the cppreference pages for vector, unordered_map and map, and note the complexity guarantees of each operation.

Then write down which you would use for an order book’s price levels and which for its order lookup.

Two choices with reasons.