DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

What is @frozen

In this post, we'll explore the @frozen attribute in Swift, a compiler directive that can be applied to enum declarations to optimize memory layout and improve performance. By marking an enum as @frozen, you can enable certain optimizations that can make your code more efficient.

What does @frozen do?

When you mark an enum or struct as @frozen, you're telling the Swift compiler that the enum's memory layout is fixed and will not change (is simply a promise that the public interface of these types will never change ). This allows the compiler to make certain assumptions about the enum that can lead to performance improvements.

Use Case for @frozen

One common use case for @frozen is when you have an enum with a fixed set of cases that you know will never change. By marking the enum as @frozen, you enable the compiler to optimize the memory layout of the enum and generate more efficient code.

Here are two examples of using @frozen with an enum:

@frozen
struct Point {
  var x: Double
  var y: Double
}

@frozen
enum Direction {
    case north
    case south
    case east
    case west
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Poin and Direction enum is marked as @frozen because we know that the set of cases will never change. This allows the compiler to optimize the memory layout of the enum and generate more efficient code when working with Direction instances.

Benefits of @frozen

By using the @frozen attribute, you can achieve the following benefits:

  • Improved Performance : The compiler can make certain assumptions about the enum that can lead to performance improvements, such as better memory layout and more efficient code generation.

  • Optimized Memory Layout : Marking an enum as @frozen allows the compiler to optimize the memory layout of the enum, reducing the size of instances and improving memory access patterns.

  • Predictable Behavior : By explicitly marking an enum as @frozen, you make it clear to other developers that the set of cases will not change, leading to more predictable behavior.

Example of Use Case

Here's an example of using @frozen with an enum that represents different types of shapes:

@frozen
enum Shape {
    case circle(radius: Double)
    case square(side: Double)
    case triangle(base: Double, height: Double)
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Shape enum is marked as @frozen because the set of cases is fixed and will not change. This allows the compiler to optimize the memory layout of Shape instances and generate more efficient code when working with shapes.

ABI Stability

The @frozen attribute is also important for maintaining ABI stability in Swift. By marking an enum as @frozen, you ensure that the memory layout of the enum is fixed and will not change in future versions of the library or framework. This is crucial for ensuring compatibility between different versions of Swift.

What is an Application Binary Interface?

An Application Binary Interface (ABI) defines how functions, data structures, and other elements are represented in binary form and interact with each other at runtime. ABI stability is important for ensuring that code compiled with different versions of a library or framework can work together without issues.

Caveats

While @frozen can provide performance benefits, there are a few caveats to keep in mind:

  • Immutable Set of Cases : Once you mark an enum as @frozen, you cannot add or remove cases from it. This can limit the flexibility of the enum if you need to make changes in the future.

  • Compatibility : The @frozen attribute is only available in Swift 5.1 and later, so if you need to maintain compatibility with older versions of Swift, you may not be able to use it.

  • Limited Use Cases : @frozen is most beneficial for enums with a fixed set of cases that will never change. If your enum is likely to evolve over time, using @frozen may not be appropriate.

Wrap up

In this post, we've explored the @frozen attribute in Swift and how it can be used to optimize the memory layout and improve the performance of enum declarations. By marking an enum as @frozen, you can enable certain optimizations that can make your code more efficient and predictable.

Resources:

Top comments (0)