So the question is rather simple, what exactly does inlining do and when should it be used in Elixir?
By inlining I mean this: @compile {:inline, myfun: 1}
P.S. I know that there's info on the subject in the Erlang documentation here but I am still not sure I understand.
Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function.
inline functions might make it faster: As shown above, procedural integration might remove a bunch of unnecessary instructions, which might make things run faster. inline functions might make it slower: Too much inlining might cause code bloat, which might cause “thrashing” on demand-paged virtual-memory systems.
Inline function instruct compiler to insert complete body of the function wherever that function got used in code. Advantages :- 1) It does not require function calling overhead. 2) It also save overhead of variables push/pop on the stack, while function calling. 3) It also save overhead of return call from a function.
Unless your "CPU meter" is pegged at 100%, inline functions probably won't make your system faster. (Even in CPU-bound systems, inline will help only when used within the bottleneck itself, and the bottleneck is typically in only a small percentage of the code.)
When you inline a function, its calls will be replaced with the function body itself at compile time. This can be used to squeeze the last bit of performance out of a specific function call, eliminating the overhead of one single function call. Unfortunately, it will also make stack traces harder to read because the original function does effectively not exist in the compiled code. So when you use inlining, you should be really confident that the inlined function is bullet-proof, otherwise you will make your code a lot harder to debug.
I would really not bother with inlining unless you have a simple function that is called all the time. Take a look at the Elixir source code to get a feeling in which cases inlining is used – you will find basic functions that operate on lists, maps etc. and will probably be called very frequently. Note that even inside the Elixir source code, inlining is used very sparingly because you will only benefit from it in some rare cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With