# Allocation on ForwardDiff + DiffResults + StaticArrays

**URL:** <https://discourse.julialang.org/t/allocation-on-forwarddiff-diffresults-staticarrays/46534>\
**Category:** General Usage\
**Tags:** forwarddiff\
**Created:** [September 13, 2020, 3:01am UTC](https://discourse.julialang.org/t/allocation-on-forwarddiff-diffresults-staticarrays/46534 "2020-09-13T03:01:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 13, 2020, 3:01am UTC](https://discourse.julialang.org/t/allocation-on-forwarddiff-diffresults-staticarrays/46534/1 "2020-09-13T03:01:50Z")

</div>

Im hunting down an allocation on DiffResults + ForwardDiff + StaticArrays. this is the MWE:

```julia
using ForwardDiff, DiffResults, StaticArrays, BenchmarkTools
fff(v1,v2) = sin(v1)+cos(v2)+exp(v1+log(v2+cos(v1)))
function ∂test(a1,a2)
    f(x) = fff(first(x),last(x))
    T = promote_type(typeof(a1),typeof(a2))
    _a1 = T(a1)
    _a2 = T(a2)
    a_vec = SVector(_a1,_a2)
    ∂result = DiffResults.GradientResult(a_vec)
    _∂f = ForwardDiff.hessian!(∂result, f,a_vec)
    val_f = DiffResults.value(_∂f)
    val_∂f = DiffResults.gradient(_∂f)
    return (val_f,val_∂f)
endfunction ∂2test(a1,a2)
    f(x) = fff(first(x),last(x))
    T = promote_type(typeof(a1),typeof(a2))
    _a1 = T(a1)
    _a2 = T(a2)
    a_vec = SVector(_a1,_a2)
    ∂result = DiffResults.HessianResult(a_vec)
    _∂f = ForwardDiff.hessian!(∂result, f,a_vec)
    val_f = DiffResults.value(_∂f)
    val_∂f = DiffResults.gradient(_∂f)
    val_∂2f = DiffResults.hessian(_∂f)
    return (val_f,val_∂f,val_∂2f)
end

@btime ∂test(0.4,0.4)
@btime ∂2test(0.4,0.4)

```

`∂test` doesn’t allocate, but `∂2test` does. any ideas on what’s happening?  
im almost sure the problem is on `ForwardDiff.hessian!` , but it worked fine with `gradient!` , does it need to specify a `ForwardDiff.Chunk`? is necesary a `HessianConfig` in the case of `StaticArrays` ?  
in the real use case, `fff` has more arguments and dispatches (but the test functions always fixes two of those arguments)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 13, 2020, 2:06pm UTC](https://discourse.julialang.org/t/allocation-on-forwarddiff-diffresults-staticarrays/46534/2 "2020-09-13T14:06:20Z")

</div>

I think your first function doesn’t run (you need a `HessianResult` for a Hessian).

I can confirm that this is happening, but I have not been able to solve it even with an explicit config. If you don’t get a more useful reply here, consider opening an issue for ForwardDiff.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 13, 2020, 2:09pm UTC](https://discourse.julialang.org/t/allocation-on-forwarddiff-diffresults-staticarrays/46534/3 "2020-09-13T14:09:12Z")

</div>

Cf

[https://github.com/JuliaDiff/ForwardDiff.jl/pull/315](https://github.com/JuliaDiff/ForwardDiff.jl/pull/315)

Something similar for Hessian may help resolve this.
