StaticArrays compilation performance bug

I also posted this as a StaticArrays issue: with Julia 1.7 and StaticArrays v0.12.5:

julia> N = 14; m = SMatrix{N,N}(rand(N,N));

julia> x->x*m # works fine
#1 (generic function with 1 method)

julia> @time [x->x*g for g∈[m]]
  3.018892 seconds (57.49 k allocations: 3.383 MiB, 100.06% compilation time)
1-element Vector{var"#20#22"{SMatrix{14, 14, Float64, 196}}}:
 #20 (generic function with 1 method)

Why does it take 3 seconds? It seems to scale horribly, too: for N=10, I get 0.449260 seconds; for N=15, 5.230108 seconds; for N=16, 9.209356 seconds.

This is expected for large SArrays. From the README:

Note that in the current implementation, working with large StaticArray s puts a lot of stress on the compiler, and becomes slower than Base.Array as the size increases. A very rough rule of thumb is that you should consider using a normal Array for arrays larger than 100 elements.

7 Likes

Check out GitHub - chriselrod/StrideArrays.jl: Library supporting the ArrayInterface.jl strided array interface. for an in-development package that would allow much larger static-like arrays AFAICT

1 Like

https://chriselrod.github.io/StrideArrays.jl/dev/stack_allocation/

julia> @benchmark sum(exp.(@StrideArray randn(8,10))) # StrideArrays
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     127.557 ns (0.00% GC)
  median time:      127.986 ns (0.00% GC)
  mean time:        128.116 ns (0.00% GC)
  maximum time:     165.890 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     888

julia> @benchmark sum(exp.(@MMatrix randn(8,10))) # StaticArrays
BenchmarkTools.Trial:
  memory estimate:  672 bytes
  allocs estimate:  1
  --------------
  minimum time:     703.599 ns (0.00% GC)
  median time:      862.130 ns (0.00% GC)
  mean time:        887.160 ns (4.56% GC)
  maximum time:     136.675 μs (99.29% GC)
  --------------
  samples:          10000
  evals/sample:     142
1 Like

Related issue: Uncomfortable compile times for long static vectors · Issue #35547 · JuliaLang/julia · GitHub