# Overhead of dynamic dispatch?

**URL:** https://discourse.julialang.org/t/overhead-of-dynamic-dispatch/7089
**Category:** General Usage
**Tags:** question
**Created:** [November 15, 2017, 6:19pm UTC](https://discourse.julialang.org/t/overhead-of-dynamic-dispatch/7089 "2017-11-15T18:19:00Z")
**Posts on this page:** 1
**Showing post:** 5

<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: [November 15, 2017, 8:01pm UTC](https://discourse.julialang.org/t/overhead-of-dynamic-dispatch/7089/5 "2017-11-15T20:01:14Z")

</div>

There are a number of downsides to encoding a property of your thing in the type system:

- There will be more time spent in compilation — every time a different type hits a different function, Julia will compile a new specialized version just for that type.
- If this property is typically determined by a run-time value, then many functions working with your things will be type-unstable. Type-unstable variables can stymie optimizations, cause allocations and hit dynamic dispatch.
- Collections of many things with different properties are type-unstable when you try to pull out and work with individual elements.
- You cannot mutate that property in-place.

On the plus column are effectively the flip-sides of _exactly the same things_:

- Julia will compile a new specialized version just for that type. If this property drastically changes how you work with your things, this can be a huge gain.
- If the value of the property _changes_ the return types of functions that work with your things, encoding it into the type system will make those functions type-stable. It actually can make things _more_ type-stable. This means that downstream analyses can do even better optimizations.
- Collections of many things with the same property can be highly optimized. That property value gets encoded once into the type of the array or collection (and not many times on each individual element), so it can more compactly represent the elements.
- You cannot mutate that property in-place.

How these things interplay will depend on what exactly you’re doing. Note that “adding types” doesn’t inherently add overhead. It can also _remove_ overhead.

---

_[View the full topic](https://discourse.julialang.org/t/overhead-of-dynamic-dispatch/7089)._
