Should you pass data by value, or by reference?
When you’re a certain kind of perfectionist, the kind that prevents you from being productive, this is a question that might plague you every time you write a function.
I’ve heard a few opinions on this, floating around. I’ve heard “stack-to-stack copies are super cheap”, but.. how cheap are they?
Fine, I’ll write a benchmark. Can’t take too long.
3… 2… (several months pass) 1… 🪄
And that’s how I ended up writing a graphing library, and a benchmark.
If you want to run these benchmarks on your own machine, or dump the assembly, you can check out the benchmark code.
This part requires JS. Sorry. I promise all it's doing it creating a graph.
This graph shows the overhead of passing structs of different sizes by value, on different machines.
In general, passing any struct by reference incurs the same overhead as passing a pointer-sized struct by value.
... continue reading