This statement is very misleading if not flat out wrong. Type inference problems have nothing to do with function specialization. Function specialization will always occur. Type inference allows the compiler to know in advance which specializations to choose for a function, but even if the concrete types are not known in advance, it will still specialize using the very slow dynamic dispatch. Please try this all out with @which
, @code_llvm
and @code_warntype
to see this in action.
When types are not inferred, there are a bunch of branches with boxed
variables, and what it’s doing is finding what the type of the variable currently is in order to choose the right dispatch. This is the part that is slow when types are not inferred, and is all “compiled away” when a function is type-stable (you can see it becomes a lot of checks!).
So your logic was in the wrong direction. Julia isn’t magic, and in the end it needs to know the concrete types to call your function with, just like it’s a C-function. But if it cannot “read the mind of the programmer”, i.e. if the program which is making the call is not type-inferrable, then it will type-check until it’s sure of the type, and afterwards dynamically (at run-time) search the methods table for that specialized function and use it. If you are calling lots of small functions (like +
), this can become a very large cost.