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

Creating SArray with more that 14 entries uses allocation #384

Closed
wsshin opened this issue Mar 21, 2018 · 4 comments
Closed

Creating SArray with more that 14 entries uses allocation #384

wsshin opened this issue Mar 21, 2018 · 4 comments

Comments

@wsshin
Copy link
Contributor

wsshin commented Mar 21, 2018

julia> VERSION
v"0.6.3-pre.0"

julia> using BenchmarkTools

julia> @btime SVector{14}(0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  2.181 ns (0 allocations: 0 bytes)

julia> @btime SVector{15}(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  12.230 ns (1 allocation: 128 bytes)

julia> @btime SMatrix{14,1}(0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  2.186 ns (0 allocations: 0 bytes)

julia> @btime SMatrix{15,1}(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  11.333 ns (1 allocation: 128 bytes)

julia> @btime SArray{Tuple{14}}(0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  2.186 ns (0 allocations: 0 bytes)

julia> @btime SArray{Tuple{15}}(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  12.952 ns (1 allocation: 128 bytes)

Is this an expected behavior?

@tkoolen
Copy link
Contributor

tkoolen commented Mar 21, 2018

This is the splatting penalty for

@inline (::Type{SA})(x...) where {SA <: StaticArray} = SA(x)

See e.g. JuliaLang/julia#22545, JuliaLang/julia#22370. Could work around the penalty in StaticArrays with an @generated constructor, but I'm not sure that that's a good idea. What's your use case?

@wsshin
Copy link
Contributor Author

wsshin commented Mar 21, 2018

I'm just creating a large matrix with >14 entries.

@martinholters
Copy link
Collaborator

If possible, just pass a tuple:

julia> @btime SVector{15}(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  13.507 ns (1 allocation: 128 bytes)

julia> @btime SVector{15}((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));
  3.701 ns (0 allocations: 0 bytes)

@andyferris
Copy link
Member

Correct. In code, I recommend using tuples to construct static arrays. There's also a Tuple(::StaticArray) constructor to go the other way.

The non-tuple constructors are only mostly for convenience. They are nice for creating 3-vectors or playing at the REPL but actually don't make any sense w.r.t. all the AbstractArray constructor interface discussions that happened during Julia v0.7 development. There are also the macros which also use tuples to avoid the splatting penalty.

Sorry @wsshin - since there's not much I can do about the splatting penalty from this side, I'll close this issue.

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

4 participants