Why are structs faster than classes?

Avinash Kaul
3 min readMay 24, 2024

--

Structs are generally faster than classes in Swift due to their different memory allocation methods.

Detailed Explanation

Structs in Swift

Structs in Swift are value types. This means that when you create an instance of a struct, it is stored directly in the stack.

Stack Storage: The stack is a contiguous block of memory with a simple, predictable structure. When a function or method call creates a local variable, it is pushed onto the stack. When the function returns, the variable is popped off the stack.

Performance Benefits: Accessing memory on the stack is very fast due to its LIFO (last-in, first-out) nature, which simplifies memory allocation and deallocation. The predictability of the stack’s structure enhances cache performance, leading to quicker read/write operations.

Classes in Swift

Classes in Swift are reference types. Instances of classes are allocated on the heap, a larger pool of memory used for dynamic memory allocation.

Heap Storage: The heap is managed by the system and can become fragmented over time. Allocating memory on the heap involves more overhead because the system must find a suitable block of memory to store the object.

Indirect Access: When you work with a class instance, you are manipulating a reference to a location in the heap. This additional layer of indirection (pointer dereferencing) introduces overhead, making access slower compared to direct stack access.

Garbage Collection: In Swift, memory for class instances is managed via Automatic Reference Counting (ARC). While ARC is efficient, it introduces additional overhead compared to the stack, as it keeps track of the reference counts and performs cleanup when references are no longer needed.

Stack vs. Heap Performance

Stack Advantages:

Speed: The stack’s predictable structure makes memory access faster and more efficient.

Cache Efficiency: Contiguous memory allocation on the stack improves cache performance.

No Need for Garbage Collection: Stack memory is automatically managed, eliminating the overhead associated with ARC or garbage collection.

Heap Disadvantages:

Slower Access: The heap’s complexity and the need for pointer dereferencing slow down memory access.

Fragmentation: Over time, the heap can become fragmented, leading to inefficient memory use and additional overhead in memory allocation.

ARC Overhead: While ARC simplifies memory management, it adds runtime overhead that can affect performance.

Practical Implications for iOS Developers

- Use Structs for Lightweight Data: For data models that are simple and do not require reference semantics, prefer structs. They provide faster access and lower overhead.

- Use Classes for Complex Objects: When you need reference semantics, such as shared mutable state, use classes. Be aware of the performance trade-offs and manage ARC efficiently.

- Optimize Memory Usage: Understanding where and how memory is allocated can help you write more efficient code. Profile your app to identify performance bottlenecks related to memory management.

In conclusion, knowing when to use structs versus classes in Swift can significantly impact your iOS app’s performance. Structs, being stored on the stack, offer faster memory access and lower overhead, making them suitable for lightweight, immutable data. Classes, on the other hand, provide flexibility through reference semantics but come with additional performance costs associated with heap storage and ARC.

Follow me on LinkedIn

--

--