Skip to content
Commitwise
Menu

Copy-on-write

Also called: CoW

Sharing data between copies until one of them is changed, and only then making a real copy.

Definition

Copy-on-write (CoW) lets value types hold large data cheaply. Assigning the value copies only a reference to a shared buffer; the first mutation checks whether the buffer is shared and, if it is, copies it before writing. Swift's Array, Dictionary, Set and String work this way. Extra references to the same buffer make every mutation copy, which can turn a loop of appends into quadratic work.

In practice

var copy = items followed by items.append(x) copies the whole array once, because the buffer is shared with copy at the moment of the append.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.