What does @inline mean?

I see a lot of “@inline function” in a package. What does it mean? According to https://subscription.packtpub.com/book/programming/9781785880919/4/ch04lvl1sec23/inlining, it is just a way to avoid the function call overhead? Therefore, it can be deleted without introducing any error?

Yes, it’s just a (potential) optimization, and deleting it should not change what the function does. See the official @inline documentation.

1 Like

Inline 101 also a good reference written by @aviatesk

2 Likes

note that what this probably means is the package is old and those @inlines should likely be removed. Julia is generally pretty good at figuring out when to inline functions now

5 Likes

If any function is called using an llvmcall, you’ll probably need @inline.
I do not trust the heuristics.

In other cases, it’s hard to get code to be type stable and avoid unnecessary heap allocations, but spamming @inline sometimes fixes it, and is probably more robust than finding a particular guilty function that may change with the Julia version.

But I think this is for a very particular style of code, using lots of recursion and building temporary objects, where you’re counting on the compiler to delete all your work.

Another example of the @inline heuristics having been bad – and it’s been maybe a year since I checked this one, so maybe my info is old! – is ForwardDiff. It adds @inline to everything, and these are essential for performance.
Because ForwardDiff doesn’t provide @fastmath specializations, @fastmath is often a hefty pessimization for ForwardDiff code.

All that said, my advice would be not to worry about adding @inline unless you already noticed some performance problem that you know can be fixed by it.

3 Likes