const A = @SMatrix [1.0 2.0;
3.0 4.0]
f(r, x::Number) = x * Vector{Float64}(A[r,:])
f is a bottleneck function. This is already really quick and I’m happy with performance but wondering if I can do better. The multiplier x is pretty much a constant, defined on initialisation but also updated occasionally based on user interaction.
Is there a way to gain some more speed by pre-multiplying A by x and indexing into that array? I haven’t been able to work out a way to do it. The pre-calculation would be done from a different file in the same module, which seems to complicate things.
The issue is that you are creating a vector which is heap allocated. You’re getting two allocations, the second because of the resulting *. It’s faster to just use the resulting SVector from indexing the SMatrix.
It’s doubtful that you’d get any speed improvement. A multiply is basically free with a copy, which you’re probably gonna have to do anyway. Depends a bit on the surrounding code though. As @ChrisRackauckas said though, your biggest bottleneck in the above code will be the Vector allocation.
Why? No codes should rely on the underlying data structure, just the actions. If you just allow AbstractVector most codes should work just fine, unless you’re modifying this vector in the future, in which case you can just use setindex.