Type instability of pdf of MixtureModel

When I create a Normal one-dimensional mixture as,

d =  MixtureModel([Normal(-1,1), Normal(1, 1)], [0.5,0.5])

it turns out that the pdf is not type stable:

julia> @code_warntype pdf(d,0.3)
MethodInstance for Distributions.pdf(::MixtureModel{Univariate, Continuous, Normal{Float64}, Float64}, ::Float64)
from pdf(d::UnivariateMixture{Continuous}, x::Real) in Distributions at /home/sgaure/.julia/packages/Distributions/Xrm9e/src/mixtures/mixturemodel.jl:419
Arguments
#self#::Core.Const(Distributions.pdf)
d::MixtureModel{Univariate, Continuous, Normal{Float64}, Float64}
x::Float64
Body::Any
1 ─ %1 = Distributions._mixpdf1(d, x)::Any
└── return %1

The pdf is used for integration inside a likelihood optimization, and this type instability slows it down by a non-trivial factor. This is of course quite simple to get around by coding the pdf myself for the particular flavours of it I need, but it ought to be handled automatically. Also, the pdf does quite some allocations. This could be avoided, I think.

Is there some simple trick to make type inference work?

I’m not seeing this type-instability. Can you double-check in a clean environment and share the outputs of Pkg.status() and versioninfo() as done below?

julia> using Distributions

julia> d =  MixtureModel([Normal(-1,1), Normal(1, 1)], [0.5,0.5])
MixtureModel{Normal{Float64}}(K = 2)
components[1] (prior = 0.5000): Normal{Float64}(μ=-1.0, σ=1.0)
components[2] (prior = 0.5000): Normal{Float64}(μ=1.0, σ=1.0)

julia> @code_warntype pdf(d,0.3)
Variables
  #self#::Core.Const(Distributions.pdf)
  d::MixtureModel{Univariate, Continuous, Normal{Float64}, Categorical{Float64, Vector{Float64}}}
  x::Float64

Body::Float64
1 ─ %1 = Distributions._mixpdf1(d, x)::Float64
└──      return %1

julia> using Pkg; Pkg.status()
      Status `/private/var/folders/5c/7l7zjpjj4fs6t31xlh4hjggm0000gn/T/jl_FhzLU5/Project.toml`
  [31c24e10] Distributions v0.25.16

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:

Ah, thanks, I was in an environment with an old version.