A small book about making code faster
Performance Hints
Jeff Dean and Sanjay Ghemawat spent years writing down what they actually do when they make a program faster. This is a walk through that list — estimation, measurement, and the handful of moves that keep working.
Everybody quotes half a sentence of Knuth and stops thinking. The other half says to not pass up the opportunities in the critical 3%. This book is about how you find that 3% — and about the fact that if you never think about speed while you write, the profiler will hand you a flat line and no place to start.
Contents
- Why Bother Thinking About Speed The 3% Knuth actually meant, and the flat-profile trap.
- Estimation: Arithmetic Before Code The numbers table, and two worked back-of-envelope calculations.
- Measurement, and What a Flat Profile Means Profilers, microbenchmarks, and the moves for when nothing sticks out.
- The Shape of an Interface Bulk APIs, view types, precomputed arguments, thread-compatible types.
- Algorithmic Improvements The category with the most leverage, and how to spot it.
- Better Memory Representation Cache lines, padding, field order, and indices instead of pointers.
- Cheaper Containers Batched and inlined storage, flattened maps, arenas, arrays, bit vectors.
- Reduce Allocations Three costs per allocation, and the patterns that remove them.
- Avoid Unnecessary Work Fast paths, precompute, defer, specialize, cache — and helping the compiler.
- Cheatsheet The numbers, the checklist, and the counter-indications on one page.
How to read this
The chapters follow the original document's order, and that order is itself the argument. You estimate before you build, measure before you change, and then work down a ladder of leverage: the shape of the interface, then the algorithm, then how the data sits in memory, then how often you allocate, then how much work you can skip outright. Each rung is cheaper to climb than the one below it is to fix later.
The examples are C++, because the source document is C++. Don't let that put you off if you write something else. Almost nothing here is about C++ — it is about cache lines, counted operations, and how many times you cross a boundary. Those bills arrive in every language; only the syntax for avoiding them changes.
Everything in this book is drawn from Performance Hints by Jeff Dean and Sanjay Ghemawat, published as part of the abseil Performance Guide. The measurements, the latency table and the percentage improvements are theirs; the explanations, the diagrams and any mistakes are this book's.