# Mutable struct vs closure

**URL:** <https://discourse.julialang.org/t/mutable-struct-vs-closure/20233>\
**Category:** New to Julia\
**Tags:** struct, closure\
**Created:** [January 29, 2019, 2:17pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233 "2019-01-29T14:17:03Z")\
**Posts on this page:** 15\
**Page:** 4

<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:** [January 31, 2019, 6:58pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/62 "2019-01-31T18:58:32Z")

</div>

The length of Julia’s IR/LLVM IR/Assembly doesn’t really directly determine complexity/better/worse. You gotta benchmark to see any effect unless you know that particular IR and your machine very well. All of these forms can call functions which can hide such complexity — and in this case I’d guess the inlined version and the impure version are probably performing about the same (definitely not much much worse).

Now, yes, in this case inlining and constant propagation didn’t work because you’re accessing (and modifying!) an array of arrays with non-constant values. I’d give up on the cache and just code it straight — it’s easier to see bugs this way, too:

```julia
       function binomsum(n::Int, i::Int)
           s = 0
           for j in 0:i-1 # was this off-by-one intentional?
               s += binomial(n, j)
           end
           s
       end

```

Now, I _think_ you may be able to mark this one `Base.@pure`. If you do so you get the same effect. If `binomial` got `@inlined`, then it can constant-propagate, but even still we aren’t using constants in our for-loop there. Probably easier to just [`@memoize`](https://github.com/JuliaCollections/Memoize.jl) things and not worry about the lookup branch unless you see it coming up as a major factor in a profiling run.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 31, 2019, 7:01pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/63 "2019-01-31T19:01:20Z")

</div>

> [@mbauman](#):
>
> Now, I _think_ you may be able to mark this one `Base.@pure` .

Nope, at least not as far as multi-threading goes:

```Julia
julia> Base.@pure function binomsum(n::Int, i::Int)
                  s = 0
                  for j in 0:i-1 # was this off-by-one intentional?
                      s += binomial(n, j)
                  end
                  s
              end

julia> Base.Threads.@threads for i ∈ 1:16
           println(binomsum(i,2))
       end
8
9
signal (11): Segmentation fault
in expression starting at no file:0
jl_apply_generic at /usr/bin/../lib/libjulia.so.1 (unknown line)
unknown function (ip: 0x7f544cd920cd)
unknown function (ip: 0xffffffffffffffff)
Allocations: 18824129 (Pool: 18819991; Big: 4138); GC: 55
Segmentation fault (core dumped)

```

---

<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:** [January 31, 2019, 7:02pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/64 "2019-01-31T19:02:24Z")

</div>

Yup. See? Don’t use `@pure`. I can’t get it right, either. Mostly because my name doesn’t happen to be Jameson.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 31, 2019, 7:03pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/65 "2019-01-31T19:03:32Z")

</div>

If `@pure` can’t be used, there needs to be some other way of ensuring the constant propagation.

And yes, the off by one was intentional, because I needed to subtract 1 later anyway, so it saves a step.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [January 31, 2019, 7:09pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/66 "2019-01-31T19:09:17Z")

</div>

`binomial` can throw an `OverflowError`; that seems like it could be (part of) the reason that `@pure` isn’t allowed here.

---

<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:** [January 31, 2019, 7:14pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/67 "2019-01-31T19:14:33Z")

</div>

Here’s how you can get constant propagation working:

```julia
# Mark binomial as `@inline` using the definition from Base:
julia> @eval Base @inline function binomial(n::T, k::T) where T<:Integer
           ...
       end

# Define `binomsum` with some of the loops unrolled — this could be done via metaprogramming
julia> @inline function binomsum(n, i)
           i == 0 && return 0
           s = binomial(n, 0)
           i == 1 && return s
           s += binomial(n, 1)
           i == 2 && return s
           s += binomial(n, 2)
           i == 3 && return s
           s += binomial(n, 3)
           i == 4 && return s
           for j in 4:i-1
               s += binomial(n, j)
           end
           s
       end
binomsum (generic function with 1 method)

julia> f() = binomsum(5, 2)
f (generic function with 1 method)

julia> f()
6

julia> @code_warntype f()
Body::Int64
1 ─ return 6

```

This makes for a monstrously huge function body, though. It’d be nice if there were a better way.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 31, 2019, 7:14pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/68 "2019-01-31T19:14:33Z")

</div>

Your segfault comes from `println`, which is not threadsafe.

@mbauman’s `binomsum` is almost `@pure`, except for the unfortunate side-effect that `binomial` decides to throw overflow errors on some inputs. I think this precludes `@pure`?

Without this execution path, the compiler would probably figure out that it is `@pure` without any “helpful” annotation.

Someone should maybe implement `unsafe_binomial` without overflow checking, or return `-1` as a guard value for overflowing things.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 31, 2019, 7:17pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/69 "2019-01-31T19:17:08Z")

</div>

Alternatively, would using a shared array or distributed array resolve the issue with array access?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 31, 2019, 7:19pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/70 "2019-01-31T19:19:26Z")

</div>

> [@mbauman](#):
>
> Mostly because my name doesn’t happen to be Jameson.

To quote Tim:

```julia
help?> Base.@pure
  @pure ex
  @pure(ex)

  @pure gives the compiler a hint for the definition of a pure function,
  helping for type inference.

  A function is pure if and only if Jameson Nash says that it is pure.

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 31, 2019, 7:46pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/71 "2019-01-31T19:46:20Z")

</div>

> [@foobar\_lv2](#):
>
> Your segfault comes from `println` , which is not threadsafe.

Now that I understand this, I found that the cached version is actually thread safe.

That issue can easily be resolved simply by making sure that `binomsum` gets called for the needed value of `n` at least once before it is used in multiple threads. If the method has been called once for the relevant value of `n`, then the method is thread safe since it doesn’t need to extend the array in that case. In my case, it would be enough to simply call `binomsum(16,1)` once at the beginning, and then all other calls should be thread safe (since I only handle `n<17` in my package).

```Julia
const binomsum_cache = [[1]]
Base.@pure function binomsum(n::Int, i::Int)
    for k=length(binomsum_cache)+1:n
        push!(binomsum_cache, cumsum([binomial(k,q) for q=0:k]))
    end
    i ≠ 0 ? binomsum_cache[n][i] : 0
end

```

no problem with `@threads` without `println`

```Julia
julia> binomsum(16,1);

julia> Base.Threads.@threads for i ∈ 2:16
           binomsum(i,2)
       end

```

One solution is to simply put `binomsum(16,1)` into the ` __init__ ()` script, where 16 is the max value of `n` encountered. This ensures that issues with `@pure` and multi-threading are avoided.

---

<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 31, 2019, 9:06pm UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/72 "2019-01-31T21:06:16Z")

</div>

Constant propagation. I know, it’s stupid 🙂

```julia
julia> bsum(::Type{Val{N}}, ::Type{Val{K}}) where {N,K} =
       K == 0 ? 0 : N == 0 ? 1 : bsum(Val{N-1}, Val{min(K, N)}) + bsum(Val{N-1}, Val{K-1});

julia> bsum(Val{5}, Val{4})
26

julia> @code_llvm bsum(Val{5}, Val{4})
define i64 @julia_bsum_12186(%jl_value_t addrspace(10)*, %jl_value_t addrspace(10)*) {
top:
  ret i64 26
}

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 1, 2019, 1:58am UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/73 "2019-02-01T01:58:38Z")

</div>

> [@foobar\_lv2](#):
>
> Closures are not a first-class concept in julia. They are `struct <: Function` , and their layout is generated by the compiler during lowering

how could I inspect the generated layout of a closure? I mean, I could learn how to build `struct <: Function` then. thanks.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 1, 2019, 2:00am UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/74 "2019-02-01T02:00:20Z")

</div>

no, I mean I do not understand how that “copy constructor” was called in your example. “copy constructor” is new to me.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 1, 2019, 4:14am UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/75 "2019-02-01T04:14:20Z")

</div>

The phrase “copy constructor” is not used so much in Julia (it comes e.g. from C++), which is why I put it in quotes.

It means a constructor in which you pass in an argument of a given type and it constructs a new object of that type by _copy_ing the fields from the argument to the new object.

My point was that by default a `Vector` will _not_ be copied if you do this; you will just have a reference to the same `Vector` as in the original object, so if you modify one the other gets modified too. You can use the `copy` function if you actually want a new copy.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [February 1, 2019, 9:32am UTC](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233/76 "2019-02-01T09:32:52Z")

</div>

You can use all the reflection things for the type:

```julia
julia> tp = typeof(closuremaker())
getfield(Main, Symbol("#inc#6"))
julia> fieldnames(tp), fieldtypes(tp), tp.name, supertype(tp), tp.mutable
((:x,), (Core.Box,), getfield(Main, Symbol("#inc#6")), Function, false)

```

We see that `inc` is an immutable `struct <: Function`, with a fancyful name and a single member field `x`, of type `Core.Box`.

In order to see the methods, let’s look at:

```julia
julia> tp.name.mt
# 1 method for generic function "(::inc)":
[1] (::getfield(Main, Symbol("#inc#6")))() in Main at REPL[78]:4

```

We see that `inc` has a single method without any arguments.

It would be nice if there was some pretty-printer for types in julia (give me a reconstructed julia-syntax like thing, with annotations for fieldoffsets and padding). Unfortunately I don’t know one ☹

[Previous page](https://discourse.julialang.org/t/mutable-struct-vs-closure/20233.md?page=3)
