What is the role of BIFs in map performance?

I have been looking at the OTP source a bit, and I was surprised to see how much of Erlang’s map functionality is implemented in C. This has led to me scratching my head a bit about where the line was drawn (between C and Erlang) when implementing maps, and the rationale behind it. As far as I can tell, there are 2 main locations defining map behavior:


So I think I have a few questions:

  1. Are BIFs essential to the performance characteristics of maps on the BEAM? If you simply wrote them as an Erlang/Elixir module would they be unusable and slow? Are there other benefits to structuring it this way?
  2. What is the role of shadowing the BIFs in the Erlang module? It seems like the shadowed functions just check types and then throw a nif_error. I can’t quite figure out the purpose of that.
  3. swizzle32??? Is that a hash function? Is it used anywhere besides the BEAM?
  1. All basic data structures in Erlang are implemented in C. You often need a minimal set of operations to work against those data structures and they need to be implemented in C too. So most of the functions in there are implemented in C as a requirement.

  2. I believe that in order to replace something as a NIF, you need to define a stub. The stub can either have an Erlang-based implementation but, when not possible, you can raise a nif error.

  3. Maps with over 32 elements use a hash function but I don’t recall from memory if swizzle32 is being used for that.


Thanks, José!

I guess what I expected to find was that these data structures would be rewritten in Erlang after they bootstrapped in C. The fact that they never did that made me wonder if the map operations would have poor performance.

No, the reason for that is that it is impossible to have them fully in the Erlang, as these are basic types handled by the VM. Just like you cannot implement addition purely in Erlang (well, at least without hacks like Peano numbers) you cannot implement basic type-related functions in the Erlang, as you need to talk directly to the VM. You can think of them like system calls in the OS.

1 Like