My name is Kurt, and I’m a code-acholic.
I have been writing software for over 35 years. I’ve never worked at Microsoft, or Google, Facebook, Apple, or anywhere else famous.
But beyond a few short vacations, I have written code every day of that time. I have spent the last 20 years almost exclusively writing C++ and talking to other very bright developers about C++.
This is my qualification to write a book about optimizing C++ code. I have also written a lot of English prose, including specifications, comments, manuals, notes, and blog posts.
It has amazed me from time to time that only half of the bright, competent developers I have worked with can string two grammatical English sentences together.
One of my favorite quotes comes by way of a letter from Sir Isaac Newton, in which he writes, “If I have seen farther, it is by standing on the shoulders of giants.”
I too have stood on the shoulders of giants, and particularly have read their book: elegant little books, like Brian Kernighan and Dennis Ritchie’s The C Programming Language; smart, ahead-of-the-curve books, like Scott Meyers’s Effective C++ series; challenging, mind-expanding books, like Andrei Alexandrescu’s Modern C++ Design; careful, precise books, like Bjarne Stroustrup and Margaret Ellis’s The Annotated C++ Reference Manual. For most of my career, it never crossed my mind that I might someday write a book. Then one day, quite suddenly, I found I needed to write this one.
So why write a book about performance tuning in C++?
At the dawn of the 21st century, C++ was under assault. Fans of C pointed to C++ programs whose performance was inferior to supposedly equivalent code written in C.
Famous corporations with big marketing budgets touted proprietary object oriented languages, claiming C++ was too hard to use, and that their tools were the future. Universities settled on Java for teaching because it came with a free toolchain.
As a result of all this buzz, big companies made big-money bets on coding websites and operating systems in Java or C# or PHP. C++ seemed to be on the wane. It was an uncomfortable time for anyone who believed C++ was a powerful, useful tool.
Then a funny thing happened. Processor cores stopped getting faster, but workloads kept growing. Those same companies began hiring C++ programmers to solve their scaling issues. The cost of rewriting code from scratch in C++ became less than the cost of the electricity going into their data centers. All of a sudden, C++ was popular again.
Uniquely among programming languages in wide use in early 2016, C++ offers developers a continuum of implementation choices, ranging from hands-off, automated support to fine manual control.
C++ empowers developers to take control of performance trade-offs. This control makes optimization possible.
There are not many books on optimization of C++ code. One of the few is Bulka and Mayhew’s meticulously researched but now somewhat dated Optimizing C++. The authors appear to have had similar career experiences to mine, and discovered many of the same principles.
For readers who are interested in another take on the issues in this book, their book is a good place to start. Also, Scott Meyers, among many others, covers avoiding copy construction extensively and well.
There are enough different things to know about optimization to fill 10 books. I have tried to pick and choose things that seemed to occur frequently in my own work, or that offered the biggest performance wins.
To the many readers with their own performance tuning war stories who may wonder why I’ve said nothing about strategies that worked miracles for them, all I can say is, so little time, so much to tell.
I welcome your errata, comments, and favorite optimization strategies at [email protected].
I love the craft of software development. I enjoy endlessly practicing the kata of each new loop or interface.
At the corner of Sonnet and Science, writing code is a skill so esoteric, an art form so internal, that almost nobody but another practitioner can appreciate it.
There is beauty in an elegantly coded function, and wisdom in a powerful idiom well used. Sadly, though, for every epic software poem like Stepanov’s Standard Template Library, there are 10,000 drab tomes of uninspired code.
The root purpose of this book is to give every reader permission to think a little harder about the beauty of well-tuned software. Take it and run with it. See farther!