I just tried out the following which indicates a type-instability which I can’t really interpret properly, but is probably the culprit:
@code_warntype ForwardDiff.gradient(w -> logl!(C, w), W) # takes very long and produces far too many memory allocations
MethodInstance for ForwardDiff.gradient(::var"#118#119", ::Matrix{Float64})
from gradient(f::F, x::AbstractArray) where F @ ForwardDiff ~/.julia/packages/ForwardDiff/UBbGT/src/gradient.jl:16
Static Parameters
F = var"#118#119"
Arguments
#self#::Core.Const(ForwardDiff.gradient)
f::Core.Const(var"#118#119"())
x::Matrix{Float64}
Body::Any
1 ─ %1 = ForwardDiff.GradientConfig(f, x)::ForwardDiff.GradientConfig{ForwardDiff.Tag{var"#118#119", Float64}, Float64, _A, Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#118#119", Float64}, Float64, _A1}, 2}} where {_A, _A1}
│ %2 = (#self#)(f, x, %1)::Any
└── return %2
All I undestand is that there is a type instability, but I don’t understand why. Neither do I know how to fix this.