making it fast

make it work, make it right, make it FAST

My first professional software engineering experience was in the context of something called extreme programming. What this actually meant was the following:

I was really lucky to get this experience because I got to learn first hand from really talented people and was encouraged early to focus on how I teach others just as much as on growing my own skills. We had a big poster on the wall of the Kent Beck mantra "Make it work. Make it right. Make it fast." which embodies the ethos of this development style.

The intent of this mantra was to keep our process focused on what the people using the software needed and not get lost in the weeds. First you make the software do the thing that the user needs it to do. Then you make it reliable—fixing any bugs that might make the software misbehave and addressing any design issues that might prevent you from maintaining the code over time. Only then, do you consider how to make the code performant—using evidence from benchmarks and profiler output to measure whether your code is fast enough. This advice was usually came paired with the often misquoted Knuthism "premature optimization is the root of all evil".

In practice this meant not spending very much time actually making anything fast. The industry was (and is) much more interested in how to increase the speed at which features were delivered to the business than how to make the code perform well for the user.

As I learned more about how the manufacture and use of computers are implicated in our compound ecological crises I gradually became aware of how this approach to software causes harm. As we continually prize the speed at which software developers work over everything else, we cause the minimum operating requirements of software to rise over time with no tangible benefit to the end user for all the extra computation happening on their devices.

For a period of time this gradual accumulation of software waste was softened by each generation of CPUs getting faster. The faster CPUs got, the more we could waste on abstraction designed to make the engineers go faster.

This is called Wirth's Law: "Intel giveth, Microsoft taketh away".

In the future I want to write more about how to step off this nasty software waste treadmill and make it a more explicit part of the tools I make as fell house audio. In the meantime, If anyone is interested in learning more about this particular articulation Jevons Paradox and its relationship to the climate crisis, I would recommend checking out permacomputing.

In my audio work I still broadly follow the path of making it work, making it right, and making it fast. However, I now treat the last step as not only mandatory, but something to be done continually during the development process.

What follows is about how to make things fast.

making it fast

step 1: don't make it slow

The famous game programmer Casey Muratori coined the phrase pessimization to describe the way we make software slow by default when we follow industry trends like Object-Oriented Design. The first step to making software fast is to not make it slow in the first place. In practice, this means having a general understanding of what is hard for your computer to do and refraining from asking it to do that stuff unless you really need it to.

I've found that avoiding the following things helps prevent me from accidentally pessimizing my code:

The other thing I like to keep in mind is that math is fast for the computer and manipulating memory is slow. (see Data Oriented Design for more on this). I don't worry about memoizing things. I do the math that I need to do, where I need to do it.

step 2: measure the performance and look for bottlenecks

Once I have something that sounds the way I want (i.e. I've made it work) and I have it organized in some well-tested modules that don't blow up or cause unwanted noise (i.e. I've made it right), I will write a benchmark around each module and around the whole DSP path. The benchmark(s) can tell me how many μs the program spends in each module and how many μs it spends processing each block of samples overall. From this I can calculate how long (on average) the CPU is occupied while producing a single sample of audio.

In order to optimize, we need to have a definition of what fast enough is. For real-time audio this is pretty easy to come up with. At a sample-rate of 48khz your computer has a around 0.0208ms per-sample to finish all of its work and give the audio data to the sound card. In practice, you actually have a lot less time than this because you are sharing the CPU time with a bunch of other plug-ins and audio processing that is being done by the DAW. In order to share the audio processing well I give myself a budget of 1% of this total available CPU time. This means we have around 7μs per-block of 32 samples to get our work done.

In addition to the benchmark tests I use a profiler to measure which parts of the code are taking the most cycles to complete and where the most cache-misses happen. With all this data in hand I can start to improve the performance of the code, targeting the worst performing areas first.

step 3: looking for opportunities in the worst performing sections

Armed with data about what is taking CPU time and what is causing cache-misses, I can start formulating hypotheses about what might make it take less time. I use the benchmarks to test my hypotheses and my unit tests and my ears to make sure I haven't broken anything. Here are some common issues I find and experiments I run to resolve them:
issue experiments
I'm doing too much shit
  • what is the minimal amount of oversampling I can get away with?
  • do I need to re-compute filter coeffs every sample? every block? only when they change?
  • is there a cheaper algorithm that gets me the same sound? (ex. polynomial approximation for expensive math functions)
The compiler isn't able to leverage SIMD automatically
  • look for ways I can process a chunk of samples before moving on to the next part of the algorithm. (this one is so important I do it as part of step 1 now.)
  • swap the order in which iteration is occurring. For example loop first by channel instead of sample or vice-versa.
  • look for dependencies on prior loop values and try to eliminate them.
  • manually vectorize the code using something like xsimd. (this is often most possible when processing multiple (4-8) channels with the same algorithm)
The cache isn't being utilized well
  • look for ways make my data structures smaller
  • consider the cache alignment of my data structures
  • if there is branching (i.e. if/else blocks), try to get rid of those for better branch prediction
If I run an experiment and it doesn't make the code faster, or it breaks the processing, I revert and try something else. After I get a win I commit and start back at step 2. I'll repeat this cycle until I get to my 1% CPU time target. If that's not possible I might reconsider my whole design and try to use an algorithm where I can get there.

your dev machine should be a little slow

I don't keep the newest computers around to test on. I currently like to develop on equipment that is at least 4-5 years old. I've made a commitment to keeping these machines running as best I can to avoid worsening the environmental damage that was incurred when they were made. As time goes on I will target older and older machines. Ideally, I would like to target machines that are 15+ years old. If I can make it work for the older machines, then I will have avoided contributing to the software waste treadmill.

Beyond fulfilling an ethical commitment, using older tech for development means you write better code. Your benchmark targets will be measured on a slower machine and you will end up seeing the performance problems more quickly.

PS.

I should perhaps clarify—given the current climate—that all the em-dashes you read on my posts are typed with my own fingers and not the output of a chat bot. It's one of my favorite pieces of punctuation as it helps me write how I talk. You can type it by entering control+shift+U followed by 2014. I insist on using the em-dash—you can take it from my cold dead fingers.