# A curious case of allocating behavior

**URL:** <https://discourse.julialang.org/t/a-curious-case-of-allocating-behavior/3896>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [May 24, 2017, 2:24am UTC](https://discourse.julialang.org/t/a-curious-case-of-allocating-behavior/3896 "2017-05-24T02:24:51Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 24, 2017, 3:21am UTC](https://discourse.julialang.org/t/a-curious-case-of-allocating-behavior/3896/2 "2017-05-24T03:21:56Z")

</div>

> [@traktofon](#):
>
> My guess is that it’s related to inlining

Bingo. You’re hitting what is frequently referred to as the splatting penalty. Splatting arguments into varargs functions like getindex can be allocation-free, but only the function inlines into the caller. Otherwise, Julia needs to allocate a tuple on the heap in order to pass the indices to `getindex`. That’s where you’re seeing the bulk of the allocations come from with the more complicated versions of your object — the computation becomes complex enough that the indexing call no longer inlines. So you can add an inline annotation to the function to ensure it inlines and avoid the allocation.

Adding inline annotations to `@generated` functions is also tricky. Doing the obvious things (`@inline @generated function …`) doesn’t work because you’re actually annotating the generator function (and not the generated one). Instead, you can use the un-exported helper function to do this:

```julia
    return :(Base.@_inline_meta; $ex)

```

That puts the inline annotation into the generated function. See [Call-site splatting optimization · Issue #13359 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/13359) for more details.

---

_[View the full topic](https://discourse.julialang.org/t/a-curious-case-of-allocating-behavior/3896)._
