I have two small notes:
First, the bitwise equality of two objects does not necessarily imply that they are identical. An object's identity sometimes includes the location of its bits in addition to their value. For example, this is the case for noncopyable synchronization constructs like struct Atomic
and struct Mutex
-- two distinct atomic integers are never identical, even if at some point they both happen to have the same integer value, i.e., their storage consists of the same bits. Some Swift types are not bitwise comparable.
(This can also go in the other way. Two semantically identical Swift values can sometimes differ in their bit patterns due to padding bytes whose values are unspecified and can vary. In general, raw bitwise equality can be used neither to deny nor confirm that two Swift objects are identical.)
Second, regarding asymptotic complexity of comparing instances using memcmp
: each concrete type in Swift has a constant memory layout, known at runtime. To decide whether the bits of two instances of the same type are the same, in the worst case (bitwise equality) we need to access each bit in both objects. This requires accessing/comparing ~2 * MemoryLayout.size bytes, which gives us a constant upper bound. The time complexity of bitwise instance comparisons in Swift is O(1). This is true for every concrete Swift type, from Bool
to InlineArray<1_000_000_000, Int>
.