Everyone thanks their spouse for helping make a book possible. Its trite, I know. My wife Renee Ostler made this book possible by giving me permission to take months off work, by giving me time and space to focus on writing, and by staying up late asking me questions about optimizing C++ code, even though she wasnt particularly engaged by the topic, just to show her support. She made this project important to her because it was important to me. No author could ask for more.
Preface
Hi. My name is Kurt, and Im a code-aholic.
I have been writing software for over 35 years. Ive 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 Ritchies The C Programming Language; smart, ahead-of-the-curve books, like Scott Meyerss EffectiveC++ series; challenging, mind-expanding books, like Andrei Alexandrescus Modern C++ Design; careful, precise books, like Bjarne Stroustrup and Margaret Elliss 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 Mayhews 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 Ive 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 antelope_book@guntheroth.com .
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 Stepanovs 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!
Apology for the Code in This Book
Although I have been writing and optimizing C++ code for over 20 years, most of the code appearing in this book was developed specifically for this book. Like all new code, it surely contains defects. I offer my apologies.
I have developed for Windows, Linux, and various embedded systems over the years. The code presented in this book was developed on Windows. The code and the book no doubt show a Windows bias. The lessons of how to optimize C++ code that are illustrated using Visual Studio on Windows apply equally to Linux, Mac OS X, or any other C++ environment. However, the precise timings of different optimizations depend on the compiler and standard library implementation, and the processor on which the code is tested. Optimization is an experimental science. Taking optimization advice on faith is fraught with negative surprises.