何时在Elixir中内联函数 [英] When to inline a function in Elixir

查看:66
本文介绍了何时在Elixir中内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以问题很简单,内联函数到底做了什么,何时应该在Elixir中使用它?

So the question is rather simple, what exactly does inlining do and when should it be used in Elixir?

通过内联,我的意思是: @compile {:inline,myfun:1}

By inlining I mean this: @compile {:inline, myfun: 1}

PS我知道Erlang文档此处中有关于此主题的信息,但我仍然不确定我是否理解。

P.S. I know that there's info on the subject in the Erlang documentation here but I am still not sure I understand.

推荐答案

内联函数时,在编译时,其调用将被函数主体本身替换。这可以用来减少特定函数调用的性能,从而消除了单个函数调用的开销。不幸的是,由于原始函数实际上不存在于编译后的代码中,因此这也会使堆栈跟踪更难以阅读。因此,当您使用内联时,您应该真的确信内联函数是防弹的,否则您的代码将很难调试。

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.

除非您有一个一直被称为 的简单函数,否则我真的不会打扰内联。 接受查看Elixir源代码以了解,在这种情况下,将使用内联–您会发现可对列表,地图等进行操作的基本函数,并且可能会被频繁调用。请注意,即使在Elixir源代码中,也很少使用内联,因为只有在极少数情况下您才能从中受益。

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.

这篇关于何时在Elixir中内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆