在 Erlang 进程中使用映射而不是记录来保存状态有什么优势吗?

Are there any advantages using maps instead of records to hold state in Erlang processes?

我看到的大多数书籍或在线资源都使用记录来保存进程的状态(可能是因为这种方式已经存在了超过(?)十多年)。另一方面,映射可有效地用于替换 stdlib 中的元组(例如 childspecs in the supervisor module)。

例如,我正在学习 Learn You Some Erlang's Finite State Machines 章节,state 记录可以用地图替换,在 init/1 回调中声明 [=12] =].

还有,这样会不会更有效率?
我知道一个经过深思熟虑的基准测试可以回答我的问题,但我只花了几个星期就开始学习 Erlang 和 maps module is fairly new and still changing.

更新:感谢我给出了糟糕的建议,我更彻底地阅读了 LYSE chapter on maps 并且答案很明确:

Using records has the advantage that the keys are known at compile time that brings advantages of

  • fast access to specific values (faster than what is possible dynamically)
  • additional safety (crash early rather than corrupting state)
  • easier type checking

These make records absolutely appropriate for a process' internal state, despite the occasional burden of writing a more verbose code_change function.

On the other hand, where Erlang users would use records to represent complex nested key/value data structures (oddly similar to objects in object-oriented languages) that would frequently cross module boundaries, maps will help a lot. Records were the wrong tool for that job.

作为乔·阿姆斯特朗 said:

Records are dead - long live maps !

We’ve been talking about maps for over twelve years, but now they are here to stay.

Why the long wait? - we wanted maps to be a replacement for records and to be as efficient as records, and its not blindingly obvious how to do so.

所以,看起来地图没问题,我们已经将我们的项目从记录切换到地图,我们没有感觉到任何性能损失。

一个限制: 如果我是对的,您不能像使用记录那样将地图存储在 mnesia 中。

我在 Learn You Some Erlang 网站上添加了一章专门介绍地图:http://learnyousomeerlang.com/maps

Mexican Standoff 部分专门比较地图与记录和口述。从语义上讲,地图更类似于字典而不是记录,我的建议实际上是在记录有意义的地方使用记录(具有 O(1) 访问权限的已知类型的受限键集),以及使用字典的地图(异构、灵活的 key/value 对集)。