Skip to content

[stdlib] Dictionary.updateValue(_:,forKey:): Don’t overwrite the existing key #19592

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

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions stdlib/public/core/NativeDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,6 @@ extension _NativeDictionary { // Insertions
if found {
let oldValue = (_values + bucket.offset).move()
(_values + bucket.offset).initialize(to: value)
// FIXME: Replacing the old key with the new is unnecessary, unintuitive,
// and actively harmful to some usecases. We shouldn't do it.
// rdar://problem/32144087
(_keys + bucket.offset).pointee = key
return oldValue
}
_insert(at: bucket, key: key, value: value)
Expand All @@ -435,10 +431,6 @@ extension _NativeDictionary { // Insertions
let (bucket, found) = mutatingFind(key, isUnique: isUnique)
if found {
(_values + bucket.offset).pointee = value
// FIXME: Replacing the old key with the new is unnecessary, unintuitive,
// and actively harmful to some usecases. We shouldn't do it.
// rdar://problem/32144087
(_keys + bucket.offset).pointee = key
} else {
_insert(at: bucket, key: key, value: value)
}
Expand Down
28 changes: 27 additions & 1 deletion validation-test/stdlib/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ DictionaryTestSuite.test("COW.Fast.UpdateValueForKeyDoesNotReallocate") {
}
}

DictionaryTestSuite.test("COW.Slow.AddDoesNotReallocate") {
DictionaryTestSuite.test("COW.Slow.UpdateValueForKeyDoesNotReallocate") {
do {
var d1 = getCOWSlowDictionary()
let identity1 = d1._rawIdentifier()
Expand Down Expand Up @@ -4642,6 +4642,32 @@ DictionaryTestSuite.test("removeAt") {
}
}

DictionaryTestSuite.test("updateValue") {
let key1 = TestKeyTy(42)
let key2 = TestKeyTy(42)
let value1 = TestValueTy(1)
let value2 = TestValueTy(2)

var d: [TestKeyTy: TestValueTy] = [:]

expectNil(d.updateValue(value1, forKey: key1))

expectEqual(d.count, 1)
let index1 = d.index(forKey: key2)
expectNotNil(index1)
expectTrue(d[index1!].key === key1)
expectTrue(d[index1!].value === value1)

expectTrue(d.updateValue(value2, forKey: key2) === value1)

expectEqual(d.count, 1)
let index2 = d.index(forKey: key2)
expectEqual(index1, index2)
// We expect updateValue to keep the original key in place.
expectTrue(d[index2!].key === key1) // Not key2
expectTrue(d[index2!].value === value2)
}

DictionaryTestSuite.test("localHashSeeds") {
// With global hashing, copying elements in hash order between hash tables
// can become quadratic. (See https://bugs.swift.org/browse/SR-3268)
Expand Down