Re:Copying data is wasteful, mutating data is dangerous
by jerf on Thursday January 09, 2020 @02:04PM
(#22004319)
"You could imagine some hypothetical language in which all objects are immutable, as far as the programmer is concerned - however the compiler is allowed to reuse objects behind the scenes if there is provably no way that a side-effect could be introduced."
There are functional languages that do this. Like many other optimizations, you get less than your intuition might naturally expect, but it does work.
A trivial case is "tail call optimization", which most immediately involves not creating a literal in-memory stack record for recursive invocations but also can easily involve the compiler re-using the memory for the arguments. IIRC Erlang will do that as much as it can in tight recursive calls.
There are functional languages that do this. Like many other optimizations, you get less than your intuition might naturally expect, but it does work.
A trivial case is "tail call optimization", which most immediately involves not creating a literal in-memory stack record for recursive invocations but also can easily involve the compiler re-using the memory for the arguments. IIRC Erlang will do that as much as it can in tight recursive calls.