[OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path - #703
Open
inju2403 wants to merge 1 commit into
Open
[OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path#703inju2403 wants to merge 1 commit into
inju2403 wants to merge 1 commit into
Conversation
…key path apple#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.
Member
|
The failing check is due to a CI issue; #706 will eventually resolve it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OrderedDictionary.replaceElement(at:withKey:value:)overwrites the value atindexin both of its paths. #688 switched one of them toexchange(_:with:); this switches the other, so both move the old value out instead of copying it. Behavior-preserving; no public API change.Motivation
#688 factored the general case onto
OrderedSet._replaceNew(at:with:in:), and in review adoptedexchange(_:with:)for the value overwrite that the same commit had just rewritten. The equal-key path above it wasn't part of that diff — #688 noted it was left unchanged — so it still does the read-then-assign it has always done.Going back over
replaceElementandreplace(at:with:)now that #688 has landed, this is the one place left in the function where the old value is copied out before being overwritten, rather than moved out. The two paths do the same thing to_values[index]a dozen lines apart; they should be written the same way: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) }Nothing else moves: the key still goes through
_keys.update(key, at: index), the hash table is still left untouched on this path, andreplaceElement's preconditions, returned element, and complexity (expected amortized O(1)) are unchanged. No public API is added, changed, or removed.Testing
Behavior-preserving change, so no new tests are added. This path is already covered by
test_replaceElement_sameKeyin bothOrderedDictionary Tests.swiftandOrderedDictionary+Elements Tests.swift— added alongside the equal-key path itself — covering sizes 1 through 19 with every valid index position, both unique and shared (copy-on-write) storage viawithHiddenCopies, lifetime tracking, and the returned old key and value. All 261OrderedCollectionsTestspass with and without-Xswiftc -DCOLLECTIONS_INTERNAL_CHECKS.Checklist