I stand behind the message of the previous chapter—the first lesson of optimization is, in fact, “Don’t optimize.” However! This strongly expressed point of view, alone among the many strongly expressed points in this book, prompted immediate dissent from a large subset of my teammates at Sucker Punch.

It’s only fair to give their well-reasoned objections a hearing! I now present the opposing points of view in the form of an imagined Socratic dialogue between me and the many dissenters, whom I’ve merged into a single character for dramatic purposes. They have all been given a chance to review this chapter to make sure their views are fairly represented.

The Dissenter: I formally lodge my disagreement with the premise of this chapter. 1

Chris: I thought this chapter was just common sense. Didn’t you see the Knuth quote? “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil!”

The Dissenter: That quote has been used to justify all sorts of code with atrocious performance, and you’re just encouraging more of it.

Chris: Wow. There’s an emotional undercurrent to that feedback. Perhaps that’s because you’ve had to spend too much time reworking other people’s code to fix performance problems that shouldn’t have been there to begin with? And also waiting for popular video games to boot?

The Dissenter: Yes. And yes.

Chris: And you work on performance-critical parts of our codebase, which probably leads to a different set of priorities than someone working on, say, the logic behind our user interface.

The Dissenter: That is true, though I’d note that we both know of a game 2 whose user-interface architecture was so ill-conceived that its performance problems were deemed unfixable. The entire user interface had to be thrown out and rebuilt, and the game missed its ship date by six months as a result.

Chris: Yes. Rule 20, “Do the Math” , applies to that case. In retrospect, they should have realized how bad their architecture was and fixed it much earlier in the project. Really big performance problems tend to show up right away—but only if you measure them. I could imagine a fourth rule of optimization: “Assume your code will be fast enough, but measure it anyway.”

The Dissenter: I would be slightly mollified by this. The biggest reason our end-of-project optimization challenges have been manageable is that we have accurate profiling tools and use them as part of our day-to-day engineering loop.

Chris: Yes. I think this is the Sucker Punch equivalent to the testing focus a lot of coding teams have. We don’t have a lot of unit tests because we’re willing to let a few bugs sneak in, but we’re less willing to be surprised by performance problems.

The Dissenter: Still, I think your argument for Rule 5 misses an important point. It would be easy to read it as “Don’t worry about optimization,” but what you’re really saying is, “Simple code is easy to optimize, so write simple code.”

Chris: Yes, that’s right. This fits into Rule 1 and the overall theme of the book: make your code as simple as possible, but no simpler. One of the benefits of this approach is that your code will be easy to optimize.

The Dissenter: But even given that, when you write simple code, you’re thinking about how it could be faster if it had to be. This has definitely come up when I’ve done a code review on your code. Or when you did a code review on my code. Actually, probably both.

Chris: Absolutely, we all think about that. That’s not the first priority for code—correctness and simplicity are—but scouting out your escape route for optimization is good practice, even if it doesn’t prove necessary. And it usually doesn’t.

The Dissenter: It’s true that optimization often isn’t free. If an optimization makes the code more complicated, uses more memory, or adds some pre-processing step, then the performance payoff better be worth it. Faster code isn’t strictly better code. On that we agree.

Chris: Good!

The Dissenter: I’d also like to say that while simple code may be easy to optimize, being slow doesn’t make code simple. In fact making code too complicated is one of the easiest ways to make it slow.

Chris: Absolutely.

The Dissenter: I’ve got to say, this Rule doesn’t really capture what most of the optimization work I do is like. Generally, I’m not optimizing some bit of new code—I’m trying to squeeze more performance out of some code that’s already been optimized. That’s a lot harder.

Chris: Yes. The chapter really is about writing new code.

The Dissenter: Right , but even then, when I’m adding new code to some system I already know is performance critical, I have to think about performance from the start. I can’t just write simple code and hope for the best.

Chris: That’s probably true, or true at least enough of the time to make it a reasonable first step. Would you agree that worrying about performance from the start has probably led to at least some code that was more optimized than it needed to be?

The Dissenter: I grudgingly accept this, but I think it’s uncommon. I still save time overall by not writing code that immediately needs to be optimized.

Chris: I’d buy that. Even Knuth’s rule stops at 97%, right? If you’re confident based on past experience that you’re working in the 3%, then it’s reasonable to consider performance in your first implementation. Just don’t get carried away until you’ve measured your code and discovered a problem. And if everyone on your team thinks they’re working on the 3%, then you all need to do a better job profiling the code.

The Dissenter: The other thing about working on optimized code is that the gains are smaller. I’ll buy the idea that you can usually make new code 5 or 10 times faster without a lot of work. But at some point you exhaust the easy ideas, and then performance gains are a lot harder to find.

Chris: Yes, and at that point the rules change. You’re more likely to halve your execution time with five small changes than with one big change. But even then, you’ve got to be alert to the idea that there could be a bigger algorithmic fix. For example, we spent weeks of effort optimizing the main draw loop in the first Sly Cooper game. We were eking out tiny fractions of performance at a time—only to discover that switching to a spatial partitioning system quintupled its performance.

The Dissenter: That was before my time. Cool story, though.

Chris: How about the five-step optimization process?

The Dissenter: Pretty solid. I was OK with that part of the chapter.

Chris: I can’t believe none of you commented on the brilliant insight of “Step 2: Make Sure There’s Not a Bug” . I was proud of that step.

The Dissenter: I indicated my appreciation of Step 2 by not criticizing it. Don’t expect a lot of praise, Chris. None of us want to deal with an even more self-confident version of you .

Chris: Fair enough.

1 This is a direct quote.

2 Which we will not name.