JuliaData / SplitApplyCombine.jl

Split-apply-combine strategies for Julia
Other
149 stars 15 forks source link

splitdims not type-stable #26

Closed cossio closed 4 years ago

cossio commented 4 years ago
julia> @code_warntype splitdims([1 2 3; 4 5 6], 1)
Variables
  #self#::Core.Compiler.Const(SplitApplyCombine.splitdims, false)
  a::Array{Int64,2}
  i::Int64
Body::Array{T,N} where T where N
1 ─      nothing
│   %2 = Core.tuple(i)::Tuple{Int64}
│   %3 = SplitApplyCombine.Val(%2)::Val{_A} where _A
│   %4 = SplitApplyCombine._splitdims(a, %3)::Array{T,N} where T where N
└──      return %4
andyferris commented 4 years ago

Thanks @cossio. You had me concerned for a moment, but actually this a consequence of working at the REPL. To be type-stable that 1 argument needs to be constant propagated, which it will from code written in functions.

Check this out:

julia> f(a) = splitdims(a, 1)
f (generic function with 1 method)

julia> @code_warntype f([1 2 3; 4 5 6])
Variables
  #self#::Core.Compiler.Const(f, false)
  a::Array{Int64,2}

Body::Array{Array{Int64,1},1}
1 ─ %1 = Main.splitdims(a, 1)::Array{Array{Int64,1},1}
└──      return %1
cossio commented 4 years ago

I see, thanks for taking a look!

andyferris commented 4 years ago

No worries!