I got so curious that I figured I’d build all the solutions. After running some benchmarks over 1 million iterations (on a special case of the get_specific_volume function that returns a constant) I have the following results:
generated function runs at about 27 nanoseconds,
straight-up function call with dynamic dispatch runs at about 58 nanoseconds
recursive method runs at about 110 nanoseconds
for loop over constant svec runs at about 120 nanoseconds,
I’ve heard from other discussions that dynamic dispatch usually takes about 100 nanoseconds. The difference between the generated function (that explicitly writes the code) and the dynamic dispatch method seems to bear this out (but dynamic dispatch is a bit better than expected). Constant propagation doesn’t appear to happen in the for-loop case, it looks like dynamic dispatch is likely still being hit as there is only a performance DROP compared to raw dynamic dispatch. Results from the recursion method are about the same when compared against the for-loop method.