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?
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).
I created the package MixedStructTypes.jl exactly to improve the performance of code which requires to work with unions of many types if someone is interested