Laying out a market data message
A feed handler stores each order update in a struct with a one-byte side, an 8-byte double price, a 4-byte quantity, a one-byte flag and an 8-byte order id, declared in that order. The engine scans millions of these a second on a 64-bit machine with 64-byte cache lines.
Rubric and preparationWhat a mid answer covers · 3 lessons to prepare
Mid standard
- I would order the fields largest first, the two 8-byte fields, then the quantity, then the two single bytes, because that leaves padding only at the end. The struct becomes 24 bytes, so more than two updates fit in a 64-byte line instead of exactly two.
- I would keep prices in their own contiguous array, a struct of arrays, because the loop then reads 8 prices per 64-byte cache line instead of pulling in whole updates it mostly ignores. I would also store the price as a fixed-point integer, which avoids floating point rounding in comparisons.
- I would add static_assert checks on sizeof and offsetof so the layout cannot drift, then measure the scan before and after with a benchmark on realistic data. I would also read cache misses from hardware counters, because a faster run with the same misses would mean something else changed.