# Equivalent of PreallocationTools.jl for functions

**URL:** <https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707>\
**Category:** General Usage\
**Created:** [February 17, 2026, 3:41pm UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707 "2026-02-17T15:41:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![densmojd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/densmojd/32/206891_2.png) [@densmojd](https://discourse.julialang.org/u/densmojd)\
**Post date:** [February 17, 2026, 3:41pm UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707/1 "2026-02-17T15:41:51Z")

</div>

A Julia design paradigm I often see to avoid allocating temporary arrays in repeated calculations is to use a struct that contains the temporary array and a “functor” for performing the calculation:

```julia
struct MyStruct 
    data::Vector{Float64}
    f::FunctionWrapper{Float64, Tuple{Float64, Float64}}
end

function (m::MyStruct)(u)
    # mutate u using m.data and m.f
end

```

In the above example, I’ve also included a function as a field of `MyStruct` that is used in the calculation, too. I’ve used [`FunctionWrappers.jl`](https://github.com/JuliaLang/FunctionWrappers.jl) so that this field has a concrete type.

To use this paradigm with [`ForwardDiff.jl`](https://github.com/JuliaDiff/ForwardDiff.jl), I know I can use [`PreallocationTools.jl`](https://github.com/SciML/PreallocationTools.jl) to replace the temporary array with a cache that can store both, e.g., `Float64` data as well as `Dual` data used by `ForwardDiff.jl`. Is there an equivalent approach for functions that would replace `FunctionWrappers.jl`, i.e., so I can have a function that takes and returns both `Float64` and `Dual` data but can be made a field of a struct with a concrete type?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [February 17, 2026, 3:52pm UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707/2 "2026-02-17T15:52:55Z")

</div>

I’m not sure I fully understand what you want to do and why the following pattern doesn’t work?

```julia
struct MyWrapper{F}
    f::F
end

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [February 17, 2026, 4:06pm UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707/3 "2026-02-17T16:06:25Z")

</div>

not sure if it fits your need but you may be interested in [GitHub - MasonProtter/Bumper.jl: Bring Your Own Stack](https://github.com/MasonProtter/Bumper.jl) ?

---

<div class="post-metadata">

**Author:** ![densmojd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/densmojd/32/206891_2.png) [@densmojd](https://discourse.julialang.org/u/densmojd)\
**Post date:** [February 17, 2026, 5:19pm UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707/4 "2026-02-17T17:19:55Z")

</div>

Yes, that would work, but in my actual case I might have half a dozen different functions, so the list of parameters could get complicated.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 18, 2026, 1:35am UTC](https://discourse.julialang.org/t/equivalent-of-preallocationtools-jl-for-functions/135707/5 "2026-02-18T01:35:09Z")

</div>

> **[GitHub - SciML/FunctionWrappersWrappers.jl](https://github.com/SciML/FunctionWrappersWrappers.jl)**
>
> Contribute to SciML/FunctionWrappersWrappers.jl development by creating an account on GitHub.
