If the Array is known to be empty, which way to better to return an empty array?

// if self is known to be empty, which way is better to
// return a empty array?
extension Array {
    func fooAAA() -> Self {
        self
    }

    func fooBBB() -> Self {
        []
    }
}

The generated assembly code is not the same: Compiler Explorer

they are exactly the same, and the compiler even knows that they are identical and can substitute them, see:

the compiler created the different versions in your example because you did not compile with -whole-module-optimization enabled, so the compiler did not know that there was no code in a different file that might have to call fooAAA on a non-empty Array.

5 Likes