Let's test your knowledge. Click the correct answer from the options.
You have an Array-of-Structs (AoS) layout and a tight numeric loop that computes a moving average over ticks
:
TEXT/X-C++SRC
1#include <vector>
2struct Tick { double price; int size; uint64_t ts; };
3
4void process(std::vector<Tick>& ticks) {
5 double sum = 0.0;
6 for (size_t i = 0; i < ticks.size(); ++i) {
7 // hot inner loop touching only `price`
8 sum += ticks[i].price;
9 }
10 (void)sum; // keep result to avoid optimizing away
11}
Which of the following is the best first action to determine whether poor data locality (AoS vs SoA) and cache behavior are causing a performance problem?
Click the option that best answers the question.
- Run a low-level profiler (e.g., perf) to measure cache-references/cache-misses and find the hot spots before changing layout.
- Immediately refactor the data into a Struct-of-Arrays (SoA) and compare wall-clock times.
- Increase the number of threads to hide cache misses by parallelizing the loop.
- Just compile with `-O3 -march=native` and assume the compiler will auto-fix layout and vectorize the loop.
- Use a source-level debugger (gdb) to step through the loop and inspect memory addresses.