Union Splitting Guarantees

I have some low level questions about union splitting

1.) If even one type in the union is abstract will union splitting not kick in? Or will there be branches created for at least the concrete types in the union. For example Union{String, Real}

2.) Is there a set limit (Julia 1.8) to the number of branches that the compiler will consider?

3.) If there is a set limit will the compiler generate as many branches as it can and then use runtime dispatch in the last branch for those values that do not get picked up?

4.) If there is a set limit is there a way to configure it?

1 Like

5.) What is happening if the dispatch concerns two parameters ?

1 Like

Yes.

No.

Union splitting is an optimization technique to make (somewhat) badly inferred code not having to do dynamic dispatch for small unions. There are almost no semantic guarantees surrounding it because the exact cutoff depends on heuristics and how much the extra/newly generated code impacts runtime, compiletime and inference time.

If you’re doing things close to that limit, chances are there’s either a better architecture to use to prevent the Unions or (in the rare case that there isn’t) you’ll have to live with it.

3 Likes