knaxuslearn · build · ship

Roadmaps / C++ / Phase 3

Phase 3 of 4 · Study weeks 9-12

Performance on real hardware

This phase makes the third checkpoint possible: a core that meets the 5 microsecond target and shows why.

Topics every week

  1. Week 98 h
    • Cache lines, prefetching and branch prediction
    • Data-oriented layout
    • Reading perf stat counters

    Hand inA counter table for the three book layouts

  2. Week 108 h
    • Benchmarking honestly: warm-up, spread and noise
    • Google Benchmark
    • Percentiles and coordinated omission
    • CPU frequency and pinning

    Hand inA benchmark with p50, p99 and p99.9 and a note on what the machine did to it

  3. Week 118 h
    • Profiling with perf and flame graphs
    • Hidden allocations and copies
    • SIMD basics
    • Compiler flags, LTO and PGO

    Hand inA flame graph that finds a seeded slowdown, and the fix

  4. Week 128 h
    • Choosing a layout from evidence
    • Writing a benchmark report
    • Performance regressions in CI

    Hand inCheckpoint 3, with predicted and measured numbers side by side

Practice projects

  • Practice 1, the layout comparison (Week 9): build the book three ways. Done when: a table shows allocations, cache misses and p99 for each, and you say which wins and why.
  • Practice 2, the honest benchmark (Week 10): run the same benchmark pinned, unpinned and with the frequency governor changed. Done when: the table shows how much the setting moves the result, and your report states the setting.
  • Practice 3, the hunt (Week 11): add a log call that allocates on the hot path. Done when: a flame graph points to it, the fix restores p99, and a CI check fails if allocations return.
  • Optional, PGO and LTO: build with both. Done when: a table shows the gain, or you report that it was under 5% and say why.

GitHub repos to study

Existence and last commit were checked on 22 Nov 2026 by shallow clone. Every repository below had a commit in the last six months.

RepoWhat to readWeek
google/benchmarkStatistics, counters and the DoNotOptimize helpers10
wolfpld/tracyAn instrumenting profiler and how it keeps overhead low11
fmtlib/fmtCompile-time format checking and the cost of formatting11

Milestone checkpoint

Build Checkpoint 3. Write your prediction for p99 under each layout first. Run the benchmark with the same settings every time, collect the counters, then find the seeded allocation from a flame graph. Hand in the comparison table with your own numbers, the perf summaries, the benchmark report and ADR-5 (the layout and the band assumption).

Check yourself

Answer each question in your head first, then open it to check.

  1. Why does a tree of small nodes often lose to a flat array?
    Each node is a separate memory access, often a cache miss. A flat array reads neighbouring memory, which the cache and prefetcher serve cheaply.
  2. What does a branch misprediction cost, and how can you see it?
    About 10 to 20 cycles on a modern core. perf stat shows the branch-miss counter and its rate per message.
  3. Why report p99 and not the mean for a latency target?
    The mean hides rare slow messages, and a strategy sees those. Report percentiles with the load that produced them.
  4. What can make a benchmark result wrong while looking precise?
    Dead-code removal, a cold cache, frequency changes, other processes and an unrealistic input. Run many times, pin the core and report spread.
  5. When is PGO worth the build complexity?
    When a hot path has branches that the profile predicts well and you ship one binary many times. Measure first.