# Not using PreallocationTools.jl correctly

**URL:** <https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609>\
**Category:** Performance\
**Tags:** question, forwarddiff, autodiff, preallocation\
**Created:** [December 9, 2024, 9:14am UTC](https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609 "2024-12-09T09:14:33Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [December 9, 2024, 11:27am UTC](https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609/6 "2024-12-09T11:27:14Z")

</div>

I just tried out the following which indicates a type-instability which I can’t really interpret properly, but is probably the culprit:

```julia
@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.

---

_[View the full topic](https://discourse.julialang.org/t/not-using-preallocationtools-jl-correctly/123609)._
