-
Notifications
You must be signed in to change notification settings - Fork 362
OrderedDictionary: skip write when assigning identical Equatable value; add tests #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OrderedDictionary: skip write when assigning identical Equatable value; add tests #542
Conversation
|
Thanks for considering this contribution! |
| result._values.append(value) | ||
| // Existing key path | ||
| if let index = idx { | ||
| let old = _values[index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use a safe: subscript at this point? there's any potential risks here?
| import XCTest | ||
| @testable import OrderedCollections | ||
|
|
||
| final class OrderedDictionaryOptimizedUpdateTests: XCTestCase { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just wondering if this could be leverage using SwiftTesting
aluco100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
There are multiple problems here:
If you need to do this, it is possible to implement this operation outside of this package: extension OrderedDictionary where Value: Equatable {
mutating func updateValueIfDifferent(
_ value: Value, forKey key: Key
) -> (originalMember: Value?, replaced: Bool) {
guard let i = self.index(forKey: key) else {
self[key] = value
return (nil, true)
}
let old = self.values[i] // Note: copy
guard old != value else { return (old, false) }
self.values[i] = value
return (old, true)
}
}I recommend you do this, rather than adding such an operation directly to this package. Note that this variant avoids uniquing storage if the dictionary won't get changed, but it hashes the key twice if it doesn't already exist in the dictionary. (There may be a real API omission here -- I think we need an |
…e; add tests
Summary
Adds a small optimization for
OrderedDictionary.updateValue(_:forKey:)to skipredundant writes when the new value is equal to the existing value (of the same
dynamic type). This avoids unnecessary mutation and copy-on-write overhead.
Implementation
Equatableand compare equal,the function now returns early without performing a write.
Tests
Added
OrderedDictionaryOptimizedUpdateTestscovering:testOptimizedUpdate_NoWriteWhenEqualEquatable— verifies no redundant write.testOptimizedUpdate_InsertWhenMissing— verifies normal insertion behavior.All tests pass locally via: swift test
Rationale
This improves performance in hot update loops where reassignments of identical
values are common. No API or semantic changes; risk level is low.
Checklist