Performance tips for Union Types

As I’ve summed up from the scattered discourse posts, the Julia compiler has been optimised for the small Union Types.
Yet, it seems, there is no information about it in the Julia docs (I’ve searched in the Performance Tips section).

As I understand, output Union Types break type stability, yet it is the mechanism used in the new iteration protocol and it clearly contradicts with what is written in Performance Tips.
Looks like the docs need updating.

It would be great to know what are the limitations of this optimisation:
How small should be Union Type?
Is it applicable to an Abstract Type, if it has small number of leaves?
Can you use Union Types in arrays?
When you should and should not use them?

Relevant blog post:


Great. Thank you!
By the way, does it work if there are more than two types in a Union? All the examples that I’ve seen use only Unions of two types.

That blog post is super helpful, and I hadn’t seen it yet. I wonder if it would be useful to have a blog-announce discourse category where people can post when they write a julia-related blog post?



Yes it works with more than two types, but there’s various limits in various parts of the compiler that’ll kick in if you go much beyond that (limits to the number of union elements, number of applicable methods considered, maximum union splitting branching factor, etc.). As a rule of thumb, I’d say around 3 or 4 union elements will probably always stay under all these limits, but it depends on the specific case and which part you care about (e.g. the storage optimization is effective up to 255 elements I believe).


It seems some of these limitations have been lifted with #37378 . Do I understand this right ?