erlang/otp — pull request #494 by nickie (2014-10-06 09:48:41+00:00)
Title: Sharing-preserving copying of terms

This set of commits implements the sharing-preserving copying of terms between process heaps, e.g. when sending messages and when spawning new processes. Until now, terms are flattened when copied, e.g. the term `[L, L]` where `L = [1,2,3]` is expanded to `[[1,2,3], [1,2,3]]` when copied. This is a safe but incomplete implementation of sharing-preserving copying: off-heap pointers in terms (e.g. constants) are copied verbatim. A new BIF is also added, `erts_debug:size_shared/1`, which computes the (non flat) size of a term, i.e. the space that the term occupies in the process's memory.

Kostis Sagonas and I have been discussing this pull request with Kenneth Lundin for a long time. The main idea is described in a paper presented at the Erlang Workshop in 2012 ([DOI 10.1145/2364489.2364493](http://dx.doi.org/10.1145/2364489.2364493)). Several of the commits are there for debugging and testing purposes.

As agreed, this sharing-preserving behavior is by default disabled, unless the configure flag `--enable-sharing-preserving` is specified.