SparseMatrix or SArray, MArray?

For convenience, yes, but this is what I mean:

using StaticArrays, BenchmarkTools, Setfield

function mut!(A)
  A .= A .+ 1
end
Base.setindex!(m::SMatrix,value,i,j) = @set! m[i,j] = value

A1 = rand(3,3)

A2 = rand(MMatrix{3,3,Float64})

A3 = rand(SMatrix{3,3,Float64})

@btime mut!($A1)

@btime mut!($A2)

@btime mut!($A3)



Results:

  14.548 ns (0 allocations: 0 bytes)
  4.733 ns (0 allocations: 0 bytes)
  1.791 ns (0 allocations: 0 bytes)

This actually makes me wonder how and why MMatrix are actually implemented as they are, and if that could not be improved.

(it has not escaped to me that the function above does not mutate the matrix if it is a static matrix, and one may have to appeal to MMatrix if passing it to another code over which one does not have access to define A = mut!(A)) .