# (Compiler) Performance of Dict

**URL:** https://discourse.julialang.org/t/compiler-performance-of-dict/27986
**Category:** General Usage
**Created:** [August 26, 2019, 6:32am UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986 "2019-08-26T06:32:49Z")
**Posts on this page:** 5
**Page:** 2

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 28, 2019, 1:31pm UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986/21 "2019-08-28T13:31:34Z")

</div>

10 posts were split to a new topic: [Code isolation side discussion](https://discourse.julialang.org/t/code-isolation-side-discussion/28108)

---

<div class="post-metadata">

### Author: ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)
#### Post date: [August 28, 2019, 10:10am UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986/31 "2019-08-28T10:10:32Z")

</div>

Perhaps code isolation should be it’s own thread.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 28, 2019, 1:31pm UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986/32 "2019-08-28T13:31:51Z")

</div>

Done.

---

<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: [August 28, 2019, 2:06pm UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986/33 "2019-08-28T14:06:21Z")

</div>

> [@pablosanjose](#):
>
> If type piracy is disallowed, can one still cause invalidations?

Yes. Julia performs an optimization in the case that there’s only one (or a couple, I think) methods to possibly call in the face of a type-instability, it’ll avoid doing the dynamic dispatch. Example:

```Julia
julia> f(x) = 1
f (generic function with 1 method)

julia> g(x) = f(x[])*2
g (generic function with 1 method)

julia> @time g(Ref{Any}(1))
  0.007295 seconds (3.41 k allocations: 216.416 KiB)
2

julia> @time g(Ref{Any}(1))
  0.000015 seconds (5 allocations: 176 bytes)
2

```

The outer function `g` doesn’t know what it’ll get out of a `Ref{Any}`, so that’s a type instability, but it knows that there’s only one possible method for `f`, so it can avoid the expensive consequence of a type instability — the dynamic dispatch. Even better, it’s “fixed” our type instability because it knows that `f` will only ever return the number 1!

```julia
julia> @code_typed g(Ref{Any}(1))
CodeInfo(
1 ─ Base.getfield(x, :x)::Any
└── return 2
) => Int64

```

So this is now compiled and happily marching along. Then some package adds another method — note that the argument could be a type it defines itself; definitely not piracy. It’s a method that never gets called, and yet it forces the recompilation of `g`… and of course causes our previous example to become somewhat type-unstable again. Add enough other methods and eventually Julia will give up on the small unions and just return `Any`:

```julia
julia> struct TT end

julia> f(::TT) = 3//2
f (generic function with 2 methods)

julia> @time g(Ref{Any}(1))
  0.048927 seconds (137.27 k allocations: 7.199 MiB)
2

julia> @time g(Ref{Any}(1))
  0.000025 seconds (5 allocations: 176 bytes)
2

julia> @code_typed g(Ref{Any}(1))
CodeInfo(
1 ── %1 = Base.getfield(x, :x)::Any
... # so much code ...
18 ┄ %38 = φ (#5 => %15, #16 => %33)::Union{Rational{Int64}, Int64}
└─── return %38
) => Union{Rational{Int64}, Int64}

```

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [August 28, 2019, 2:19pm UTC](https://discourse.julialang.org/t/compiler-performance-of-dict/27986/34 "2019-08-28T14:19:16Z")

</div>

That’s really informative @mbauman, many thanks. Also, it’s really important to learn this type of compiler details to develop a sense of where performance opportunities lie. For me the compiler is still a magic black box… A box of black magic, actually :-D.

[Previous page](https://discourse.julialang.org/t/compiler-performance-of-dict/27986.md?page=1)
