Type inference of array slices in 0.6


#1

I came across this strange behavior in 0.6 where the type of an array slice isn’t inferred when it is assigned in a function. Consider this MWE.

test(A::Array{Float64, 2}, i::Int64) = A[i,:]
test2(A::Array{Float64, 2}, i::Int64) = Ai = A[i,:]
A = reshape(1:16, 4, 4)

When I run @code_warntype test(A, 2) I get (with the Body section omitted)

Variables:
  #self#::#test
  A::Array{Float64,2}
  i::Int64

Body:
...
end::Array{Float64,1}

The types of the inputs and output are correctly inferred.

However, when I run @code_warntype test2(A, 2) I get

Variables:
  #self#::#test2
  A::Array{Float64,2}
  i::Int64
  Ai::Any

Body:
...
end::Array{Float64,1}

The types of the inputs and output are still correctly inferred, but the temporary variable Ai is type Any even though it is the same as the output. In 0.5 Ai is inferred correctly as Ai::Array{Float64,1}. Does anyone know what causes this behavior?


#2

I believe this is because Ai is eliminated by the type type inference runs on the code. Is there an actual performance issue here or were you just alarmed by the @code_warntype output?


#3

It doesn’t appear that there is a performance issue. I was just surprised when I ran @code_warntype on one of my functions and all the array slices (actually views in my case) were typed as Any as well as every array calculated from them. I guess since the output type is correct I shouldn’t worry about it.


#4

Yes, it would be nice to not list this alarming type in the future, but for now this is a bit of a known wart when one displays the results of type inference for a variable that ends up being eliminated entirely.


#5

Ok thanks for the clarification!