# Allocating OnlineStats on the stack

**URL:** https://discourse.julialang.org/t/allocating-onlinestats-on-the-stack/110856
**Category:** Performance
**Created:** [February 27, 2024, 4:19pm UTC](https://discourse.julialang.org/t/allocating-onlinestats-on-the-stack/110856 "2024-02-27T16:19:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mattwigway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwigway/32/22520_2.png) [@mattwigway](https://discourse.julialang.org/u/mattwigway)
#### Post date: [February 27, 2024, 4:19pm UTC](https://discourse.julialang.org/t/allocating-onlinestats-on-the-stack/110856/1 "2024-02-27T16:19:47Z")

</div>

I’m using OnlineStats to calculate a likelihood function, so I’m creating and destroying a LogSumExp for each row. LogSumExps, like all online stats, are mutable structs, so heap-allocated in general, but I thought that since the LogSumExp is not visible outside the function the optimizer would get rid of those allocations [as discussed here](https://discourse.julialang.org/t/why-mutable-structs-are-allocated-on-the-heap/12992). However, the code in the MWE below prints `0.000000 seconds (1 allocation: 112 bytes)`. Since this happens once per row, it’s a significant performance penalty, any suggestions?

MWE:

```julia
using OnlineStats

function logsumexp(vals)
    result = LogSumExp()

    for val in vals
        fit!(result, val)
    end

    x = value(result)

    return x
end

function main()
    logsumexp([1, 2, 3, 4, 5, 6])
    @time logsumexp([1, 2, 3, 4, 5, 6])
end

main()

```

Actual example code: [DiscreteChoiceModels.jl/src/mnl.jl at main · mattwigway/DiscreteChoiceModels.jl · GitHub](https://github.com/mattwigway/DiscreteChoiceModels.jl/blob/main/src/mnl.jl#L42)

---

<div class="post-metadata">

### Author: ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)
#### Post date: [February 27, 2024, 4:25pm UTC](https://discourse.julialang.org/t/allocating-onlinestats-on-the-stack/110856/2 "2024-02-27T16:25:48Z")

</div>

If I understand correctly, you are running your `logsumexp` multiple times inside of a loop, thus being penalized by its allocations.

The options I can think of are:

1. Rework your `logsumexp` to take a pre-allocated buffer `LogSumExp` and zero it out each time at the start of the function.
2. Try this experimental bumper allocator [GitHub - MasonProtter/Bumper.jl: Bring Your Own Stack](https://github.com/MasonProtter/Bumper.jl) (it will probably require some modification of how `logsumexp` is set up).

I do not have domain knowledge here, so I might be missing a more obvious domain-specific solution.

---

<div class="post-metadata">

### Author: ![mattwigway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattwigway/32/22520_2.png) [@mattwigway](https://discourse.julialang.org/u/mattwigway)
#### Post date: [February 27, 2024, 4:26pm UTC](https://discourse.julialang.org/t/allocating-onlinestats-on-the-stack/110856/3 "2024-02-27T16:26:22Z")

</div>

So, in my example code, I just realized the allocation is coming from creating the Vector. That’s not the case in the actual code, I will work on making a better MWE.
