[erlang-questions] Could NIFs keep pointers to Erlang terms without copying them?

Jean Rougé rouge.j@REDACTED
Wed Feb 8 00:41:02 CET 2017


Hi,

If NIFs want to keep an erlang term around, they currently have to copy
them to their own, process-independent environment, using enif_make_copy.

That can be a pretty big performance issue if dabbling with big terms:
imagine for example a NIF storing a complicated data structure, e.g. a
gb_tree; now say we want to keep adding/deleting items from that gb_tree in
our erlang code: we now have to copy the entire thing back and forth.

How hard/conceivable would it be to give NIFs a way to keep a pointer to an
erlang term that they could pass back and forth with the process'
environments without having to copy everything?

I imagine that would mean that it would be the NIF's programmer
responsibility to:

   - ensure that they never keep a pointer to an erlang term that's no
   longer referenced in erlang land (and hence is going to get garbage
   collected)
   - ensure thread-safety

But the erlang VM would still have to somehow let the NIF know when an
erlang term it has a pointer to has been moved somewhere else.

Is that something that could maybe, in theory, be possible to add to the
current VM?

Thanks a lot,

Jean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170207/aa57bbe2/attachment.htm>


More information about the erlang-questions mailing list