Improve iOS App Performance

Furkan Kaplan
Level Up Coding

--

Code quality depends on several factors like readability, accuracy, performance, etc. If you think that time is the most important thing in the world, performance cannot be an underestimated factor.

When you create your first Hello World project, everything works great and so smooth. While your dream project is getting bigger, you may suffer from huge compile time or app performance. Just dive into iOS ecosystem architecture.

Use Protocol Oriented Programming over Object Oriented Programming

If you review Standard Swift Library code, you can realize that protocols are being used frequently. Apple prefers Protocol Oriented Programming and advice to use it if you are building an inheritance relationship from scratch.

Read official document advicing POP by touching this link.

OOP paradigm solves the complexity problem in our apps by using inheritance and polymorphism is the most useful part itself. While the program is running, which is at runtime, it decides which parameter or method should be called. This decision process is called Dynamic Dispatch.

The screenshot above is a small example of OOP. We know that class B has an echo method with override keyword because it was already defined in superclass which is class A. And when we call the echo method in class B, the echo method overridden in class B is triggered, not class A.

Everything looks great, right? Actually no, because each process is done at runtime, like in the example above, slows down our execution time. Well, what is the solution?

POP which stands for Protocol Oriented Programmings is here. We have reduced runtime calculations with just a small modification. The POP usage is so familiar, isn’t it? It’s Apple’s heavily used delegation pattern in UIKit.

Use Static Dispatch

While reading of Apple documentation about Swift Standard Library, you can see that “use structs over classes” phase so much. Both struct and class are very same structures except that structs are value type, whereas classes are reference type. Looks that it’s just a small difference. Actually not that small!

As a result of that difference, if you create an object from a class, it’s allocated dynamically but struct objects are allocated statically. Just explain another way, objects created statically are created in compile time in Stack with a fixed size whereas objects created dynamically are created in runtime in Heap. Their sizes are calculated and allocated the required memory in runtime. Therefore methods called from class object works with dynamic dispatch, whereas struct object works with static dispatch as well.

In conclusion, use structs combined with protocols instead of heavy class inheritance.

**Update

Big thanks to Pham Hoang Le for his notice. We say that structs are allocated statically whereas classes are allocated dynamically. But what if a class contains a parameter which is a struct type? Is the struct parameter still allocated statically and created on Stack? Answer is no! Even though it’s a struct type, it’s container is a class so the parameter is allocated in Heap with dynamic dispatch.

Check Your Access Levels

Until the program is running, a method call and parameter access are not being determined for an object created from a class. Therefore when you click the run button in Xcode, the compiler compiles your code and determinates the memory allocation and relations between methods and parameter access, i.e polymorphism usage. And if the compiler finds that a method or parameter which is not accessed outside, signs it as final keywords.

If you know that a class is not a base of any class, you should add the final keyword to the class definition. While adding the final definition to a class, you add the final definition to all parameters and methods in the class.

Assume you have a class that should be overridden. Therefore this class cannot have the final keyword. So you can add private restriction to all parameters and methods which are not accessed in subclasses.

return 👨‍💻

--

--