# How to efficiently splat values into argument list?

**URL:** <https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177>\
**Category:** Performance\
**Created:** [January 28, 2019, 12:40pm UTC](https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177 "2019-01-28T12:40:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShnitzelKiller](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@ShnitzelKiller](https://discourse.julialang.org/u/ShnitzelKiller)\
**Post date:** [January 28, 2019, 12:40pm UTC](https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177/1 "2019-01-28T12:40:40Z")

</div>

I’ve found that using generator syntax in argument lists seems to cause unwanted allocations.  
For example, I have a piece of code that does something very simple, taking the slices of a 3D array and turning them into RGB colors in a 2D array (with some transformation). The code below performs fine with no allocations.

```julia
array = randn(100, 100)

function display_array(image, array)
    for I in CartesianIndices(image)
        rg = @view array[:,I]
        image[I] = RGB(rg[1]*0.3+0.5, rg[2]*0.3+0.5, 0.5)
    end
    return image
end
function display_array(array)
    image = Array{RGB{eltype(array)}}(undef, size(array)[2:end]...)::Array{RGB{eltype(array)}, 2}
    return display_normals(image, array)
end
image = display_array(array)

```

But when I change this to use a generator to reduce the repeated code,

```julia
function display_array(image, array)
    for I in CartesianIndices(image)
        rg = @view array[:,I]
        image[I] = RGB((c*0.3+0.5 for c in rg)..., 0.5)
    end
    return image
end

```

There are tens of thousands of allocations happening in the inner loop. I’ve tried using list comprehensions and using broadcasting math on the slice directly but all of these result in more allocations. I can’t use a loop because I’m generating values to be fed into the constructor RGB and don’t want to allocate an array for these values.  
Is metaprogramming the only solution to write performant and non-repetitive code in an example like this? Is there a macro equivalent to generators/comprehensions?  
EDIT: At the suggestion of others, I wrote a self-contained example, also removed references to normals since it’s not relevant.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 28, 2019, 1:09pm UTC](https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177/2 "2019-01-28T13:09:18Z")

</div>

Please quote your code using backticks, and provide a self-contained example (with `image`, `normals`, etc).

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

The first think I would try is making `normals` an array of `StaticArrays.SVector`.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 28, 2019, 1:21pm UTC](https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177/3 "2019-01-28T13:21:45Z")

</div>

If it’s all about getting rid of repeated logic, you can extract it to a method. It will be inlined, so won’t affect performance:

```julia
for I in CartesianIndices(image)
    rg = @view normals[:,I]
    t(c) = c*0.3+0.5
    image[I] = RGB(t(rg[1]), t(rg[2]), 0.5)
end

```

---

<div class="post-metadata">

**Author:** ![ShnitzelKiller](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@ShnitzelKiller](https://discourse.julialang.org/u/ShnitzelKiller)\
**Post date:** [January 28, 2019, 11:42pm UTC](https://discourse.julialang.org/t/how-to-efficiently-splat-values-into-argument-list/20177/4 "2019-01-28T23:42:17Z")

</div>

Thanks, it’s good to know that functions like this have no overhead.
