by ihumanable · 2y ago · 0 comments
The BEAM VM (which is the thing that runs erlang / elixir / gleam / etc) has 3 flavors of functions.
- BIFs - Built-in functions, these are written in C and ship with the VM
- NIFs - Natively implemented functions, these are written in any language that can speak the NIF ABI that BEAM exposes and allows you to provide a function that looks like a built-in function but that you build yourself.
- User - User functions are written in the language that's running on BEAM, so if you write a function in erlang or elixir, that's a user function.
NIFs allow you to drop down into a lower level language and extend the VM. Originally most NIFs were written in C, but now a lot more languages have built out nice facilities for writing NIFs. Rust has Rustler and Zig now has Zigler, although people have been writing zig nifs for a while without zigler and I'm sure people wrote rust nifs without rustler.
The creator of Zigler has a talk from ElixirConf 2021 on how he made Zig NIFs behave nicely:
Discord is a big Erlang + Rustler user.
It's one less potential cause that might bring down the entire Erlang VM.
With data structures that have some definite behavior unless someone finds a defect there isn’t going to be much activity.
Yes, it's Apache 2.0
There's another option and that's setting up an Erlang node in the other language. The Erlang term format is relatively straightforward. But I'm honestly not sure of the benefit of a node versus just using a port.
- can "easily" send beam terms back and forth
- if you want it to be os-supervised separately (systemd, kubernetes, e.g.)
- pain in the ass
Port:
- easy
- usually the only choice if you're not the software author
- really only communicates via stdio bytestreams
- risk of zombies if... Iirc the stdout is not closed properly?
- kind of crazy how it works, Erlang VM spawns a separate process as a middleman
It's not impossibly large but it's not something one does on a lark either; if there isn't support in your language already it's hard to justify this over any of the many, many message busses supported by both Erlang and other languages that don't have so many requirements.
If you've got some mathematical/crypto function, chances are you don't want that to go through a command queue to an external port, because that's too much overhead. If it's a many round crypto function like bcrypt or something, you do need to be a bit careful doing it as a NIF because of runtime. But you wouldn't want to put a sha256 through an external program and have to pass all that data to it, etc.
Something that you might actually want queueing for and is likely to have potential for memory unsafety like say transcoding with ffmpeg, would be a good fit as an external Port rather than a NIF or a linked in Port driver.
We do have IPC handles that could enable this over, say, ports, but then there's a whole other discussion on pointers vs ipc handles
Forgive me if I'm mixing up my terminology it's been a bit since I have poked at Elixir.
https://www.erlang.org/doc/apps/erts/erl_nif#enif_schedule_n...
After all, many of the BIFs have been replaced internally by NIFs
And there's this, which would scare me:
https://erlang.org/documentation/doc-15.0-rc3/erts-15.0/doc/...
Here’s a link I found talking about using the dirty scheduler with Rust(ler): https://bgmarx.com/2018/08/15/using-dirty-schedulers-with-ru...