# Performance: mutable vs immutable structs

**URL:** <https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221>\
**Category:** Performance\
**Tags:** performance, memory, memory-allocation\
**Created:** [October 3, 2017, 3:53pm UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221 "2017-10-03T15:53:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tryingtosolve](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tryingtosolve](https://discourse.julialang.org/u/tryingtosolve)\
**Post date:** [October 3, 2017, 3:53pm UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221/1 "2017-10-03T15:53:42Z")

</div>

In my code, I need to create a struct to hold two vectors. In terms of speed and memory allocations, would it make a difference if I make it mutable instead of immutable, assuming that there’s no need to make to mutable?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 3, 2017, 5:46pm UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221/2 "2017-10-03T17:46:39Z")

</div>

Presumably you are _trying to profile_ it? 😉

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 3, 2017, 7:19pm UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221/3 "2017-10-03T19:19:47Z")

</div>

Using `immutable`s seems to make aliasing analysis slightly easier for the compiler, see some analysis in e.g. [https://github.com/JuliaLang/julia/pull/16371#issuecomment-219644197](https://github.com/JuliaLang/julia/pull/16371#issuecomment-219644197).

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 3, 2017, 10:03pm UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221/4 "2017-10-03T22:03:19Z")

</div>

I don’t know all the details, but in general I would expect to see a much bigger difference between _bits_ types and non-bits types than immutables vs. mutables. A bits type is a primitive or an immutable struct whose fields are all bits types. You can test that with `isbits()`. Notably, bits types can be stack-allocated, so you can construct them at will with little to no speed or allocation penalty. Here’s a simple demo:

```julia
julia> mutable struct MyMutable
         x::Vector{Int}
       end

julia> struct MyImmutable
         x::Vector{Int}
       end

julia> using StaticArrays: SVector

julia> struct MyBits
         x::SVector{2, Int}
       end

```

`Vector` is not a bits type, so both `MyMutable` and `MyImmutable` are not bits types. But `StaticArrays.SVector{2, Int}` is a bits type (because it’s immutable and contains only a tuple of two integers), so `MyBits` is also a bits type. This has a significant performance impact in code that constructs a lot of objects:

```julia
julia> using BenchmarkTools

julia> function f_mutable()
         for i in 1:1000
           x = MyMutable([rand(Int), rand(Int)])
       end
       end
f_mutable (generic function with 1 method)

julia> function f_immutable()
         for i in 1:1000
           x = MyImmutable([rand(Int), rand(Int)])
       end
       end
f_immutable (generic function with 1 method)

julia> function f_bits()
         for i in 1:1000
           x = MyBits(SVector(rand(Int), rand(Int)))
         end
       end
f_bits (generic function with 1 method)

julia> @btime f_mutable()
  44.277 μs (3000 allocations: 125.00 KiB)

julia> @btime f_immutable()
  44.220 μs (3000 allocations: 125.00 KiB)

julia> @btime f_bits()
  13.201 μs (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 4, 2017, 9:10am UTC](https://discourse.julialang.org/t/performance-mutable-vs-immutable-structs/6221/5 "2017-10-04T09:10:36Z")

</div>

True but the question was specifically about:

> [@tryingtosolve](#):
>
> In my code, I need to create a struct to hold two vectors.
