Scott Wolchok

Recent posts

Apr 3, 2021
The Sad Truth About C++ Copy ElisionCopy elision is a C++ compiler optimization that, as its name suggests, eliminates extra copy and move operations. It is similar to the classical copy propagation optimization, but specifically performed on C++ objects that may have non-trivial copy and move constructors. In this post, I’ll walk through an example where an obvious optimization you might expect from your compiler doesn’t actually happen in practice. Introducing an extra variable to break up a long line Let’s say that you have a long function call that returns an object, and you want to immediately pass that object to another function, like this:…
Mar 14, 2021
How to Read ARM64 Assembly LanguageARM64 is a computer architecture that competes with the popular Intel x86-64 architecture used for the CPUs in desktops, laptops, and so on. ARM64 is common in mobile phones1, as well as Graviton-based Amazon EC2 instances, the Raspberry Pi 3 and 4, and the much ballyhooed Apple M1 chips, so knowing about it might be useful! In fact, I have almost certainly spent more time with ARM64 than x86-64 because of the iPhone.…
Mar 7, 2021
Parameter Passing in C and C++Now that we know how to read assembly language, we can talk about how parameter passing works at the machine level and its consequences for writing faster code. We will focus on x86_64, but ARM64 works in a roughly similar way. What does “parameter passing” mean? Let’s say you’ve written a large program, and in it you have two files: // Something.h int doSomething(int x, int y); // OtherStuff.cpp void doOtherStuff() { // .…
Feb 26, 2021
How to Read Assembly LanguageUPDATE: This article now has an ARM64 port. Why, in 2021, does anyone need to learn about assembly language? First, reading assembly language is the way to know exactly what your program is doing. Why, exactly, is that C++ program 1 MiB (say) instead of 100 KiB? Is it possible to squeeze some more performance out of that function that gets called all the time? For C++ in particular, it is easy to forget or just not notice some operation (e.…
Jan 23, 2021
Inlining and Compiler OptimizationsWhy is inlining so important in C++? Clearly, it reduces function call overhead: if a function is inlined, there is no need to spend time setting up its arguments, jumping to it, creating a stack frame, and then undoing all that upon returning. More interestingly, though, inlining enables other compiler optimizations. In this article, I will show examples of constant propagation and loop-invariant code motion (LICM). Then, I will explain how inlining enables these optimizations to apply more widely and show an example of the consequences when that doesn’t happen.…
Jan 14, 2021
C++ Performance Trap #2: Unnecessary std::functionstd::function does what it says on the tin: it holds any callable object. For example, you can use lambdas with or without captures. It has a couple key drawbacks: It is fairly large: 32 bytes on a 64-bit system. It causes typeinfo for each used lambda to be generated. Function pointers, on the other hand, are much more limited: they can only point to free-standing functions (including lambdas with no captures!…
Jan 14, 2021
C++ Performance Trap #1: Constant-size std::vectorC++ Performance Traps Series Introduction The C++ standard library has many classes and functions that are easy to use and relatively safe. However, in situations where performance and efficiency really matter, straightforward use of the standard library may not always be the best choice. This post is the first in a series that will catalogue some of those opportunities for improvement. It is meant to be accessible to people who are not C++ gurus or language lawyers.…
Jan 14, 2021
How This Site Is MadeTLDR: Hugo, GitHub, Netlify. Putting writing on a website in 2021 is surprisingly pleasant, despite a few glitches. I wanted to have a plain old static site. I can write raw HTML well enough, and I knew about Bootstrap, but that still seemed like too much work. Instead, I looked at a couple overviews of static site generators, and I ended up picking Hugo as the one most fitting my sensibilities.…