You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice if constant propagation worked through broadcasting.
using BenchmarkTools
get2(x) =getindex(x,2)
foo1(x) =sum(get2.(x))
foo2(x) =sum(getindex.(x,2))
x =fill((1,2.0,3//1),1000) # eltype(eltype(x)) is not concrete@code_warntypefoo1(x) # succeeds inference@code_warntypefoo2(x) # fails inference@btimefoo1($x); # 762.069 ns (1 allocation: 7.94 KiB)@btimefoo2($x); # 26.800 μs (2000 allocations: 70.39 KiB)
This fails because getindex(::Tuple{A,B,C},i::Int) where {A,B,C} is unstable unless A==B==C or i gets constant-folded.
One can work around this via the get2 solution above or a similar broadcast(...) do block (and obviously sum(get2,x) is the best solution in this MWE), but having broadcast natively recognize and propagate scalar constants would be convenient and prevent people from unknowingly falling into this trap. The primary cases where I've gotten into trouble with this are broadcasts over getindex and getproperty.
The text was updated successfully, but these errors were encountered:
The problem is that during broadcast, we call copy, which checks the return type of bc::Broadcasted first, if it's concrete then falls-back to copyto!.
IIUC, the current combine_eltypes is pure-type based, and of-cource it fails to return Float64.
It would be nice if constant propagation worked through broadcasting.
This fails because
getindex(::Tuple{A,B,C},i::Int) where {A,B,C}
is unstable unlessA==B==C
ori
gets constant-folded.One can work around this via the
get2
solution above or a similarbroadcast(...) do
block (and obviouslysum(get2,x)
is the best solution in this MWE), but having broadcast natively recognize and propagate scalar constants would be convenient and prevent people from unknowingly falling into this trap. The primary cases where I've gotten into trouble with this are broadcasts overgetindex
andgetproperty
.The text was updated successfully, but these errors were encountered: