cigrainger

cigrainger

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.

Showing Posts 1 to 8

josevalim

josevalim

Creator of Elixir

As far as I know it is not possible to call Elixir/Erlang functions from C/Rust code.

cigrainger

cigrainger OP

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.

amnu3387

amnu3387

Not sure how helpful this is, but if you can link C libs and you want to serialise stuff, there’s Erlang Interface / docs

You can then from C encode terms:

void respond_with_window(osea_CTX *osea_ctx) {
  char msg[2], finish[2];
  ei_x_buff buf;
  ei_x_new_with_version(&buf);

  msg[0] = RSP_WINDOW;
  finish[0] = '\n';
  ei_x_format_wo_ver(
	      &buf,
	      "{~l,~i,~i,~l,~i}",
	      osea_ctx->egl_native_window_type,
	      osea_ctx->osea_win.width,
	      osea_ctx->osea_win.height,
	      osea_ctx->toolbar.height,
	      osea_ctx->toolbar.visible
	      );


  write(1, msg, 1);
  write(1, buf.buff, buf.buffsz);
  write(1, finish, 1);
  ei_x_free(&buf);
};

Which encodes a tuple with five elements, {long/unsigned, int, int, long/u, int}.
And from the Elixir/Erlang side call binary_to_term(Rem), which decodes it into a normal tuple.

tessi

tessi

Hi,

I am pretty sure it’s possible to call from a NIF into elixir because I’m doing just that in wasmex :wink:

Someone asked about how I do that in a GitHub issue where I explained the why’s and how’s in a little more detail:
https://github.com/tessi/wasmex/issues/256#issuecomment-848339952

Feel free to ask away if there is any questions :slight_smile:

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

tessi

tessi

Thanks @akash-akya

your points are very valid. The whole thing is a big dance for just calling into Elixir. But it’s possible. I think in Rust, the thread handling part is a little more safe, but still deadlocks are possible. (we could build in timeouts, though, so that things eventually return).

And yes, it’s not super efficient. It could be much more efficient and more easy to build if we settle for async calls (message sends) only.

cigrainger

cigrainger OP

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.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews