# What is the most idiomatic way to allocate a (reusable) buffer for a function?

**URL:** <https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892>\
**Category:** Performance\
**Created:** [October 12, 2023, 12:24pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892 "2023-10-12T12:24:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [October 12, 2023, 12:24pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/1 "2023-10-12T12:24:12Z")

</div>

Suppose I have a function `f` that allocates, for instance the following

```julia
function sum_of_fractions(a)
    v = map(x -> 1/(x+a), 1:10)
    return sum(v)
end

```

Assume for the sake of this discussion that I cannot get rid of the temporary vector v by converting the operation to a loop. To get rid of the allocations, one could pass a vector `v` to use as a buffer:

```julia
function sum_of_fractions(a, buffer)
    buffer .= map(x -> 1/(x+a), 1:10)
    return sum(buffer)

```

In this way, I can reuse the same buffer under multiple calls, without allocating more memory:

```julia
v = zeros(10)
for k = 1:100
    @show sum_of_squares(k, v)
end

```

This looks like it should be quite a common pattern in Julia. Is there a more idiomatic way to write it? What is the best way to do it?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [October 12, 2023, 12:30pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/2 "2023-10-12T12:30:17Z")

</div>

I think that providing a buffer is OK and quite Julian.  
In this simple case you could also avoid to materialize the array:

```julia
sum_of_fractions(a) =sum(x->1/(x+a),1:10)

```

or

```julia
sum_of_fractions_bis(a) = sum(x->inv(x+a),1:10)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2023, 1:00pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/3 "2023-10-12T13:00:05Z")

</div>

One common way is to provide the buffer as an optional keyword parameter, such as:

```julia
function foo(x; buffer = similar(x))
    ...
end

```

so that the function can be used with and without providing the buffer.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [October 12, 2023, 1:19pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/4 "2023-10-12T13:19:00Z")

</div>

But in this case, how do deal with the `!` convention ?

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [October 12, 2023, 1:30pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/5 "2023-10-12T13:30:37Z")

</div>

![pirates-caribbean-code](https://global.discourse-cdn.com/julialang/original/3X/f/1/f1513701d8f25d94aaa7c7838baf86728e027281.gif)

I dont think the `!` is truly neccessary if the function only mutates an optional(!) key word argument.

Another common pattern is

```julia
function dostuff!(buffer,args...)
  [...]
end
dostuff(args...) = dostuff!(allocateBuffer(args...),args...)

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [October 12, 2023, 1:31pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/6 "2023-10-12T13:31:37Z")

</div>

> [@fph](#):
>
> ```julia
> function y = sum_of_fractions(a, buffer)
> buffer .= map(x -> 1/(x+a), 1:10)
> return sum(buffer)
> end
> 
> ```

NB: even though a buffer was passed to it, this still allocates a vector to store the result of `map`. You’d need something like this to avoid allocating:

```julia
function y = sum_of_fractions!(buffer, a)
    map!(x -> 1/(x+a), buffer, 1:10)
    return sum(buffer)
end

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 12, 2023, 1:34pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/7 "2023-10-12T13:34:40Z")

</div>

> [@fph](#):
>
> Is there a more idiomatic way to write it?

For example:

```julia
function sum_of_fractions!(buffer::AbstractVector, a::Real)
    # https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured
    fun = let a = a
        x -> 1/(x + a)
    end

    map!(fun, buffer, 1:length(buffer))
    sum(buffer)
end

function sum_of_fractions(::Type{T}, len::Integer, a::Real) where {T}
  sum_of_fractions!(zeros(T, len), a)
end

```

Regarding the order of the parameters of the mutating function, see the manual’s style guide: [section](https://docs.julialang.org/en/v1/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base).

Instead of putting the closure into a local variable, it might sometimes be nicer to create it with a global function:

```julia
function my_callable_object(b)
    let a = b
        x -> 1/(x + a)
    end
end

```

Then create your function by calling `my_callable_object`, like `my_callable_object(a)`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 12, 2023, 1:39pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/8 "2023-10-12T13:39:30Z")

</div>

> [@LaurentPlagne](#):
>
> But in this case, how do deal with the `!` convention ?

Actually in this case I would _not_ use it, and have the name of the variable (`buffer`) be very clear in that it won’t use the content of `buffer` in the computation. The other alternative, mentioned above, is to have two methods:

```julia
function foo(x)
    buffer = similar(x)
    return foo!(buffer, x)
end

function foo!(buffer, x)
    ...
end

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 12, 2023, 2:11pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/9 "2023-10-12T14:11:50Z")

</div>

Also of note to know about in case it helps: Bumper.jl. I believe it’s very stable and useful now on Linux at least. It will also Work on Windows, less tested, and if it seems to work there or on any platform then it’s as good as on Linux. It’s just that Windows doesn’t overcommit, so it could possibly run out of space in extreme situations.

> [@lmiq](#):
>
> `function foo(x; buffer = similar(x))`

Is this idiomatic? I believe you would (want to) define `foo!` if you want to change arguments. And would have a corresponding `foo`, that doesn’t and reuse that code. Though your way works too, to simplify into one function. Using ! is actually just a convention, though useful to maintain.

I’m thinking, since it’s a common pattern, to have these two similarily named functions, can and should it be handled with a macro, that defines the other for you?

```julia
function y = sum_of_fractions(a)

```

Note, this is not idiomatic. You could skip `y =` I think this may be from or similar syntax to MATLAB?! [Or you can skip function and use the short form. Though I believe that 3rd form (and more!) works too and you’re making a function named y, as synonym?]

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 12, 2023, 2:36pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/10 "2023-10-12T14:36:15Z")

</div>

> [@Palli](#):
>
> Note, this is not idiomatic.

It’s a syntax error, in fact.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [October 12, 2023, 2:42pm UTC](https://discourse.julialang.org/t/what-is-the-most-idiomatic-way-to-allocate-a-reusable-buffer-for-a-function/104892/11 "2023-10-12T14:42:55Z")

</div>

> [@Palli](#):
>
> ```julia
> function y = sum_of_fractions(a)
> 
> ```
> 
> Note, this is not idiomatic. You could skip `y =` I think this may be from or similar syntax to MATLAB?!

Oops yes, I wrote this example code from scratch in the forum post and I included a Matlabism. Sorry, I’ll fix it.
