# Performance issue when storing function in a struct as a field

**URL:** https://discourse.julialang.org/t/performance-issue-when-storing-function-in-a-struct-as-a-field/35122
**Category:** Performance
**Created:** [February 25, 2020, 4:18pm UTC](https://discourse.julialang.org/t/performance-issue-when-storing-function-in-a-struct-as-a-field/35122 "2020-02-25T16:18:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)
#### Post date: [February 25, 2020, 4:18pm UTC](https://discourse.julialang.org/t/performance-issue-when-storing-function-in-a-struct-as-a-field/35122/1 "2020-02-25T16:18:36Z")

</div>

I have a bit of code like this. In the function `simulation`, I generate a random tree and count the size of each subtree. Then I `sim.func` to the subtree sizes and add it up.

I mostly need `sim.func=log`. But to have some flexibility, I choose to make it a field in `SubtreeSizeSimulator`. However, this turns out to be about twice as slow as using `log` directly. Is there anyway to avoid such performance issue?

```julia
struct SubtreeSizeSimulator <: AbstractSimulator
    tree::FiniteTree
    func::Function
    funcname::String
end

function simulation(sim::SubtreeSizeSimulator)
    # simulate the random tree and get subtree sizes
    walker = SubtreeSizeWalker(sim.tree)
    walk(sim.tree, walker)
    subtree_sizes = result(walker)

    # sum over log of subtree sizes
    ret = 0
    for size in subtree_sizes
        #@fastmath ret += sim.func(size) # slow
        @fastmath ret += log(size) # much faster
    end
    ret
end

```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [February 25, 2020, 4:25pm UTC](https://discourse.julialang.org/t/performance-issue-when-storing-function-in-a-struct-as-a-field/35122/2 "2020-02-25T16:25:34Z")

</div>

You want to use

```julia
struct SubtreeSizeSimulator{F<:Function} <: AbstractSimulator
    tree::FiniteTree
    func::F
    funcname::String
end

```

The difference is that this new one specializes the struct on the specific function you use (all functions are their own type), so this will make it efficient as long as your code is still type stable. This will still cause dynamic dispatch issues if you have a bunch of these each with different functions though.

---

<div class="post-metadata">

### Author: ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)
#### Post date: [February 25, 2020, 4:34pm UTC](https://discourse.julialang.org/t/performance-issue-when-storing-function-in-a-struct-as-a-field/35122/3 "2020-02-25T16:34:19Z")

</div>

This indeed solves the problem. Thanks.
