Hacker News new | ask | show | jobs
by cooljacob204 605 days ago
Do nifs have the equal process time stuff that regular elixir processes have? Where the BEAM will move the scheduler into another process if it's taking too long?

Forgive me if I'm mixing up my terminology it's been a bit since I have poked at Elixir.

3 comments

You can write nifs that way but it seems like a pain in the ass

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/...

BEAM can't preempt native code, that's why NIFs should either be fast/low-latency to not excessively block the scheduler or be put in what's called a dirty scheduler which just means to run it in a separate thread.
Nope, at least not by default or like one would expect from pure Erlang (when it comes to preempting). Been a while since I dug into this admittedly but I write Elixir daily for work (and have for about ten years now). They don’t do the record keeping necessary for the BEAM to interrupt. You need to make sure the “dirty scheduler” is enabled or you can end up blocking other processes on the same scheduler.

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...