mat-hek

mat-hek

Membrane Core Team

Unifex - same code for NIFs and C nodes

Hello everyone,

pleased to announce Unifex, a tool that simplifies integrating elixir with C/C++. Unifex has been out for some time already but since v0.3 released today, it supports generating C nodes - lightweight programs acting as Erlang nodes. Though communication with NIFs is way faster than with separate nodes, a failing node doesn’t crash the entire VM, moreover, it can be supervised like a normal process. Unifex allows choosing between NIFs and C nodes at any point, even in runtime.

Approach

Here are some key facts about Unifex:

  • Native code is kept separate from elixir code
  • Native code doesn’t depend on erlang libraries, but on Unifex abstraction, that aims to be more user friendly than erlang APIs
  • Unifex works by generating boilerplate code in the compile-time, based on DSL in elixir
  • Unifex aims to support the intersection of sets of data types from Elixir and C
  • Unifex relies on Bundlex, our tool for compiling native code

Here’s how it looks like in short: firstly, you describe the interface with spec-like DSL:

module Example

interface [NIF, CNode]

spec foo(num :: int) :: {:ok :: label, answer :: int}

then you provide implementation:

#include "_generated/example.h"

UNIFEX_TERM foo(UnifexEnv* env, int num) {
  return foo_result_ok(env, num);
}

and finally, you use it either as NIF

defmodule Example do
  use Unifex.Loader
end
iex> Example.foo(10)
{:ok, 10}

or as CNode

iex> require Unifex.CNode
iex> {:ok, cnode} = Unifex.CNode.start_link(:example)
iex> Unifex.CNode.call(cnode, :foo, [10])
{:ok, 10}

A detailed description of how to place that in a Mix project can be found here.

Use cases

We’re using Unifex heavily at Membrane, mainly for wrapping some complex, stateful libs, and that’s where Unifex suits the most IMHO.

Status

Though we’ve been using Unifex for NIFs for some time, adding C nodes required lots of changes that are quite fresh. We’re validating them while developing Membrane ICE plugin.

Feedback and contributions are most welcome :wink:

Credits: @Feliks, @mickel8, @bblaszkow06, @mkaput, Software Mansion

Most Liked

mat-hek

mat-hek

Membrane Core Team

Unifex v0.4.0 released :tada:

This version adds the ability to define custom types and use them in Unifex specs. For now, enums and structs are supported. Declaring them generates C enums and C structs, respectively, for example:

type position :: :manager | :developer | :intern | :product_owner

type personal_data :: %PersonalData{
  age: int,
  expirience: int,
  name: string
}

will generate

enum Position_t { MANAGER, DEVELOPER, INTERN, PRODUCT_OWNER };
typedef enum Position_t Position;

struct personal_data_t {
  int age;
  int expirience;
  char *name;
};
typedef struct personal_data_t personal_data;

and can be used like

spec get_salary(person :: personal_data, person_position :: position) ::
       {:ok :: label, salary :: int}
       | {:error :: label, :unemployed :: label}

Docs, GitHub

Credits go to @Feliks, thanks!

Where Next?

Popular in Libraries Top

Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
Qqwy
Solution is a library to help you with working with ok/error-tuples in case and with-expressions by exposing special matching macros, as ...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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

Sub Categories:

We're in Beta

About us Mission Statement