-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Retrieving elements from an inhomogeneous tuple is slow #19850
Comments
Thanks. Would you be willing to bisect this to see if it regressed in the past month or so? |
In Julia 0.5 there was the same behavior: julia> @benchmark tuple_elems_slow((0.,0,0))
BenchmarkTools.Trial:
memory estimate: 112.00 bytes
allocs estimate: 4
--------------
minimum time: 72.274 ns (0.00% GC)
median time: 75.505 ns (0.00% GC)
mean time: 89.214 ns (13.57% GC)
maximum time: 2.915 μs (96.81% GC)
--------------
samples: 10000
evals/sample: 974
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark tuple_elems_slow((0,0,0))
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 3.271 ns (0.00% GC)
median time: 3.375 ns (0.00% GC)
mean time: 3.389 ns (0.00% GC)
maximum time: 15.121 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00% Hope this helps. |
Ok --- I can understand why this might be slow, but I'm definitely concerned if it regressed recently. |
Why "might" this be slow? If it is type-stable I don't see why it would be allocating.
|
@JeffBezanson Because @giordano says it exists also in v0.5, it might be the case that my memory is wrong and the problem has existed ever since. I'll test the Julia version for which I remember this problem didn't exist and report back. |
@JeffBezanson My memory turns out to be wrong. The problem must have existed for quite a while. I used to use Julia v0.5, but picked up the v0.6 master branch when the "Vectorization Roadmap" (#16285) was completed, which corresponds to the commit 99b6a8c. That's also when I wrote some functions like the above in my project. I don't remember seeing any significant performance difference between homogeneous and inhomogeneous tuples back then, so I had an impression that the commit didn't have the problem. Maybe I was passing only homogeneous tuples to the functions. Anyway, I just built Julia fresh with the commit 99b6a8c and ran the same tests, but the problem didn't go away. Together with @giordano's report of the same problem on v0.5, I think it is safe to assume that the problem wasn't introduced recently, but has been there for quite a while. Sorry about the false information, and hope this helps solving the problem! |
The thing that happens is that julia actually pulls a copy for the tuple access. I don't understand why codegen decides to do this, but this makes retrieving the element O(N) instead of O(1).
|
It looks like it's fixed in v1.0? using BenchmarkTools
function tuple_elems_slow(t::NTuple{3,Real})
i1 = 1; i2 = 2; i3 = 3
t1, t2, t3 = t[i1], t[i2], t[i3]
end
@btime tuple_elems_slow((0,0,0)) # 1.170 ns (0 allocations: 0 bytes)
@btime tuple_elems_slow((0.0,0,0)) # 1.463 ns (0 allocations: 0 bytes) Seems like constant propogation of literals #24011 may have done it (though that is slightly different)? |
Yep, looks fixed. |
Consider the following function:
The performance of this function varies a lot depending on whether
t
is homogeneous (e.g.,t = (0,0,0)
with allInt64
elements) or inhomogeneous (e.g.,t = (0.,0,0)
with the first element beingFloat64
). Whent
is inhomogeneous, the function uses extra allocations:This is not caused by type instability, because
@code_warntype
shows that the function is type-stable for both executions.In comparison, if integer linerals are used to index tuple elements, the function does not experience such performance degradation even if the tuple is inhomogeneous:
Here is the
versioninfo()
result:I could be wrong, but I think this problem was introduced recently. I don't think I had this problem on the
master
branch at least about a month ago.The text was updated successfully, but these errors were encountered: