Swift: Struct vs Class Performance

Tram Ho

During iOS application development, you have probably faced a situation where you need to choose between Class and Struct. How can you decide between them? We need to consider before choosing:

  1. We need a type of reference value (Reference Types) or a type of value (Value Types)
  2. Do we need to support inheritance? Most of us need to consider the above 2 options when choosing Struct and Class. But what about their performance? You must consider the following issues:
  3. Which ones in Struct and Class work faster?
  4. Which one makes more memory optimization?
  5. Which one is fast? In this article, I will mainly focus on the performance and memory management of Struct and Class.

Performance and Memory management between Struct and Class:

When it comes to performance, we always have to consider the following facts: Value Types are faster than Reference Types because the value is stored in the Stack and the reference is stored in Heap. So the storage in the Heap takes more time. Also when storing the reference type, there is additional responsibility for managing the “retain count” of the object. So, based on the above theory, we can say that Structs are faster than Class because:

  1. To store in the class, Apple first finds the memory in the Heap, then maintains an additional field to count the number of “Retain count”. The same goes for storing the Heap’s reference on the Stack. So when it comes to access, it has to deal with the stack and the heap. See an example image from the following Apple video:

So if you look at the image, stored in Class we have:

  • Stack for storing heap references
  • Heap for storing class variables x and y
  • Add a field to maintain the amount kept for that class.

So you can understand that in this case if you want to access class variables, it has to find both stack and heap stuff.

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

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

  • No heap is used when using struct
  • Assigning another stack variable makes another copy

So based on the above, we can say that the Struct always executes the class fast. But wait if that’s the case:

  • Why do we need classes if they are slower than struct?
  • Is struct always faster than struct?

Yes, there is a situation where struct can be too costly for you. Consider a case in which your structure contains multiple reference type variables, for example String. So we have a problem here:

  • Swift always stores reference types in the heap, so all reference types will be stored in the heap.
  • Structured behavior, if we pass struct with 8–10 methods and classes, what happens? Well, if we talk about structured behavior, it will generate a lot of heap allocation, as assigning struct will create a copy. But can the apple really do this? The answer is no. Here we come up with a ** Copy on Write ** approach. * Let’s see what it is. *

Copy On Write:

In situations where we have too many reference types stored in the struct, Apple only makes a copy of the struct when we modify some of the structure’s properties. So, let’s understand by example:

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

According to Apple, they only create copies if we modify some of the object’s properties. This is to optimize wasted memory, so only replicate it all only if any object is modifying.

The above image is from Apple’s WWDC video, where they explicitly mention that if we just assign structure variables, it will keep pointing to the same heap until any modifications are not made.

So the decision between a Struct and a Class can be made by looking at the facts above. If the Struct has multiple reference types and we need to change multiple objects then the class can be useful in this case. But again it depends on the case.

More in Depth:

If you don’t get a chance to watch Apple’s WWDC video, then I’ll add a screenshot for one more example, how we can improve performance in our app. The idea is to avoid creating multiple instances of reference type:

If we look at the above example:

There is a map, image of which the key is a String. So if you take an example if a table view is for each cell we need to download OR get it from cache.So in many cases we keep calling the function “makeBalloon” to continue generating the String key. So now you have to know how much resource it takes to create your reference type.

So Apple recommends that we avoid these cases as much as possible to improve application performance.”

The above example can be solved in the following way:

Instead of using String as a key, we now have Attributes as keys. So creating struct variables is not a time consuming process. So this will greatly improve performance .”

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

Source: https://medium.com/macoclock/swift-struct-vs-class-performance-29b7be73d9fd

Share the news now

Source : Viblo