Use Copy-On-Write With Swift Value Types
Premature optimisation is the root of all evil. But, there are moments where we need to optimise our code. Let’s see how to improve the performance of value type in Swift.
Introduction
A variable in Swift can be either reference or value type.
The most basic distinguishing feature of a value type is that copying — the effect of assignment, initialization, and argument passing — creates an independent instance with its own unique copy of its data
In this article, I explain an optimisation of this copying: the copy-on-write.
What Is Copy-On-Write?
Every time we assign a value type to another one, we have a copy of the original object:
|
|
If we copy just a plain String we may not have problems with the performance.
We may start having some troubles when we have Arrays with thousands of elements, and we copy them around our app. For this reason, the Array has a different way to copy, which is called copy-on-write.
When we assign an Array to another one, we don’t have a copy. The two Arrays share the same instance. In this way, we don’t have two different copies of a big Array and we can use the same instance, with a better performance. Then, just when one of the two Arrays change we have a copy.
Let’s see an example:
|
|
We can notice, in this example, that the two Arrays share the same address until one of them changes. In this way, we can assign array1
to other variables several times without copying every time the whole array, but just sharing the same instance until one of them changes. This is very useful for the performance of our App.
For your info, this is the function used to dump the address:
|
|
Unfortunately, not all the value types have this behaviour. This means that, 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 behaviour manually.
Manual Copy-On-Write
Let’s consider a struct User
in which we want to use copy-on-write:
|
|
We must start creating a class, with a generic property T
, which wraps our value type:
|
|
We use class
—which is a reference type—because when we assign a reference type to another one the two variables will share the same instance, instead of copying it like the value type.
Then, we can create a struct
to wrap Ref
:
|
|
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.
Then, the first time we change value
of one the two Box
variables, we create a new instance of ref
thanks to:
|
|
In this way the two Box
variable don’t share the same ref
instance anymore.
isKnownUniquelyReferenced returns a boolean indicating whether the given object is known to have a single strong reference.
Here the whole code:
|
|
We can use this wrapping like this:
|
|
Conclusions
An alternative to this approach is using an Array—instead of Box
—to wrap the value type to copy on write. Unfortunately, the approach with the Array has some disadvantages. You can find more details in the Apple’s optimisation tips.