From 7a67033ee22997949436763445b7cffcff3da36a Mon Sep 17 00:00:00 2001 From: inju2403 <56947879+inju2403@users.noreply.github.com> Date: Fri, 14 Aug 2026 09:19:43 +0900 Subject: [PATCH] [OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path #688 factored `replaceElement`'s general case onto `OrderedSet._replaceNew(at:with:in:)` and, per review feedback, switched the value overwrite there to `exchange(_:with:)`. The equal-key path above it was outside that diff, so it still reads the old value into a local and then assigns over it. Switch it to `exchange(_:with:)` as well, so both paths move the old value out instead of copying it. No behavior change: the returned element, the preconditions, and the expected amortized O(1) complexity are all unchanged. --- .../OrderedDictionary+Partial MutableCollection.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift index 2c07c148..6b6aed5c 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift @@ -83,8 +83,7 @@ extension OrderedDictionary { // in place. The hash bucket is unchanged, so no rehash is needed. if existingIndex == index { let oldKey = _keys.update(key, at: index) - let oldValue = _values[index] - _values[index] = value + let oldValue = exchange(&_values[index], with: value) _checkInvariants() return (oldKey, oldValue) }