I don’t know C so I can’t answer that. MVectors are not stack allocated because they are mutable, and mutable data structures only get stack allocated when Julia’s optimizer can see the entire lifetime of the data, so it can’t be allowed to escape the function body (modulo inlining).
The usual trick people use is to convert a SVector to MVector, mutate it, then convert back to SVector. If the compiler is in a good mood, this should be stack allocated.
Here’s an example:
using StaticArrays
using BenchmarkTools
function guncS(v)
SVector{2}(v[1], v[2] + 1)
end
function guncM(v)
mv = MVector(v)
mv[2] += 1 # Mutation on the stack!
SVector(mv)
end
let v = Ref(SVector(1, 2))
@btime guncS($v[])
@btime guncM($v[])
end;
#+RESULTS:
: 1.329 ns (0 allocations: 0 bytes)
: 1.329 ns (0 allocations: 0 bytes)
Note that I used Ref here to stop the function call from being hoisted out of the benchmark loop. Your funcS never actually ran because the compiler realized it was a compile time constnat. (Your CPU can’t do anything faster than a nanosecond)