Does elixir support mmap?

Packages | Hex currently shows nothing.

Does elixir have bindings to mmap , or is it rejected because shared memory basically goes against Erlang’s philosophy of shared nothing / message passing style ?

No. Elixir does not work with mmapping directly. The reason for this is indeed that memory is managed by Erlang’s garbage collector. Memory you use for your Elixir datastructures is usually allocated on a process-specific stack/heap. This also means that garbage collection can be done independently for each processes (so the VM does not need to ‘stop-the-world’).

For most situations this is good enough. Only for certain kinds of compute-heavy code, it might sometimes turn into a bit of a bottleneck.
In these situations, you might write a NIF (‘natively implemented function’) in a language like Rust, Zig or C, that can be called from Elixir/Erlang. Be sure to benchmark to check whether you really need this. It usually is not worth the extra mental complexity.

Why are you wondering about mmap specifically?

6 Likes
4 Likes

I have a udp/tcp key-value cache written in Rust.

The only down side is that I am not 100% certain the Rust code never panics; and if a single thread/task panics, it might poison a lock/mutex, in which case the entire program might as well as crash.

I am considering porting it to Elixir. I need mmap as the Rust version uses mmap.

You may want to just consider using ETS. It’s the built in key/val store in the BEAM VM. There are a variety of packages that wrap it (lru, memoize, cachex, etc).

Here’s the appropriate search:

After picking a package, the next step would be to write the ucp/tcp handling via a GenServer if you need this exposed to another service. If you’re only going to be talking to Elixir nodes, then there is no need for that either as nodes can communicate with each other with the standard message passing.

3 Likes

The easiest thing to do is to make the rust side into a CLI tool and access it via a port. If the rust side crashed you can relaunch it.

3 Likes