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

Does multihypo NamedTuple recipe dynamic dispatch?

Open dehann opened this issue 3 years ago • 10 comments

Oh, btw, think the same thing might be happening with multihypo recipes: https://github.com/JuliaRobotics/IncrementalInference.jl/blob/40962b76cc03c55b49bfded871a7db60f0a0fd22/src/ExplicitDiscreteMarginalizations.jl#L227

Originally posted by @dehann in https://github.com/JuliaRobotics/IncrementalInference.jl/issues/1564#issuecomment-1185633151

dehann avatar Jul 15 '22 15:07 dehann

Probably best to just change the recipe to a hard type.

dehann avatar Jul 15 '22 15:07 dehann

Should also fix related:

https://github.com/JuliaRobotics/IncrementalInference.jl/blob/40962b76cc03c55b49bfded871a7db60f0a0fd22/src/services/EvalFactor.jl#L155-L157

Affie avatar Jul 15 '22 15:07 Affie

and: https://github.com/JuliaRobotics/IncrementalInference.jl/blob/40962b76cc03c55b49bfded871a7db60f0a0fd22/src/services/EvalFactor.jl#L235-L237

Affie avatar Jul 15 '22 15:07 Affie

(;certainidx, allelements, activehypo, mhidx)

That should actually be fine.

Affie avatar Jul 15 '22 15:07 Affie

Okay, but let me just ocnfirm. A dispatch as function argument should only dispatch once, so all these cases above are fine. The problem is when a struct field has type

struct MyType1{T <: NamedTuple}
  x::T
end

Thats when its bad?

Obviously the forced abstract type will be bad:

struct MyType2
  x::NamedTuple
end

dehann avatar Jul 15 '22 15:07 dehann

Yes I agree. The bad case is if the names are variable. The functions will compile a new method for each name.

Affie avatar Jul 15 '22 16:07 Affie

Yes I agree. The bad case is if the names are variable.

So is this fine to use with good performance?

struct MyType1{T <: NamedTuple}
  x::T
end

dehann avatar Jul 15 '22 16:07 dehann

It will be a tradeoff between compile time and runtime. Every new NamedTuple will require a new method to be compiled.

Affie avatar Jul 15 '22 16:07 Affie

This tip is relevant here: https://docs.julialang.org/en/v1/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)

Affie avatar Jul 26 '22 15:07 Affie

Agreed, thanks.

dehann avatar Jul 29 '22 01:07 dehann

Changed this to a hard type HypoRecipe anyway:

  • #1669

dehann avatar Jan 02 '23 07:01 dehann