making it fast
my process for approaching dsp optimization
2026-10-11
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:
- working with a pair partner while you write code
- working in small, iterative cycles, against a flexible plan
- writing the code test first (TDD)
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:
- Object-Oriented Programming: traditionally OOP tends to use a lot of unnessesary virtual inheritance. This stops the compiler from inlining code, and optimizing code automatically. I still organize my code into testable modules but I separate algorithms from data structures and use other means of creating polymorphism when I need it. (and I don't always need it.)
- Copying data: its really easy to accidentally copy data when writing c++ code. Working with pointers and references and thinking carefully about where your memory is and its lifetime helps me avoid copying things where I don't need to.
- Allocating data on the heap: General purpose memory allocation is slow and unpredictable. I therefore choose to, whenever possible, allocate my memory up front. There are several good techniques for this with arena allocation being perhaps the best entry point. As a bonus your code gets a lot simpler than if you had to utilize RAII or other idioms that are designed to prevent you from leaking memory or accessing already freed memory. You know where stuff is stored because you allocated it all at once. You know when it's freed because you free it all at once.
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 |
|
| The compiler isn't able to leverage SIMD automatically |
|
| The cache isn't being utilized well |
|
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.