I'm under the impression that in Erlang when calling a function, Erlang will deep copy the whole argument from the caller to the callee, mostly told by Erlang Efficiency Guide. However, I do notice the guide as well as the original paper mainly focus on records but not lists. I tried to find a specification or something that explains how Erlang passes list arguments, but the efforts were in vain.
At this point, one of my colleagues came to me and told me Erlang passes list-arguments "by reference" just like C pointer and Golang slice, but according to my experiences, this seems not to be true. With no reliable knowledge on hand, I can't prove either my or his hypothesis.
I wonder if there is such a piece of document, paper, or specification where I can learn how Erlang handles list arguments? No doubt the more official the better, but actually even a blog or email can do.
CodePudding user response:
When calling a function, all data structures are passed by reference (the exception being "immediates" such as small integers and atoms, which are passed directly by value). For normal function calls, you never have to worry about overhead.
When spawning a process, however, any parameters to its initial function call need to be copied to the heap of the new process, just as if they had been messages sent to the process when it starts up. This way, it does not matter if the new process runs on the same machine or on a different machine over the network.
See The Beam Book for a deep dive into how the Beam virtual machine works: https://github.com/happi/theBeamBook