julia icon indicating copy to clipboard operation
julia copied to clipboard

improve inference in `afoldl`

Open jishnub opened this issue 2 years ago • 1 comments

This improves the type inference of the temporary variable y.

MWE, on master

julia> struct A end

julia> struct B end

julia> struct C end

julia> Base.:(+)(::A, ::B) = C()

julia> @code_warntype Base.afoldl(+, A(), B())
MethodInstance for Base.afoldl(::typeof(+), ::A, ::B)
  from afoldl(op, a, bs...) @ Base operators.jl:531
Arguments
  #self#::Core.Const(Base.afoldl)
  op::Core.Const(+)
  a::Core.Const(A())
  bs::Core.Const((B(),))
Locals
  @_5::Union{}
  y::Union{A, C}
  i@_7::Int64
  l::Int64
  i@_9::Union{}
Body::C
[...]

PR

julia> @code_warntype Base.afoldl(+, A(), B())
MethodInstance for Base.afoldl(::typeof(+), ::A, ::B)
  from afoldl(op, a, bs...) @ Base operators.jl:531
Arguments
  #self#::Core.Const(Base.afoldl)
  op::Core.Const(+)
  a::Core.Const(A())
  bs::Core.Const((B(),))
Locals
  @_5::Union{}
  y::C
  i@_7::Int64
  l::Int64
  i@_9::Union{}
Body::C
[...]

jishnub avatar Aug 09 '22 13:08 jishnub

But @code_typed gives the same result with/without this PR? So I'm assuming there's no performance difference. (Our compiler should be good enough to track this kind of local instability.)

N5N3 avatar Aug 09 '22 15:08 N5N3

Yeah, it's only because return type information or the Locals information show result that is "squashed" to represent inference on a single frame, while our compiler actually sees types per-statement. So this change won't make performance difference.

aviatesk avatar Aug 10 '22 05:08 aviatesk

I see. In that case, I'll close this. If I see some real-world difference in the future, I'll open a separate issue.

jishnub avatar Aug 10 '22 12:08 jishnub