Leanpub Header

Skip to main content

Bare-Metal Performance Engineering in C++

Profiling with perf and Top-Down Microarchitecture Analysis to Build Benchmarks That Don’t Lie

Bare-Metal Performance Engineering in C++
This book is 95% completeLast updated on 2026-11-16

A hash function benchmarked at four times faster ships, and the service doesn’t move, because the compiler deleted the work being measured. Most C++ performance work fails like this: confident numbers, wrong conclusions. Bare-Metal Performance Engineering in C++ follows one real request-handling service from 50,000 cycles per request down to about a quarter of that, showing every measurement, every bottleneck, and every fix along the way.

Free With Membership

With Membership

Free!

$39.99

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
About

About

About the Book

Your benchmark says 4x faster and production doesn’t move. This book is about why, and how to make your numbers tell the truth.

Most C++ performance advice is folklore: virtual calls are slow, exceptions are free, -O3 beats -O2, more threads means more throughput. Each of these is true on some hardware, for some workloads, some of the time, and teams lose weeks acting on them without measuring. Meanwhile the real costs go unseen: a compiler that quietly deleted the code being benchmarked, a profiler that blamed the instruction after the one that missed cache, a hash table that waits on DRAM twice per lookup, a load test that misses the stalls production clients hit.

Bare-Metal Performance Engineering in C++ teaches the discipline that replaces guesswork with evidence. You’ll learn to write benchmarks the compiler can’t fool, to read hardware counters without being misled by multiplexing or skid, and to use Top-Down Microarchitecture Analysis to find in one run whether code is starved in the front end, losing work to mispredictions, or stalled on memory. Then you’ll apply that method from the compiler out to the kernel, and keep the results in place in production.

The book follows a single realistic workload from start to finish: a request-handling service that parses JSON, looks keys up in a multi-million-entry hash table, and serializes responses. Every chapter ends with a Bottleneck Ledger entry recording the service’s cycles per request, IPC, cache misses, and latency percentiles, the dominant bottleneck at that stage, what changed, and how the change was verified. By the end, the service uses about a third to a quarter of the CPU per request it started with, with a fraction of its original tail latency, and every step of that improvement is shown and justified.

What you’ll learn
  • How microbenchmarks lie through dead code elimination, constant folding, and hoisting, and how to prove they don’t with assembly and statistics
  • How to control frequency scaling, turbo, SMT, and core isolation so that a 2 percent difference means something
  • How the front end, out-of-order back end, branch predictor, SIMD units, caches, TLBs, and NUMA actually determine your code’s speed, with differences across Intel, AMD, and Arm called out
  • How to use perf stat, perf record, precise events, call graphs, flame graphs, and differential profiles correctly
  • How to diagnose bottlenecks with Top-Down analysis using perf, toplev, and VTune
  • How to read x86-64 and ARM64 compiler output well enough to spot missed vectorization, spills, bounds checks, and hidden calls
  • How to find time spent waiting rather than computing, using off-CPU profiling, eBPF, Perfetto, LTTng, and Intel Processor Trace
  • How to get the most from the compiler with LTO, PGO, AutoFDO, BOLT, and optimization remarks
  • What C++ abstractions really cost under measurement: virtual dispatch, exceptions, std::function, and template bloat
  • How to apply data layout, allocation, container, branchless, SIMD, and loop-transformation techniques, each shown as a measured before-and-after pair
  • How to scale concurrency: diagnose false sharing, coherence traffic, and lock contention, and choose between locks, sharding, and lock-free snapshots
  • How to tune at the system level: system call costs, page faults, io_uring, interrupt affinity, and transparent huge pages
  • How to keep it fast with HDR histograms, open-loop load testing, continuous profiling, and regression gates in CI
Who this book is for

This book is for C++ engineers who own the performance of code that runs in production: backend and infrastructure engineers with latency objectives, library maintainers, low-latency and trading developers, and anyone who reviews pull requests that claim speedups. It assumes you’re comfortable with modern C++ and basic computer architecture terms like caches and pipelines. It doesn’t assume prior experience with perf, assembly, or Top-Down analysis, because each tool is introduced from first principles where it’s needed.

It isn’t an algorithms book. It covers what happens after you’ve chosen the right algorithm, when two implementations of it differ by a factor of five because of how they use the machine.

What makes it different
  • Measurement first. Every performance claim is either measured with the method shown or framed as something you must measure. Figures are given as realistic ranges, not invented benchmark results.
  • Real code. The listings are complete, modern C++20/23 and are meant to compile, with the error handling real benchmarks and profiling harnesses need. Assembly listings state the compiler, version, and flags.
  • Hardware-aware. Claims name the microarchitecture they apply to, and the book points out where Intel, AMD, and Arm cores behave differently.
  • Trade-offs stated up front. Every technique comes with its costs: code size, memory, semantics, portability.
  • Production-ready. The final part covers tail-latency measurement, continuous profiling, CI regression detection, and a step-by-step investigation runbook.
What you need
  • A Linux machine, either x86-64 or AArch64, ideally bare metal, since many cloud VMs expose few or no hardware counters
  • GCC 13 or newer, or Clang 17 or newer
  • CMake and Google Benchmark
  • perf

VTune, AMD uProf, bpftrace, and Perfetto are used in later chapters. Four appendices give quick references for perf, hardware events and Top-Down metrics, compiler flags, and a full benchmarking and profiling checklist.

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ systems engineer who builds software where being wrong or slow has a cost. He came up through quantitative trading, building order books, matching logic and the low-latency infrastructure underneath them and now brings the same discipline to performance engineering, concurrency, storage, networking, GPUs, and AI inference.

His rule for performance-critical code is simple: nothing is true until it has been profiled, fuzzed, and replayed. Every technique in his books comes with the measurement that proves it, the trade-off it costs, and the failure it prevents. He writes for working engineers who ship and debug production C++, and he leaves out anything that only works on a slide.

Get the free Community Edition

You can get the free Community Edition in PDF or EPUB just by sharing your name and email address with the author.

 

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub