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

Unexpected allocation when splatting #784

Closed
garrison opened this issue May 7, 2020 · 2 comments
Closed

Unexpected allocation when splatting #784

garrison opened this issue May 7, 2020 · 2 comments
Labels
fix-in-base Fix needs some work in Base

Comments

@garrison
Copy link
Contributor

garrison commented May 7, 2020

I was surprised that f() allocated. I was even more surprised when I found out that g() does not:

julia> f() = CartesianIndex((@SVector[1,2,3])...);

julia> g() = CartesianIndex(Tuple(@SVector[1,2,3])...);

julia> @btime f()
  705.205 ns (8 allocations: 208 bytes)
CartesianIndex(1, 2, 3)

julia> @btime g()
  0.026 ns (0 allocations: 0 bytes)
CartesianIndex(1, 2, 3)
@andyferris
Copy link
Member

Yes this is called the "splatting penalty".

Julia "lowers" function calls with ... in it to an internal function called apply. As an internal function it knows how to deal with tuples but otherwise falls back to the iteration protocol (constructing an intermediate boxed tuple).

This can be a bit surpising since it (a) works correctly, (b) ... in slurping and destructuring there is no such efficiency problem, and (c) Julia does a pretty good job of avoiding performance gotchas in general. Fixing this would be the realm of the compiler developers, unfortunately. As you noted, the second one works fine; @c42f even suggested to have "lowering" wrap splats into an overloadable generic function that constructs a tuple from an iterable so you essentially always get the second case, which is an idea I have been warming to.

@c42f
Copy link
Member

c42f commented May 11, 2020

Right on Andy. See JuliaLang/julia#29114 for an extended discussion.

Summary: type inference needs to model the iteration protocol because splatting is defined in terms of iteration. (There's other implementation options, but they all seem to have some undesirable properties.)

@c42f c42f closed this as completed May 11, 2020
@c42f c42f added the fix-in-base Fix needs some work in Base label May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix-in-base Fix needs some work in Base
Projects
None yet
Development

No branches or pull requests

3 participants