# Allocations with nested Jacobians with StaticArrays and ForwardDiff

**URL:** <https://discourse.julialang.org/t/allocations-with-nested-jacobians-with-staticarrays-and-forwarddiff/136789>\
**Category:** General Usage\
**Created:** [April 20, 2026, 9:13am UTC](https://discourse.julialang.org/t/allocations-with-nested-jacobians-with-staticarrays-and-forwarddiff/136789 "2026-04-20T09:13:26Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [April 20, 2026, 1:16pm UTC](https://discourse.julialang.org/t/allocations-with-nested-jacobians-with-staticarrays-and-forwarddiff/136789/2 "2026-04-20T13:16:35Z")

</div>

> [@Albert\_de\_montserrat](#):
>
> ```julia-auto
> f(x) = SVector(x[1]^2 * x[2], sin(x[1]) + x[2]^3)
> 
> function J_flat(x) 
> y = ForwardDiff.jacobian(f, x)
> SA[y[1], y[2]]
> end
> 
> function nested_jacobian(x0::SVector{2, T}) where T
> ForwardDiff.jacobian(J_flat, x0)
> end
> 
> x0 = SVector(1.0, 2.0)
> @code_warntype nested_jacobian(x0) # shows a type instability
> @b nested_jacobian($x0)
> 
> ```

I think there have been a few threads on this over the years, but the only one I can find at the moment is my own from quite a while ago now: [Type stability for higher derivatives in ForwardDiff](https://discourse.julialang.org/t/type-stability-for-higher-derivatives-in-forwarddiff/66686)

As best as I’m aware there is currently no easy workaround for this. However, if some manual intervention is feasible in your case, then you can do something like this:

```julia-auto
f(x) = SVector(x[1]^2 * x[2], sin(x[1]) + x[2]^3)

function J_flat(x)  
    y1 = ForwardDiff.derivative(x1 -> f(SA[x1, x[2]])[1], x[1])
    y2 = ForwardDiff.derivative(x1 -> f(SA[x1, x[2]])[2], x[1])

    SA[y1, y2]
end

function nested_jacobian(x0::SVector{2, T}) where T
    ForwardDiff.jacobian(J_flat, x0)
end

x0 = SVector(1.0, 2.0)
@code_warntype nested_jacobian(x0) # Is now type stable

```

Edit: Forgot to get first index of the output of `f`  
Edit 2: Got the wrong indices I think. I guess this showcase why doing this manually is less than ideal.

---

_[View the full topic](https://discourse.julialang.org/t/allocations-with-nested-jacobians-with-staticarrays-and-forwarddiff/136789)._
