concept link - in driver in category erlang

This is an excerpt from Manning's book Erlang and OTP in Action.
The other kind of port uses linked-in drivers, often also called port drivers. As the name implies, a linked-in driver is a shared library, usually written in C, that’s loaded and linked dynamically with the Erlang VM. This has the benefit of faster communication; but as with NIFs, a linked-in driver has the potential to crash or corrupt the entire Erlang VM. Communication with a linked-in driver is still byte-oriented, as with plain ports; and from the point of view of your Erlang code, both kinds of ports look the same. This means it’s easy to go from a plain port to a linked-in driver when the need arises.
Figure 12.2. Linked-in drivers live in the same operating system process space as the Erlang VM, using a C API with callback functions and buffers for transferring the data.
![]()
Keep in mind that when you use a linked-in driver, you’re trading safety for speed. You should make this trade-off only when you’re sure you need the additional speed for your system. See chapter 14 for a discussion about profiling and optimization.
Creating a linked-in driver should almost never be the first step you take when interfacing with foreign code. But at this point, you already have an implementation of a port connected to an external program that has been proven to be stable—it just doesn’t seem to be efficient enough. It’s time to consider moving to a linked-in driver.
The parser logic of your C code is already in working order, and you don’t need to change it. You just need to take that logic and wrap it up in such a way that Erlang can link it in, start it, and talk to it directly. This means you’ll be replacing the main function with a callback function that conforms to the erl_driver API, and creating some additional support structures and functions that are needed for linked-in drivers. But first, let’s look at what makes linked-in driver code different from code in external programs.