Swift: Struct Vs Class Performance

Mohit Kumar
Mac O’Clock
Published in
5 min readJun 19, 2020

While doing iOS development, you must have faced a situation where you need to select between Class Vs Struct. How did you decide to choose between those? Well, we take some consideration while choosing:

  1. Do we need value type OR reference type?
  2. Do we need to support inheritance?

Most of us take the above 2 considerations while choosing between Struct and Class. But what about the performance of them? Did you consider the following facts too:

  1. Which one in Struct and Class is faster?
  2. Which is more optimized in memory?
  3. Which is Faster?

Well in this blog I will be majorly focusing on performance and memory management of Struct and Class.

Performance and Memory management between Struct and Class:

When it comes to performance, we should always consider facts that:

Value type is faster than reference type because they store in Stack and reference type store in Heap. So storing in Heap if more time consuming process. Also while storing reference type, we have one more burden to manage retain count of that object.

So based on the above theory we can say that Struct is faster than Class because:

  1. To store class, Apple first finds memory in Heap, then maintain the extra field for RETAIN count. Also, store reference of Heap into Stack. So when it comes to access part, it has to process stack and heap. See below example image from Apple video:

So if you look at Image, to store a Class we have:

  • Stack to store heap reference
  • Heap to store class variable x and y.
  • One extra field to maintain retain count for that class.

So you can take the fact that in this case if you want to access class variables, it has to look stack and heap both.

2. Now if we talk about Struct, It just uses Stack to store values, so while reading value we just read directly from the stack in O(1) time. Look below example using Stack where we need to store x and y.

So if you look above example, this is the same as class one but here we don’t need any Heap. So 2 points here:

  • No heap is used while using struct
  • Assigning to another stack variable create another copy

So based on the above thing, we can say that Struct always performs fast then class. But wait if that is the case:

  • Why do we need class if they slow then struct?
  • Is struct always fast then class?

Well, there is a situation where struct can be too costly for you. Consider a case where your struct contains lots of variables which need to store in heap, eg String (it’s value type but apple internal implementation store in heap) So we have an issue here:

  • Swift always store reference type or String type variable into the heap.
  • Struct behavior, if we pass struct within 8–10 methods and classes, what will happen? Well if we talk about struct behavior it should create so many heap allocations, since assigning struct will create a copy. But does the apple really do like this? The answer is NO. Here we come to an approach Copy on Write. Let’s look at what it is.

Copy On Write:

In a situation where we have so many reference types store into the struct, Apple creates a copy of struct only when we modify some property of struct. So let’s understand by example:

So if we look at the above example, we assigned one object of Employee to another object. So do you think there is a copy created into memory? Well, the answer is a big NO here.

As per apple, they create copy only if we modify some property of employee2 object. This is to optimize memory to be wasted, so just copy all slots only when any object is modifying.

The above image is from the Apple WWDC video, where they clearly mention that if we just assign struct variables, it keeps pointing to the same heap until any modification is not made.

So the decision between Struct and Class can be taken by considering the above facts. If Struct has lots of reference types and we need to mutate lots of objects then the class may be helpful in this case. But again it depends on a case by case.

More in Depth:

If you haven’t got a chance to look apple WWDC video, I am adding a screenshot for one more example, how we can improve performance in our app. Idea is to avoid multiple instances creation of variable which store in heap.

If we look at the above example:

It has an image map where the key is a string. So if you take an example if a table view where for each cell we either need to download OR get from the cache. So whatever is the case, but we keep calling make balloon function which keeps creating string key. So now you must be aware of how costly is to create reference type or a variable which store in heap.

So Apple suggested that we should avoid these cases as much is possible to improve app performance.

Above example can be solved in the following way:

Instead of String as key, Now we has a struct Attributes as key. So creating struct variable is not time consuming process. So this will help to improve performance a lot.

This is the end of this article, so based on the above facts we should always be very careful while choosing between Reference Type and Value Type.

--

--