Copy On Write (COW) with SWIFT

What is Copy On Write?

Marthin Satrya Pasaribu
4 min readMar 5, 2020

Copy on Write (CoW or COW), sometimes referred to as implicit sharing or shadowing is a resource-management technique used in computer programming to efficiently implement a “duplicate” or “copy” operation on modifiable resources. If a resource duplicated but not modified it is not necessary to create a new resource. The resource can be shared between the copy and the original.
Source: Wikipedia

Copy on write is a common computing technique that helps increase performance when copying structures. The example you have an array with having 100 elements, and you need to copy that array into a variable, Swift has to copy all elements even that array never modified.

That problem is solved by copy on write.

Hey, Array is Value Type, how if I modified something on array after copying the data??

Let’s take an example:

I have an array of Integer we call it arrayOfNumber and then I copy that array into copyOfArray . You know array is value type and it means when you copy that array will be given a copy, but on this case, because right now the data still same it means both arrays pointing to the same data. It looks like contradict what is value type meaning right?

But don’t worry, Swift is smart 😎

Source: https://media0.giphy.com/media/HGFgvdeDA4ePK/source.gif

So, if you look at an example above, when copyOfArray assigned with arrayOfNumberthe value will be pointing to the same value. But when we trying to modify copyOfArray Swift takes a full copy at that point so that only the second variable is modified — the arrayOfNumber will not change.

That’s how Swift can ensure that no wasted work is done when we trying to copy array or dictionaries but it never changes.

Important Notes

  • Copy on Write is a feature specially added to Swift arrays and dictionaries.
  • COW enables value types to be referenced when they are copied just like reference types. The real copy only happens when you trying to modify that copy.

Cause it’s only happening in Arrays and Dictionaries and How I can implement it in my own struct?

If we have a struct with a lot of information, we may need a copy-on-write to improve the performance of our App and to avoid useless copies. For this reason, we have to create this behavior manually.

Let’s consider a struct Profile which we want to use copy-on-write:

and we will create a class with generic property T, which will wrap our value Type, in this case, it can Profile class. We are using class here because when we assign a reference type to another one to the second variables will share the same instance, instead of copying it like the value type:

We will create a struct to wrap a Ref :

isKnownUniquelyReferenced: Returns a Boolean value indicating whether the given object is known to have a single strong reference.

Since struct is a value type, when we assign it to another variable its value is copied, whereas the instance of the property ref remains shared by the two copies since it’s a reference type.

On this case, because of isKnownUniquelyReference the second Box variable does not share the same ref instance anymore.

Let’s took an example like below:

If you see an example above:

  • Line number 39: We instance profile object
  • Line number 41:: We instance first box we call it box
  • Line number 42: We copy box into box2
  • Line number 43 & 44: We trying to print box and box2 value, the result is:
  • Line number 46: We try to create a new object of box2 with changing the value, in this case, we have validation isKnowUniquelyReferenced which is to check whether the given object is known to have a single strong reference or not.

We will try to print after the value was change:

  • Line Number 48 will print value.name of box2 which is copy of box.
  • Line Number 49 will print original value.name of box

This is the result:

References:

--

--