Interpreting a body expression from @code_warntype

If we run:

@code_warntype deepcopy(rand(2))

as edited by Julia REPL, the output contains the marked values ​​in the Body expression. In particular, two Anyat the end:

Body:
  begin  # deepcopy.jl, line 8:
      GenSym(0) = (Base.Array)(Base.Any,32)::Array{Any,1}
      return (Base.deepcopy_internal)(x::Array{Float64,1},$(Expr(:new, :((top(getfield))(Base,:ObjectIdDict)::Type{ObjectIdDict}), GenSym(0))))::Any
  end::Any

I understand from this question that we usually do not need to worry about marked values ​​in a body expression if our main problem is type instability. So instead my question is this:

Why does a simple function from Basegenerate any marked values ​​in @code_warntype? I'm sure there are good reasons, but I'm new to interpreting the conclusion from @code_warntypeand have had some problems understanding the discussion of the Body expression from white papers.

+4
source share
1 answer

, . ( ::Any !) , , - , , , .

, :

julia> function f()
         y = rand(10)
         @time y[1] + y[10]
         z = deepcopy(y)
         @time z[1] + z[10]
       end
f (generic function with 1 method)

julia> f();  # ignore output here on first compile

julia> f();
  0.000000 seconds
  0.000002 seconds (3 allocations: 48 bytes)

, , .

, 0.5 (, , ), . ,

julia> @code_warntype deepcopy(rand(2))
Variables:
  #self#::Base.#deepcopy
  x::Array{Float64,1}

Body:
  begin  # deepcopy.jl, line 8:
      # meta: location dict.jl Type # dict.jl, line 338:
      SSAValue(1) = (Core.ccall)(:jl_alloc_array_1d,(Core.apply_type)(Core.Array,Any,1)::Type{Array{Any,1}},(Core.svec)(Core.Any,Core.Int)::SimpleVector,Array{Any,1},0,32,0)::Array{Any,1}
      # meta: pop location
      return (Core.typeassert)((Base.deepcopy_internal)(x::Array{Float64,1},$(Expr(:new, :(Base.ObjectIdDict), SSAValue(1))))::Any,Array{Float64,1})::Array{Float64,1}
  end::Array{Float64,1}

,

julia> f()
  0.000000 seconds
  0.000000 seconds

.

+5

All Articles