Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_array slow for large static arrays #653

Open
JakobAsslaender opened this issue Sep 25, 2024 · 1 comment
Open

create_array slow for large static arrays #653

JakobAsslaender opened this issue Sep 25, 2024 · 1 comment

Comments

@JakobAsslaender
Copy link

Hi,
I would like to auto-generate functions that create large (roughly 10x10) static matrices. However, the call of SymbolicUtils.Code.create_array seems slow as shown in the MWE below. Some brief tests suggest that the problem might be the elems... in

SArray{Tuple{dims...}}(elems...)

Many thanks for looking into this!!!

MWE:

using StaticArrays
using Symbolics
using BenchmarkTools 

function symbolics_output(a, b, c, d)
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:373 =#
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:374 =#
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:375 =#
    begin
        #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:468 =#
        (SymbolicUtils.Code.create_array)(SArray, nothing, Val{2}(), Val{(8, 8)}(), a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d)
    end
end

function symbolics_optimized(a, b, c, d)
    SMatrix{8,8,Float64,64}(a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d)
end

@benchmark symbolics_output(1.0, 2.0, 3.0, 4.0)
@benchmark symbolics_optimized(1.0, 2.0, 3.0, 4.0)

Output:

BenchmarkTools.Trial: 10000 samples with 8 evaluations.
 Range (min  max):  3.114 μs   12.461 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     3.191 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   3.221 μs ± 319.398 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

     ▄█▆▁                                                      
  ▂▃▅████▅▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▁▂▂▁▁▂▁▂▁▁▁▂▁▁▁▂▂▂▂▂ ▃
  3.11 μs         Histogram: frequency by time        4.04 μs <

 Memory estimate: 3.12 KiB, allocs estimate: 68.

vs.

BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min  max):  5.775 ns  61.289 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     5.796 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.861 ns ±  0.928 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▄█▃                                                         
  ███▅▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▁▁▂▁▁▁▁▁▁▂▂▂▁▁▁▂▁▂▁▁▂▂▂▂▂▂▁▁▁▁▁▂▁▁▂▂▂▂▂ ▂
  5.78 ns        Histogram: frequency by time        6.47 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
@ernestds
Copy link

ernestds commented Nov 7, 2024

Does simply removing the ... solve the problem? Or does it create other problems somewhere? (Julia noob here)

using StaticArrays
using Symbolics
using BenchmarkTools 

@inline function create_array2(::Type{<:SArray}, ::Nothing, nd::Val, ::Val{dims}, elems...) where dims
    SArray{Tuple{dims...}}(elems)
end
function symbolics_output(a, b, c, d)
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:373 =#
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:374 =#
    #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:375 =#
    begin
        #= /Users/asslaj01/.julia/packages/SymbolicUtils/8d6hE/src/code.jl:468 =#
        (create_array2)(SArray, nothing, Val{2}(), Val{(8, 8)}(), a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d)
    end
end

@benchmark symbolics_output(1.0, 2.0, 3.0, 4.0)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  3.917 ns … 17.943 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     3.967 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   4.010 ns ±  0.371 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

   █                                                          
  ▅█▄▂▂▂▂▂▁▂▁▂▂▁▂▂▂▁▁▂▂▂▂▂▂▂▁▂▂▁▁▂▁▂▁▂▂▂▂▂▁▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▂
  3.92 ns        Histogram: frequency by time        5.78 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants