Closed chriselrod closed 3 years ago
Thanks. Can you please add some @inferred
tests so this does not happen again? Anywhere in the test file will do.
Unfortunately, I'm not sure how to make a test that fails on master to actually catch this issue. On master:
julia> @btime TransformVariables._sum_dimensions($trft)
410.345 ns (19 allocations: 544 bytes)
6
julia> @inferred TransformVariables._sum_dimensions(trft)
6
julia> @code_warntype TransformVariables._sum_dimensions(trft)
Variables
#self#::Core.Const(TransformVariables._sum_dimensions)
transformations::NamedTuple{(:CL_base, :CL_logwt, :v_base, :ω_1, :ω_2, :σ_0), Tuple{TransformVariables.ShiftedExp{true, Float64}, TransformVariables.Identity, TransformVariables.ShiftedExp{true, Float64}, TransformVariables.ShiftedExp{true, Float64}, TransformVariables.ShiftedExp{true, Float64}, TransformVariables.ShiftedExp{true, Float64}}}
Body::Int64
1 ─ %1 = (:init,)::Core.Const((:init,))
│ %2 = Core.apply_type(Core.NamedTuple, %1)::Core.Const(NamedTuple{(:init,), T} where T<:Tuple)
│ %3 = Core.tuple(0)::Core.Const((0,))
│ %4 = (%2)(%3)::Core.Const((init = 0,))
│ %5 = Core.kwfunc(TransformVariables.mapreduce)::Core.Const(Base.var"#mapreduce##kw"())
│ %6 = (%5)(%4, TransformVariables.mapreduce, TransformVariables.dimension, TransformVariables.:+, transformations)::Int64
└── return %6
The return type is stable and @code_warntype
also looks fine, but internally mapreduce
isn't and the problems show up in Cthulhu.@descend
, @code_typed
, and benchmarks.
I could do something like occursin
on the result of code_typed
.
So maybe this is best thought of us a workaround for a base issue.
Thanks for investigating this. Can you please add a comment then into the source with a short explanation, and maybe a reference to this issue, so that we will not modify it inadvertently?
Also, is there an issue in Julia about this?
I added the same comment to both instances. I'm not aware of a Julia issue.
I also added a test using @test iszero(@allocated ...)
which passes on this PR but fails on master.
Thanks for the nice PR, merging, and will tag a release too.
mapreduce
tends to be type unstable with hetogenous tuples whilemap
andreduce
are not.Note the
Union
s before:versus after
Benchmarks before and after: