JuliaNotes.jl icon indicating copy to clipboard operation
JuliaNotes.jl copied to clipboard

Concerning example of type instability in section of closures

Open ngiann opened this issue 4 months ago • 0 comments

First of all, thanks for these thoughtful notes.


It seems that the example in https://m3g.github.io/JuliaNotes.jl/stable/anonymous/#Example-of-type-instability does not exhibit type instabilities under Julia 1.11.6.

I execute the following immediately after starting up julia:

function u(f,x)
                u = 0.
                for i in 1:length(x)-1
                   for j in i+1:length(x)
                     u += f(x[i],x[j])
                   end
                end
                u
end

f(x,y,a,b) = a*x - b*y

a = 5; b = 7;

x=5.

@code_warntype u( (x,y) -> f(x,y,a,b), x )

The output of the last line reads:


MethodInstance for u(::var"#3#4", ::Float64)
  from u(f, x) @ Main REPL[1]:1
Arguments
  #self#::Core.Const(Main.u)
  f::Core.Const(var"#3#4"())
  x::Float64
Locals
  @_4::Nothing
  u::Float64
  @_6::Union{}
  i::Union{}
  j::Union{}
Body::Float64
1 ─       (u = 0.0)
│   %2  = Main.:(:)::Core.Const(Colon())
│   %3  = Main.:-::Core.Const(-)
│   %4  = Main.length::Core.Const(length)
│   %5  = (%4)(x)::Core.Const(1)
│   %6  = (%3)(%5, 1)::Core.Const(0)
│   %7  = (%2)(1, %6)::Core.Const(1:0)
│         (@_4 = Base.iterate(%7))
│   %9  = @_4::Core.Const(nothing)
│   %10 = (%9 === nothing)::Core.Const(true)
│   %11 = Base.not_int(%10)::Core.Const(false)
└──       goto #3 if not %11
2 ─       Core.Const(:(@_4))
│         Core.Const(:(i = Core.getfield(%13, 1)))
│         Core.Const(:(Core.getfield(%13, 2)))
│         Core.Const(:(Main.:(:)))
│         Core.Const(:(Main.:+))
│         Core.Const(:(i))
│         Core.Const(:((%17)(%18, 1)))
│         Core.Const(:(Main.length))
│         Core.Const(:((%20)(x)))
│         Core.Const(:((%16)(%19, %21)))
│         Core.Const(:(@_6 = Base.iterate(%22)))
│         Core.Const(:(@_6))
│         Core.Const(:(%24 === nothing))
│         Core.Const(:(Base.not_int(%25)))
│         Core.Const(:(goto %45 if not %26))
│         Core.Const(:(@_6))
│         Core.Const(:(j = Core.getfield(%28, 1)))
│         Core.Const(:(Core.getfield(%28, 2)))
│         Core.Const(:(Main.:+))
│         Core.Const(:(u))
│         Core.Const(:(i))
│         Core.Const(:(Base.getindex(x, %33)))
│         Core.Const(:(j))
│         Core.Const(:(Base.getindex(x, %35)))
│         Core.Const(:((f)(%34, %36)))
│         Core.Const(:(u = (%31)(%32, %37)))
│         Core.Const(:(@_6 = Base.iterate(%22, %30)))
│         Core.Const(:(@_6))
│         Core.Const(:(%40 === nothing))
│         Core.Const(:(Base.not_int(%41)))
│         Core.Const(:(goto %45 if not %42))
│         Core.Const(:(goto %28))
│         Core.Const(:(@_4 = Base.iterate(%7, %15)))
│         Core.Const(:(@_4))
│         Core.Const(:(%46 === nothing))
│         Core.Const(:(Base.not_int(%47)))
│         Core.Const(:(goto %51 if not %48))
└──       Core.Const(:(goto %13))
3 ┄ %51 = u::Core.Const(0.0)
└──       return %51

ngiann avatar Oct 24 '25 06:10 ngiann