TensorCast.jl icon indicating copy to clipboard operation
TensorCast.jl copied to clipboard

Broadcasting a splat

Open mcabbott opened this issue 4 years ago • 0 comments

On master the following syntax is understood:

julia> using TensorCast, ImageCore

julia> mat = rand(1:10, 4, 3) ./ 10;

julia> @cast _[k] := RGB(mat[k,:]...)
4-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.8,0.7,0.7)
 RGB{Float64}(0.5,0.7,0.5)
 RGB{Float64}(0.9,0.7,0.2)
 RGB{Float64}(0.7,0.2,1.0)

But this is done quite inefficiently, by literally performing the splat for every slice. It would be better to re-write it to slice the other way, splat once, and then broadcast:

julia> @btime Base.splat(RGB).(eachrow(m))  setup=(m=rand(100,3));
 22.541 μs (1122 allocations: 38.89 KiB)

julia> @btime RGB.(eachcol(m)...)  setup=(m=rand(100,3));
 421.905 ns (10 allocations: 3.12 KiB)

Can this be done in general?

mcabbott avatar Apr 04 '21 04:04 mcabbott