From 0ccbcec380bc069fe0a90ba28c34c69516ba61f9 Mon Sep 17 00:00:00 2001 From: Rick van Voorden Date: Mon, 23 Jun 2025 16:38:13 -0700 Subject: [PATCH 1/2] [stdlib] array identical --- stdlib/public/core/Array.swift | 30 ++++++++++++++++++++++++ stdlib/public/core/ArraySlice.swift | 30 ++++++++++++++++++++++++ stdlib/public/core/ContiguousArray.swift | 20 ++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/stdlib/public/core/Array.swift b/stdlib/public/core/Array.swift index 46066791213e0..b6a5369577a70 100644 --- a/stdlib/public/core/Array.swift +++ b/stdlib/public/core/Array.swift @@ -2136,3 +2136,33 @@ internal struct _ArrayAnyHashableBox } extension Array: @unchecked Sendable where Element: Sendable { } + +extension Array { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @backDeployed(before: SwiftStdlib 6.3) + public func isIdentical(to other: Self) -> Bool { + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false + } +} diff --git a/stdlib/public/core/ArraySlice.swift b/stdlib/public/core/ArraySlice.swift index 9b278648dbc28..66662eed94d85 100644 --- a/stdlib/public/core/ArraySlice.swift +++ b/stdlib/public/core/ArraySlice.swift @@ -1586,3 +1586,33 @@ extension ArraySlice { } } #endif + +extension ArraySlice { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @backDeployed(before: SwiftStdlib 6.3) + public func isIdentical(to other: Self) -> Bool { + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false + } +} diff --git a/stdlib/public/core/ContiguousArray.swift b/stdlib/public/core/ContiguousArray.swift index 19b5daf8e9193..31a5e9e38e7fc 100644 --- a/stdlib/public/core/ContiguousArray.swift +++ b/stdlib/public/core/ContiguousArray.swift @@ -1495,3 +1495,23 @@ extension ContiguousArray { extension ContiguousArray: @unchecked Sendable where Element: Sendable { } + +extension ContiguousArray { + /// Returns a boolean value indicating whether this array is identical to + /// `other`. + /// + /// Two array values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing arrays this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// array storage object. Therefore, identical arrays are guaranteed to + /// compare equal with `==`, but not all equal arrays are considered + /// identical. + /// + /// - Performance: O(1) + @backDeployed(before: SwiftStdlib 6.3) + public func isIdentical(to other: Self) -> Bool { + self._buffer.identity == other._buffer.identity + } +} From 8fa9379b6e32ad56af420e9124306ab518f3cc6e Mon Sep 17 00:00:00 2001 From: Rick van Voorden Date: Tue, 24 Jun 2025 17:16:04 -0700 Subject: [PATCH 2/2] [stdlib] array identical aeic --- stdlib/public/core/Array.swift | 2 +- stdlib/public/core/ArraySlice.swift | 2 +- stdlib/public/core/ContiguousArray.swift | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/stdlib/public/core/Array.swift b/stdlib/public/core/Array.swift index b6a5369577a70..dbb71e14c311c 100644 --- a/stdlib/public/core/Array.swift +++ b/stdlib/public/core/Array.swift @@ -2151,7 +2151,7 @@ extension Array { /// identical. /// /// - Performance: O(1) - @backDeployed(before: SwiftStdlib 6.3) + @_alwaysEmitIntoClient public func isIdentical(to other: Self) -> Bool { let lhsCount = self.count if lhsCount != other.count { diff --git a/stdlib/public/core/ArraySlice.swift b/stdlib/public/core/ArraySlice.swift index 66662eed94d85..e3f2b43d3ffda 100644 --- a/stdlib/public/core/ArraySlice.swift +++ b/stdlib/public/core/ArraySlice.swift @@ -1601,7 +1601,7 @@ extension ArraySlice { /// identical. /// /// - Performance: O(1) - @backDeployed(before: SwiftStdlib 6.3) + @_alwaysEmitIntoClient public func isIdentical(to other: Self) -> Bool { let lhsCount = self.count if lhsCount != other.count { diff --git a/stdlib/public/core/ContiguousArray.swift b/stdlib/public/core/ContiguousArray.swift index 31a5e9e38e7fc..41a4a4e37771a 100644 --- a/stdlib/public/core/ContiguousArray.swift +++ b/stdlib/public/core/ContiguousArray.swift @@ -1510,8 +1510,18 @@ extension ContiguousArray { /// identical. /// /// - Performance: O(1) - @backDeployed(before: SwiftStdlib 6.3) + @_alwaysEmitIntoClient public func isIdentical(to other: Self) -> Bool { - self._buffer.identity == other._buffer.identity + let lhsCount = self.count + if lhsCount != other.count { + return false + } + + // Test referential equality. + if unsafe lhsCount == 0 || self._buffer.identity == other._buffer.identity { + return true + } + + return false } }