Book Deep-Read · A Philosophy of Software Design · Chapter 20
A Philosophy of Software Design · Ch 20 · Designing for Performance · Ousterhout · 2nd ed 2021
You tap an app and it spins for three seconds — somewhere, a piece of code is "too slow." Many programmers believe that to be fast, code has to be twisted and hard to read. This chapter's author says the opposite: most of the time, clean and simple code is already the faster code.
The author once tore down and rewrote a piece of his team's code that packs up messages, aiming to make it faster. The new version ran nearly twice as fast — and it was shorter. Here, fast and simple turned out to be the same thing.
Two common extremes both go wrong. Some people squeeze for speed from day one and turn the code into a tangle. Others ignore speed entirely, and when the system finally crawls they discover it is a little slow everywhere — with no single spot to fix.
Day to day, write simply and pick the faster option when it costs nothing. When you truly need speed, measure where it's slow first, then make the most-traveled road short and straight. One honest cost: redesigning that main road often means tearing up existing structure, which is real work — so do it only after measuring shows it's worth it.
Simple is usually fast; to go faster, measure first, then straighten the road most traveled.
Want the orders of magnitude between operations, the concrete critical-path redesign recipe, the RAMCloud rewrite, and why Google says "write it simply and optimize later" is often wrong? → Switch to the deep read
You think performance and clean code are enemies; this chapter's conclusion is that clean design and high performance are largely compatible, and complicated code tends to be slow — because it does extraneous or redundant work. The author's approach to performance compresses into three steps: develop a feel for what is expensive and pick the faster of equally clean options; measure before you modify; when you need a fundamental improvement, redesign around the critical path.
malloc in C or new in C++.Chapter 20. Right after Chapter 19 tests software fashions against complexity, this chapter tests another common excuse — "we had to make it complicated for performance." It leads into Chapter 21, "Decide What Matters." In practice it maps onto every review that asks "should we add a cache or a new structure to make this faster?"
The first nineteen chapters urge you to write simple code. A fair question is: won't simple be slow? If the answer is yes, the book's advice collapses for performance-sensitive systems.
Real teams fail in two ways. One is optimizing everything: adding special branches, hand-rolled caches, and broken abstractions to save a few nanoseconds — the code becomes hard to read, and most of the savings land where they don't matter. The other is ignoring performance entirely: every module is "a bit slow," the system ends up several times slower, and there is no obvious hot spot to attack. The chapter offers a middle road: get most of the performance you need for the least complexity.
The author does not recommend heavy performance work from day one, nor ignoring it. His approach: among several equally natural alternatives, pick the more efficient one — provided it is just as clean. To do that you need to know which operations are fundamentally expensive. The book names four:
malloc / new carry significant overhead for allocation, freeing, and garbage collection.Knowing this gives everyday choices a basis. The author's examples: for a large table looked up by key, a hash table is typically several times faster than an ordered map; in C/C++, allocating an array of structures as one block usually beats allocating each separately and storing pointers. These choices are no more complicated — you just have to know. When unsure, write a micro-benchmark.
This is the chapter's most counterintuitive line: simpler code tends to run faster. Three reasons. First, if you have defined special cases and exceptions out of existence (Chapter 10), no code is needed to check for them. Second, deep classes are more efficient than shallow ones: each call does more work and crosses fewer layers, and every layer crossing adds overhead. Third, complicated code often does extraneous or redundant work.
So what if the faster design really is more complex? The author weighs cost against payoff. If it adds only a little complexity and the gain is significant (say, adding a cache), it may be worth it. If it adds a lot of implementation complexity or complicates interfaces, start with the simpler approach and optimize later if performance becomes a problem. But if there is clear evidence that performance is the central goal, design for it from the start — the author's RAMCloud project aimed for the lowest possible remote-access latency, so it used networking that bypasses the operating-system kernel and talks to the network card directly, despite the extra complexity.
When a system really is too slow, "fixing it by intuition" is the most common mistake. Even experienced programmers' performance intuitions are unreliable, the author says; acting on them often spends effort where it doesn't matter while adding complexity. Measurement serves two purposes: finding the place where a change will have the biggest impact — knowing "what's slow" isn't enough; you want the point with the most leverage — and setting a baseline so you can re-measure afterward and confirm things actually improved. If a change makes no measurable difference, back it out — unless it also made the system simpler. Keeping it only keeps the complexity.
The best optimizations are often "fundamental": adding a cache, or switching algorithms (say, from a balanced tree to a hash table). When none is available, the author's method is to redesign around the critical path:
if at the start detects every special case, and everything else moves to a separate slow path. Reshape data structures if needed — for example, combining several scattered conditions into one value that can be tested once.RAMCloud is the all-in-memory key-value store built by the author's group; its Buffer class holds variable-length RPC request and response messages. A Buffer is made of memory chunks, whose storage either belongs to the Buffer (about 1KB inside the object, with more allocated as needed) or comes from outside (such as network packets).
In the old version, allocating new space — the most common operation — passed through several layers of method calls, each mostly forwarding arguments, with special-case checks along the way. The author rewrote it as a critical path: the common case needs just one test, "is there enough room at the end of the last chunk?" If so, carve the new region right there and return; every other situation — new chunks, insufficient space, external memory — goes to the slow path. The key operation got nearly twice as fast (the book measures about 8.8 ns down to 4.75 ns), and the new code was about 20% smaller. Performance and simplicity won together.
Table 1 · Is the faster option worth it? Graded by cost
| Situation | Example | Author's advice | Cost / risk |
|---|---|---|---|
| Fast option equally clean | Hash table vs ordered map; one block for an array of structs | Just pick the fast one, as a habit | Near zero; needs only a feel for costs |
| Small complexity, big gain | Adding a cache | Worth considering | New state to maintain: invalidation, consistency |
| Big complexity, unclear gain | Special branches everywhere for a few ns | Write it simply; optimize when measured | If everything is slow, hot spots are hard to find later |
| Performance is the core goal | RAMCloud's kernel-bypass networking | Design for it from the start | More complex to build and deploy |
| System is already too slow | RAMCloud Buffer rewrite | Measure first, then redesign the critical path | May mean tearing up structure; real work |
Rule of thumb: a performance change must clear two bars — does it measurably help? If not, back it out; is the complexity worth it? Changes that make the critical path shorter and straighter often win on both.
The chapter's value is dismantling the "performance vs. readability" opposition: most of the time, deep modules with few special cases are exactly the fast code; the places that truly require sacrificing clarity are rare, and measurement should decide them.
Industry evidence · publicly verifiable
alloc() opens with exactly this chapter's fast-path test, commented "There is extra space at the end of the current last chunk, so we can just allocate the new region there." Source: PlatformLab/RAMCloud README · src/Buffer.ccIn daily work: when a review shows a complex change "for performance," ask for the measurements and the baseline; when choosing data structures, count network round trips, disk I/O, and allocations as the "expensive things"; when you inherit slow code, first ask "what is the least the common case must do?", then see what the existing code does beyond that. In an interview asked "how would you optimize this?", leading with measurement and then the critical path sounds far more experienced than reciting tricks.
① Clean design and high performance are largely compatible; complicated code is often slow because it does extraneous or redundant work.
② Have an order-of-magnitude feel for what's expensive: network round trips, disk / flash I/O, dynamic memory allocation, cache misses.
③ Among equally clean options, pick the faster one — it costs almost nothing.
④ Small complexity for a big gain: do it. Big complexity: start simple. Performance as the core goal: design for it from day one.
⑤ Measure before modifying: find the point of greatest impact, keep a baseline; back out changes with no measurable gain.
⑥ Prefer fundamental fixes — a cache, a better algorithm; failing those, redesign around the critical path.
⑦ Critical path: write the ideal code for just the common case, guard every special case with one if, move the rest to a slow path.
⑧ Evidence: the RAMCloud Buffer rewrite ran nearly 2x faster with ~20% less code; Google's "Performance Hints" also says pick the faster option when readability doesn't suffer, and warns of the "flat profile."