[erlang-questions] What is the best way to parse complex erlang terms in port drivers?

Samuel <>
Thu Jun 7 22:44:00 CEST 2012

> It seems like the eterm interface is the older/slower interface, and the ei interface is the newer/faster interface (where you have better control over memory usage).  That is just based on experience, and is unlikely to be found in the documentation.

A better way to see it is that the ETERM abstraction used by
erl_interface builds on top of ei. ei contains functions to decode and
encode the erlang binary format, but how to represent the data in your
program is up to you, ei will only provide pointers and integers. Also
you have to manage the memory yourself (well, there is a little aid
called ei_x_buff, but still quite low level).

ETERM buils on that so that you get c terms representing lists, tuples
and the rest of non-recursive types. That comes with the tradeoff of
letting erl_interface manage the memory for you which may or may not
work depending on your needs. It is good because it saves time and is
supposed to prevent errors, but is bad because you loose flexibility
and optimisation opportunities.

Using ei directly gives you more freedom, but requires more coding usually.

In my experience, erl_interface works well for fairly simple ports,
but is a bit difficult to keep memory and performance under control as
soon as you start using more complex data types.

In any case, if you end up managing fairly complex data types, the
serialisation/deserialisation code is going to be a pain, no matter
which option you use. When I was working for Interoud we implemented a
code generator that given type specifications generated code to
serialise and deserialise erlang terms from/to C types in a fairly
compact format. You can use the same idea using either library to
support it if you don't want to use your own serialisation algorithm,
but in this case I'm pretty sure that the best option would be ei.
There are also more sophisticated ways of doing it as UBF [1,2] and
other frameworks.

Also, I remember reading at some point that erl_interface was
deprecated, but I cannot find the reference now. The only thing that I
see is that the code for erl_interface lives in a directory called
legacy ...

[1] http://www.sics.se/~joe/ubf/site/home.html
[2] https://github.com/norton/ubf


More information about the erlang-questions mailing list