作为我们阅读小组的一部分,我们最近正在阅读BEAM 书。第 7 章提到了 Erlang 中的 ref 技巧/同步调用技巧。
不幸的是,这本书是不完整的,经过讨论,我们无法弄清楚参考技巧是什么。
在执行 时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.