In the following example, it seems like it should be simple for Julia to infer that the type of prev_x
is Float64
. Why is it instead getting assigned a boxed type?
julia> function tf(xs)
prev_x = xs[1]
map(xs) do x
r = x / prev_x
prev_x = x
r
end
end
tf (generic function with 1 method)
julia> xs = rand(10^6);
julia> @time tf(xs);
0.178356 seconds (3.12 M allocations: 61.497 MiB, 67.31% compilation time)
julia> @time tf(xs);
0.060390 seconds (3.00 M allocations: 53.406 MiB)
julia> @code_warntype tf(xs)
MethodInstance for tf(::Vector{Float64})
from tf(xs) @ Main REPL[243]:1
Arguments
#self#::Core.Const(tf)
xs::Vector{Float64}
Locals
#228::var"#228#229"
prev_x::Core.Box
Body::Any
1 ─ (prev_x = Core.Box())
│ %2 = Base.getindex(xs, 1)::Float64
│ Core.setfield!(prev_x, :contents, %2)
│ (#228 = %new(Main.:(var"#228#229"), prev_x))
│ %5 = #228::var"#228#229"
│ %6 = Main.map(%5, xs)::Any
└── return %6