Skip to content

Commit 2125304

Browse files
authored
Merge pull request #19563 from apple/revert-19500-updateValue-but-not-the-key
2 parents bf5133d + b4e27b1 commit 2125304

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

stdlib/public/core/NativeDictionary.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ extension _NativeDictionary { // Insertions
413413
if found {
414414
let oldValue = (_values + index.bucket).move()
415415
(_values + index.bucket).initialize(to: value)
416+
// FIXME: Replacing the old key with the new is unnecessary, unintuitive,
417+
// and actively harmful to some usecases. We shouldn't do it.
418+
// rdar://problem/32144087
419+
(_keys + index.bucket).pointee = key
416420
return oldValue
417421
}
418422
_insert(at: index, key: key, value: value)
@@ -428,6 +432,10 @@ extension _NativeDictionary { // Insertions
428432
let (index, found) = mutatingFind(key, isUnique: isUnique)
429433
if found {
430434
(_values + index.bucket).pointee = value
435+
// FIXME: Replacing the old key with the new is unnecessary, unintuitive,
436+
// and actively harmful to some usecases. We shouldn't do it.
437+
// rdar://problem/32144087
438+
(_keys + index.bucket).pointee = key
431439
} else {
432440
_insert(at: index, key: key, value: value)
433441
}

validation-test/stdlib/Dictionary.swift

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ DictionaryTestSuite.test("COW.Fast.UpdateValueForKeyDoesNotReallocate") {
411411
}
412412
}
413413

414-
DictionaryTestSuite.test("COW.Slow.UpdateValueForKeyDoesNotReallocate") {
414+
DictionaryTestSuite.test("COW.Slow.AddDoesNotReallocate") {
415415
do {
416416
var d1 = getCOWSlowDictionary()
417417
let identity1 = d1._rawIdentifier()
@@ -4642,32 +4642,6 @@ DictionaryTestSuite.test("removeAt") {
46424642
}
46434643
}
46444644

4645-
DictionaryTestSuite.test("updateValue") {
4646-
let key1 = TestKeyTy(42)
4647-
let key2 = TestKeyTy(42)
4648-
let value1 = TestValueTy(1)
4649-
let value2 = TestValueTy(2)
4650-
4651-
var d: [TestKeyTy: TestValueTy] = [:]
4652-
4653-
expectNil(d.updateValue(value1, forKey: key1))
4654-
4655-
expectEqual(d.count, 1)
4656-
let index1 = d.index(forKey: key2)
4657-
expectNotNil(index1)
4658-
expectTrue(d[index1!].key === key1)
4659-
expectTrue(d[index1!].value === value1)
4660-
4661-
expectTrue(d.updateValue(value2, forKey: key2) === value1)
4662-
4663-
expectEqual(d.count, 1)
4664-
let index2 = d.index(forKey: key2)
4665-
expectEqual(index1, index2)
4666-
// We expect updateValue to keep the original key in place.
4667-
expectTrue(d[index2!].key === key1) // Not key2
4668-
expectTrue(d[index2!].value === value2)
4669-
}
4670-
46714645
DictionaryTestSuite.test("localHashSeeds") {
46724646
// With global hashing, copying elements in hash order between hash tables
46734647
// can become quadratic. (See https://bugs.swift.org/browse/SR-3268)

0 commit comments

Comments
 (0)