concept NIFs in category erlang

This is an excerpt from Manning's book Erlang and OTP in Action.
NIFs are a new feature in Erlang. Although ports remain the simplest and safest solution for communicating with foreign programs, sometimes you want to make something like a new built-in function with minimal overhead. With NIFs, you define a function in C in such a way that it becomes available as an Erlang function, just like a BIF. This is similar to the way foreign function interfaces work in most other languages—for example, Java and Python—so it probably looks more familiar if you have experience with such languages. Don’t let that make you take the NIF approach as your first choice—consider the consequences of getting a hard system crash with no other clues than a core dump. If you do use NIFs, you may want to run your most important systems on nodes separate from the ones that run the NIF code, at least until the code has been sufficiently tested in battle.
NIFs also have another fundamental problem: the native function runs in the context of the VM thread that calls it, and the thread can’t be rescheduled until the NIF has returned control to the VM. This makes NIFs suitable only for functions that execute and return quickly; long-running NIFs hold up resources for the Erlang VM scheduler.
To summarize, there are three low level mechanisms for interfacing between Erlang and other languages: plain ports, linked-in port drivers, and NIFs. All are valuable for different reasons: plain ports are a safe and easy way to hook up an external program over standard I/O, linked-in port drivers offer greater speed at the price of safety, and NIFs are an efficient but perilous way of hooking in library functions. To integrate the JSON parsing library with the Simple Cache, you’ll start by making a plain port interface.
When you implement the JSON parser interface as NIFs, you’ll no longer need a gen_server to hold an open port—all the functionality will be provided directly from the module json_parser. There’ll be no need to start the application before it can be used, and there’s no supervision tree. Thus, you can get rid of the jp_app, jp_sup, and jp_server modules. The application will be a simple library application, and you can simplify the file ebin/json_parser.app to the following:
{application, json_parser, [{description, "JSON parser (using NIFs)"}, {vsn, "0.1.0"}, {modules, [json_parser]}, {applications, [kernel, stdlib]} ]}.
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.