Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Compile Times - Should the `final` keyword increase or decrease compile time? [duplicate]

Tags:

swift

Except for obvious reasons, such as if by design I do not want certain method, or property, or whatever to be overridden down the inheritance tree, are there other reasons to mark things final in Swift?

For example, are there performance considerations? I recall reading somewhere on SO answers that suggest something along the line.

like image 999
0x416e746f6e Avatar asked Nov 20 '22 08:11

0x416e746f6e


1 Answers

From Apple's Swift blog: Increasing Performance by Reducing Dynamic Dispatch

Swift allows a class to override methods and properties declared in its superclasses. This means that the program has to determine at runtime which method or property is being referred to and then perform an indirect call or indirect access. This technique, called dynamic dispatch, increases language expressivity at the cost of a constant amount of runtime overhead for each indirect usage.

Using final is one of several ways to improve performance by eliminating such dynamism:

The "final" keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection.

like image 156
Eric Aya Avatar answered Jan 09 '23 03:01

Eric Aya