# Unnecessary allocations happening at big StaticArray expression

**URL:** https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005
**Category:** Performance
**Created:** [December 26, 2018, 6:36pm UTC](https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005 "2018-12-26T18:36:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [December 26, 2018, 6:36pm UTC](https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005/1 "2018-12-26T18:36:29Z")

</div>

I’m thinking this might actually be a bug report… I am working with matrix exponentiation, adapting the original Julia function first to be able to use ForwardDiff, and now trying to optimize the code as much as I can. More specifically, I am now trying to get rid of allocations.

This is a quite big expression, but the most scary part is a left-divide, and Julia handles it very nicely, not to say miraculously. The function works, and I get a staticArray in the end. But I was still strangely getting allocations.

I narrowed down the source of the allocation to the following code snippet:

```julia
function myexpr(A)
    A2 = A * A
    A4 = A2 * A2
    A6 = A2 * A4
    
    CC = @SVector [
            64764752532480000.,32382376266240000.,7771770303897600.,
               1187353796428800., 129060195264000., 10559470521600.,
                   670442572800., 33522128640., 1323241920.,
                       40840800., 960960., 16380.,
                            182., 1.]

    A6 * (CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2) .+ CC[8].*A6 .+ CC[6].*A4 .+ CC[4].*A2

    # Ua = A6 * (CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2)
    # Uo = CC[8].*A6 .+ CC[6].*A4 .+ CC[4].*A2
    # Ua .+ Uo
end
myexpr(aa)
@time myexpr(aa)

```

In the original expression we get 950 allocations, but using the commented block we get instead the expected REPL 5.

Just using paretheses like `A6 * (CC[14].*A6 .+ CC[12].*A4 .+ CC[10].*A2) .+ (CC[8].*A6 .+ CC[6].*A4 .+ CC[4].*A2)` also works. Removing one of the sum terms also.

I can only imagine somewhere in the compiler there must be some sort of declaration of specialized StaticArray expression handling that only goes up to 4 sum terms, or something like that… Is this a known issue? Anybody ever had a similar experience? How do I avoid this?

---

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [December 26, 2018, 6:49pm UTC](https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005/2 "2018-12-26T18:49:46Z")

</div>

Note: I just found out the StaticArrays package offers an implementation of `expm()` that might solve all my problems. But the question remains why that happens

---

<div class="post-metadata">

### Author: ![schmrlng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schmrlng/32/2325_2.png) [@schmrlng](https://discourse.julialang.org/u/schmrlng)
#### Post date: [December 27, 2018, 3:53pm UTC](https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005/3 "2018-12-27T15:53:17Z")

</div>

Yes, I’ve also experienced this before – this is why the implementation of expm in StaticArrays.jl also has some apparently unnecessary parentheses: [StaticArrays.jl/expm.jl at master · JuliaArrays/StaticArrays.jl · GitHub](https://github.com/JuliaArrays/StaticArrays.jl/blob/master/src/expm.jl#L105).

I’m not sure what the exact cause is, but I speculate that it might have something to do with “recursion limiting heuristics” in the Julia compiler, a phrase I’ve seen mentioned a few times in association with strange StaticArrays allocations.

---

<div class="post-metadata">

### Author: ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)
#### Post date: [December 27, 2018, 6:41pm UTC](https://discourse.julialang.org/t/unnecessary-allocations-happening-at-big-staticarray-expression/19005/4 "2018-12-27T18:41:07Z")

</div>

OK, great. Thanks for the code by the way 🙂 I’ll probably switch to it now depending on an unrelated issue.
