Inlining and function boundaries

I read that inlining, by removing function boundaries, may cause type instability. Is this true? This doesn’t seem too hard to fix, but I’m not familiar with Julia internals.

Only if the call site is already type-unstable. If you have code where the types of variables are correctly inferred, then inlining function calls on these type-inferred variables is fine.

If you have a call to f(x) at a call site where x is not type-inferred (e.g. x is type-unstable), then the call f(x) must dynamically dispatch to the correct compiled version of f depending on the type of x, but thereafter can be fast because it runs a type-specialized version of f. In this case, inlining f may worsen performance because it removes the type-specialization for particular types of x.

In practice, however, you would typically only want to inline relatively cheap functions anyway, and in this case you would need the call-site to be type-stable to avoid the dynamic-dispatch overhead even if you weren’t inlining.

So I don’t think this is a big problem — the focus should be on making the call-site type-stable, not on optimizing inlining of type-unstable calls.

9 Likes