Comparison
Copy-on-writevsDeep copy vs shallow copy
Copy-on-write
the copy was free until somebody wrote to it, and only then did the data actually get duplicated.
Sharing one underlying representation between a value and its copies until one of them is modified, at which point that one is duplicated. It gives value semantics at reference-semantics cost for the common read-only case, and it is how several standard library collections and most process forks work. Its performance cliff is the write nobody expected to be expensive, which is the same shape as every other lazy optimisation.
Full entry →Deep copy vs shallow copy
you copied the object, edited the copy's list, and the original's list changed too.
A shallow copy duplicates the object and shares whatever its fields point at; a deep copy duplicates the reachable graph as well. Almost every default copy in every language is shallow, which is why the surprise is so common. Deep copies are expensive, must decide what to do about cycles and shared identity, and are usually a sign that immutable values would serve better.
Full entry →