Contents

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:

1
2
3
4
5
let myString = "Hello"
var myString2 = myString			// myString is copied to myString2
myString2.append(" World!")

print("\(myString) - \(myString2)")	// prints "Hello - Hello World!"

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var array1 = [1, 2, 3, 4]

address(of: array1)		// 0x60000006e420

var array2 = array1

address(of: array2)		// 0x60000006e420

array1.append(2)

address(of: array1)		// 0x6080000a88a0
address(of: array2)		// 0x60000006e420

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:

1
2
3
4
func address(of object: UnsafeRawPointer) -> String {
	let addr = Int(bitPattern: object)
	return String(format: "%p", addr)
}

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:

1
2
3
struct User {
	var identifier = 1
}

We must start creating a class, with a generic property T, which wraps our value type:

1
2
3
4
5
6
final class Ref<T> {
	var value: T
	init(value: T) {
		self.value = value
	}
}

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
struct Box<T> {
	private var ref: Ref<T>
	init(value: T) {
		ref = Ref(value: value)
	}

	var value: T {
		get { return ref.value }
		set {
			guard isKnownUniquelyReferenced(&ref) else {
				ref = Ref(value: newValue)
				return
			}
			ref.value = newValue
		}
	}
}

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:

1
2
3
4
guard isKnownUniquelyReferenced(&ref) else {
	ref = Ref(value: newValue)
	return
}

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
final class Ref<T> {
	var value: T
	init(value: T) {
		self.value = value
	}
}

struct Box<T> {
	private var ref: Ref<T>
	init(value: T) {
		ref = Ref(value: value)
	}

	var value: T {
		get { return ref.value }
		set {
			guard isKnownUniquelyReferenced(&ref) else {
				ref = Ref(value: newValue)
				return
			}
			ref.value = newValue
		}
	}
}

We can use this wrapping like this:

1
2
3
4
5
6
let user = User()

let box = Box(value: user)
var box2 = box					// box2 shares instance of box.ref

box2.value.identifier = 2		// Creates new object for box2.ref

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.