Overhead of dynamic dispatch?

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.

14 Likes