Question about "type-stability" of Arrays in Julia 0.6

Hello! Today I’m reading the doc on performance tips and started to learning it. I’ve tested the @code_warntype macro with some my codes and got a little weird result.

The following function almost do nothing, just make an 1-element Float64 array.

function test()
    a = ones(Float64, 1)
end

For me, it looks it is obvious that type of a is Array{Float64,1}, however, the output of @code_warntype is:

Variables:
  #self#::#test
  a::Any

Body:
  begin
      SSAValue(0) = $(Expr(:invoke, MethodInstance for fill!(::Array{Float64,1}, ::Float64), :(Base.fill!), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Float64,1}, svec(Any, Int64), Array{Float64,1}, 0, 1, 0))), :((Base.sitofp)(Float64, 1)::Float64)))
      return SSAValue(0)
  end::Array{Float64,1}

It says type of a is not stable (?), thus Any. I used julia 0.6 but also tested on julia 0.5.1, then it shows type of a is Array{Float64,1} as expected. I would like to ask that there was any change between 0.5.1 and 0.6? Please tell me how to keep “type-stability” for arrays, or just say “don’t mind about that” if it is not important. Thanks.

This is the output of versioninfo().

Julia Version 0.6.0
Commit 903644385b* (2017-06-19 13:05 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)

The compiler realized it didn’t need to use a and can just return the temporary array it created for you, so then a is Any because it’s unused. That started happening in v0.6. There’s an issue open to make this printout more clear.

2 Likes

Thank you, I understand.

I started a PR that tries to deal with this problem: https://github.com/JuliaLang/julia/pull/23280

2 Likes