On preserving term sharing in the Erlang virtual machine

In programming language implementations, one of the most important design decisions concerns the underlying representation of terms. In functional languages with immutable terms, the runtime system can choose to preserve sharing of subterms or destroy sharing and expand terms to their flattened representation during certain key operations. Both options have pros and cons. The implementation of Erlang in the Erlang/OTP system from Ericsson has so far opted for an implementation where sharing of subterms is not preserved when terms are copied (e.g., when sent from one process to another or when used as arguments in spawns). In this paper we describe our experiences and argue through examples why flattening terms during copying is not a good idea for a language like Erlang. More importantly, we propose a sharing-preserving copying mechanism for Erlang/OTP and describe a publicly available complete implementation of this mechanism. Performance results show that, even in extreme cases where no subterms are shared, this implementation has a reasonable overhead which is negligible in practice. In cases where shared subterms do exist, perhaps accidentally, the performance savings can be substantial.