How is Array's Synthesized Equatable conformance implemented?

When Swift synthesizes Equatable conformance for an Array property, does it perform some sort of "fast-check" to see if the underlying storage of the array is the same or does it always perform an equality test on each element in the arrays?

let founders = ["Alice", "Dave", "Jane", "John"]

struct Organization: Equatable {
  let name:String
  let members:[String]
}

let org1 = Organization(name:"Org 1", members:founders)
let org2 = Organization(name:"Org 2", members:founders)

// Does this equality test check every element in `members`?
if org1 == org2 {
  // ...
}

Bonus question: Where in the Swift codebase would I look to see how something like synthesized Equatable conformance is implemented for array properties?

It does a fast check:

let x = Double.nan
var a = [x]
var b = a
a[0] == b[0]        // false
a == b              // true
a[0] = b[0]
a == b              // false
3 Likes

It’s definitely somewhere in https://github.com/apple/swift.

Maybe you can start here:

1 Like
Terms of Service

Privacy Policy

Cookie Policy