Saturday, April 10, 2010

Immutable objects are more efficient

Scala Creator Martin Odersky talks about immutability
http://www.artima.com/forums/flat.jsp?forum=276&thread=221233

[Stateful objects] used to be a shortcut for efficiency. You couldn't create new copies of objects because that was too expensive. But it actually has turned around, even for single-threaded code. If you change an object, modern garbage collectors have to do a thing called card marking, which is incredibly expensive. The more you change fields, particularly of old objects, your performance is going to suffer. So it turns out that it's actually better to have immutable objects.