# How to support passing temporary buffers around

**URL:** <https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880>\
**Category:** Internals & Design\
**Tags:** memory-allocation, functions\
**Created:** [November 3, 2021, 2:45pm UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880 "2021-11-03T14:45:23Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [November 8, 2021, 9:44am UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880/21 "2021-11-08T09:44:28Z")

</div>

You might want to have a look at [AutoPreallocation.jl](https://github.com/oxinabox/AutoPreallocation.jl).

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [March 14, 2022, 9:22pm UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880/22 "2022-03-14T21:22:41Z")

</div>

@fverdugo - I’ve been doing something very very similar in my own codes for a while. I’ll need to look at Gridap to compare. Do I understand correctly that the callable `f` would need to keep an object pool it returns the cache to? One problem that I encountered and haven’t really solved is the case when the type of the cache depends on the type of the input data. E.g. this might happen if I evaluate with a Float or with a Dual for ForwardDiff. How do you solve this?

Independently - something I’m playing with right now, is to attach this object pool to the cache array and then call `release!` on it without having to remember where it is from :). Something like this:

```julia
struct CachedArray{N, T}
   A::Array{N, T}
   pool::ArrayCache{T}
end

release!(pA::CachedArray) = release!(pA.pool, pA)

Base.getindex(pA::CachedArray, args...) = getindex(pA.A, args...)

```

Then a callable object `f` would return a `A::CachedArray`, which the caller via `A = evaluate(f, ...)` and when done it can be released via `release!(A)` _anywhere_ without needed to remember where `A` came from. This seems to simplify my codes quite a bit, but I haven’t extensively tested the idea yet, so might still run into problems.

---

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [March 31, 2022, 5:56am UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880/23 "2022-03-31T05:56:47Z")

</div>

> [@cortner](#):
>
> One problem that I encountered and haven’t really solved is the case when the type of the cache depends on the type of the input data. E.g. this might happen if I evaluate with a Float or with a Dual for ForwardDiff. How do you solve this?

Hi @cortner! We solve this by parametrizing the cache creation also with the inputs of the function:

```julia
cache = return_cache(f,x...)
fx = evaluate!(cache,f,x...)

```

Then the user code is consistent with this invariant: The cache object created for args of types as in x… can only be used when calling the function with args of the same type. Note that the cache creation only needs info about the types of the args x… in general.

Since this mechanism is quite general purpose, I am considering to factor this interface out of Gridap and release it in a separate package with almost no dependencies. Perhaps it will be interesting for others.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [March 31, 2022, 2:47pm UTC](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880/24 "2022-03-31T14:47:36Z")

</div>

Ive used this mechanism as well but it is still problematic when you are composing several layers of function calls and makes for very ugly codes trying to predict the occurring types along the way. It could be context specific how generally useful it is. I’ve certainly found this worked very well indeed in many cases.

[Previous page](https://discourse.julialang.org/t/how-to-support-passing-temporary-buffers-around/70880.md?page=1)
