什么是BEAM中的同步呼叫技巧/参考技巧

时间:2020-08-23 12:10:02

标签: erlang elixir beam

我们最近正在阅读BEAM book,这是我们阅读小组的一部分。在Chapter 7中,提到了Erlang中的ref技巧/ Synchronous Call Trick。

不幸的是,这本书是不完整的,经过讨论我们无法确定参考技巧是什么。

1 个答案:

答案 0 :(得分:3)

执行receive时,BEAM VM扫描邮箱以查找第一个合适的(匹配)消息,如果找不到则阻止该过程。
“诀窍”是,由于新引用在创建之前就不可能存在于邮箱中,因此receive {Reference, Term}时无需扫描整个邮箱,而仅需从Reference开始扫描已创建。

这就是以下短语的意思:
The compiler recognizes code that uses a newly created reference (ref) in a receive (see [ref_trick_code]), and emits code to avoid the long inbox scan since the new ref can not already be in the inbox.