# ForwardDiff Lie brackets allocate with StaticArrays

**URL:** <https://discourse.julialang.org/t/forwarddiff-lie-brackets-allocate-with-staticarrays/137248>\
**Category:** Performance\
**Tags:** memory-allocation, forwarddiff, staticarrays, autodiff\
**Created:** [May 22, 2026, 9:34pm UTC](https://discourse.julialang.org/t/forwarddiff-lie-brackets-allocate-with-staticarrays/137248 "2026-05-22T21:34:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tremelow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tremelow/32/221943_2.png) [@tremelow](https://discourse.julialang.org/u/tremelow)\
**Post date:** [May 22, 2026, 9:34pm UTC](https://discourse.julialang.org/t/forwarddiff-lie-brackets-allocate-with-staticarrays/137248/1 "2026-05-22T21:34:52Z")

</div>

I’m working on a code where I need Lie brackets of Lie brackets, but computing the Jacobian of a Lie bracket allocates data. Here’s a MWE

```julia
using StaticArrays
using BenchmarkTools
import ForwardDiff

f(x::SVector{2,T}) where T = SVector{2,T}(x[1] * exp(x[2]), -exp(x[1]))
g(x::SVector{2,T}) where T = SVector{2,T}(exp(0.9 * x[2]), -1.2 * exp(x[1]))
x0 = @SVector [0.2, 0.5]

function fg(x::SVector{2,T}) where T
    fx, jacfx = f(x), ForwardDiff.jacobian(f, x)
    gx, jacgx = g(x), ForwardDiff.jacobian(g, x)
    return SVector{2,T}(jacgx * fx - jacfx * gx)
end

jacfg(x::SVector{2,T}) where T = ForwardDiff.jacobian(fg, x)

@btime jacfg($x0) # 117.574 ns (4 allocations: 176 bytes)

```

I tried the fixes found in [a previous post](https://discourse.julialang.org/t/allocations-with-nested-jacobians-with-staticarrays-and-forwarddiff/136789). One of them was to strictly force the input/output types of `fg` (which is the case above), but this clearly did not work.

Another fix was to repeat the definition of the function `fg` after the first call of `jacfg`, but this is hardly sustainable practice, especially with anonymous functions.

Another workaround I found was to pre-call the Jacobians of `f` and `g` with an appropriate argument:

```julia
Dfg = ForwardDiff.Dual{ForwardDiff.Tag{typeof(fg),Float64}}
xtmp = SVector([Dfg(xi, 0.0) for xi in x0])
ForwardDiff.jacobian(f, xtmp)
ForwardDiff.jacobian(g, xtmp)

```

This still seems _ad-hoc_, but more manageable. Does anyone know how to fix/generalize this? Thanks in advance for your help!
