From 9d866eb2101f66f1f626234fad75458fa38544ac Mon Sep 17 00:00:00 2001 From: Rick van Voorden Date: Mon, 23 Jun 2025 16:50:25 -0700 Subject: [PATCH 1/2] [stdlib] dictionary identical --- stdlib/public/core/Dictionary.swift | 40 +++++++++++++++++++++++++++++ stdlib/public/core/Set.swift | 39 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/stdlib/public/core/Dictionary.swift b/stdlib/public/core/Dictionary.swift index 6e867aedec522..e058f1d4b4a56 100644 --- a/stdlib/public/core/Dictionary.swift +++ b/stdlib/public/core/Dictionary.swift @@ -2154,3 +2154,43 @@ extension Dictionary.Index: @unchecked Sendable where Key: Sendable, Value: Sendable {} extension Dictionary.Iterator: @unchecked Sendable where Key: Sendable, Value: Sendable {} + +extension Dictionary { + /// Returns a boolean value indicating whether this dictionary is identical to + /// `other`. + /// + /// Two dictionary values are identical if there is no way to distinguish + /// between them. + /// + /// Comparing dictionaries this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying + /// dictionary storage object. Therefore, identical dictionaries are + /// guaranteed to compare equal with `==`, but not all equal dictionaries are + /// considered identical. + /// + /// - Performance: O(1) + @backDeployed(before: SwiftStdlib 6.3) + public func isIdentical(to other: Self) -> Bool { +#if _runtime(_ObjC) + if + self._variant.isNative, + other._variant.isNative, + unsafe (self._variant.asNative._storage === other._variant.asNative._storage) + { + return true + } + if + !self._variant.isNative, + !other._variant.isNative, + self._variant.asCocoa.object === other._variant.asCocoa.object + { + return true + } +#else + if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) { + return true + } +#endif + return false + } +} diff --git a/stdlib/public/core/Set.swift b/stdlib/public/core/Set.swift index fa79ca2858d0a..95868b400c141 100644 --- a/stdlib/public/core/Set.swift +++ b/stdlib/public/core/Set.swift @@ -1658,3 +1658,42 @@ extension Set.Index: @unchecked Sendable where Element: Sendable { } extension Set.Iterator: @unchecked Sendable where Element: Sendable { } + +extension Set { + /// Returns a boolean value indicating whether this set is identical to + /// `other`. + /// + /// Two set values are identical if there is no way to distinguish between + /// them. + /// + /// Comparing sets this way includes comparing (normally) hidden + /// implementation details such as the memory location of any underlying set + /// storage object. Therefore, identical sets are guaranteed to compare equal + /// with `==`, but not all equal sets are considered identical. + /// + /// - Performance: O(1) + @backDeployed(before: SwiftStdlib 6.3) + public func isIdentical(to other: Self) -> Bool { +#if _runtime(_ObjC) + if + self._variant.isNative, + other._variant.isNative, + unsafe (self._variant.asNative._storage === other._variant.asNative._storage) + { + return true + } + if + !self._variant.isNative, + !other._variant.isNative, + self._variant.asCocoa.object === other._variant.asCocoa.object + { + return true + } +#else + if unsafe (self._variant.asNative._storage === other._variant.asNative._storage) { + return true + } +#endif + return false + } +} From e395baae7237a66f04bd4e83c9d5dab417237dba Mon Sep 17 00:00:00 2001 From: Rick van Voorden Date: Tue, 24 Jun 2025 19:23:44 -0700 Subject: [PATCH 2/2] [stdlib] dictionary identical aeic --- stdlib/public/core/Dictionary.swift | 2 +- stdlib/public/core/Set.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/public/core/Dictionary.swift b/stdlib/public/core/Dictionary.swift index e058f1d4b4a56..ef67c73bb9b14 100644 --- a/stdlib/public/core/Dictionary.swift +++ b/stdlib/public/core/Dictionary.swift @@ -2169,7 +2169,7 @@ extension Dictionary { /// considered identical. /// /// - Performance: O(1) - @backDeployed(before: SwiftStdlib 6.3) + @_alwaysEmitIntoClient public func isIdentical(to other: Self) -> Bool { #if _runtime(_ObjC) if diff --git a/stdlib/public/core/Set.swift b/stdlib/public/core/Set.swift index 95868b400c141..90ff27f5d0443 100644 --- a/stdlib/public/core/Set.swift +++ b/stdlib/public/core/Set.swift @@ -1672,7 +1672,7 @@ extension Set { /// with `==`, but not all equal sets are considered identical. /// /// - Performance: O(1) - @backDeployed(before: SwiftStdlib 6.3) + @_alwaysEmitIntoClient public func isIdentical(to other: Self) -> Bool { #if _runtime(_ObjC) if