Computed properties that return a constant are *more* performant than stored properties
I had a hunch that the compiler should almost certainly optimize away a computed property that returns a constant.
To make a fairer comparison for cross-module compilation situations (like in a large real world app), consider the assembly generated from just this snippet (no main function, to simulate exposing this from a Framework):
public enum Constants {
public static var computedProperty: Int { 3 }
public static let storedProperty: Int = 3
}
Compiling with -Osize (optimize for executable size), we get the following relevant assembly:
static output.Constants.computedProperty.getter : Swift.Int:
mov w0, #3
ret
output.Constants.storedProperty.unsafeMutableAddressor : Swift.Int:
adrp x0, (static output.Constants.storedProperty : Swift.Int)
add x0, x0, :lo12:(static output.Constants.storedProperty : Swift.Int)
ret
The computed property is actually smaller/faster/more optimized! Just a single mov instruction, rather than loading a value from memory.