cigrainger

cigrainger

Calling Elixir in Rust?

Hey everybody! I’m delving into Rustler to enable some work on data frames in Elixir backed by polars. I’m feeling pretty confident in the mental model of using a nif resource and delegating to native functions for much of the API. One place I’m struggling to get my head around is arbitrary elixir functions on data in the nif resource. The python bindings for polars enable passing lambdas across that are then applied on the polars dataframe (see here). Does Rustler enable something or is there a preferred interop path for this sort of thing? It feels like a pretty heavy approach to serialise, apply, then deserialise back.

Most Liked

akash-akya

akash-akya

Hi @tessi, thanks for taking time to write this approach. I also played around same idea in the past (in C though).

Just want to highlight the risks involved if someone is considering

  • since mutex does not compose well, its easy to end-up with deadlocks. Especially if the callback is not internal function but something given by the user of the library (consider a callback calling another NIF).
  • its tricky to handle errors, “let-it-crash” does not work well with this, since we have to unblock the thread no matter what happens to the callback.
  • forces process at erlang/elixir side and thread at NIF side. And most likely we need thread pooling at NIF side
  • needless to say its not very efficient if you are calling callback often - due to all the context-switching & synchronizations involved.

I think there were few more but not able to recall at the moment.

Another reference: [erlang-questions] Calling Erlang functions from NIFs

cigrainger

cigrainger

Thanks! Wow, @tessi that is very impressive work. Your explanation of the ResourceArc is really helpful for me and I’d love to see clearer explanations like that in the official documentation. That’s really where I’m hurting the most. Definitely simplified in 0.22.0-rc though.

Probably not terribly useful for what I’m working on to call into Elixir that way, but seeing how you did it is extremely enlightening.

cigrainger

cigrainger

Thanks José! I suppose I’ll cross that bridge when I get there then. Won’t hurt to start with serialise/apply/deserialise. I’m not sure if pyo3’s approach of embedding a Python interpreter in the binary is particularly reasonable anyway. I may be imagining bottlenecks in copying before I’ve proved the problem anyway.

Last Post!

ityonemo

ityonemo

Wow that’s super impressive. It’s giving me ideas though, I think there might be a clever and safe way of doing this in zigler by yielding first, and storing the frame, then resuming once the result term has come back. If the owning process is killed, the resource can be freed and the yield will exit, triggering cleanup of allocated memory, etc.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement