When to inline a function in Elixir

前端 未结 1 1550
长情又很酷
长情又很酷 2021-02-19 13:02

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}

1条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 13:42

    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.

    0 讨论(0)
提交回复
热议问题