Files
Karoy Lorentey 8c409d4e06 [RigidDictionary] Deallocate values buffer on deinit (#869)
Oops, empty RigidDictionary instances leaked their values buffers on deinit.
2026-11-17 20:51:22 -08:00

630 lines
21 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
import XCTest
#if COLLECTIONS_SINGLE_MODULE
import Collections
#else
import _CollectionsTestSupport
import BasicContainers
#endif
import ContainersPreview
#if compiler(>=6.4) && UnstableHashedContainers
class RigidDictionaryTests: CollectionTestCase {
func test_memory_layout() {
let word = MemoryLayout<Int>.size
expectEqual(MemoryLayout<RigidDictionary<String, Int>>.stride, 7 * word)
expectEqual(MemoryLayout<RigidDictionary<String, Int>?>.stride, 7 * word)
}
func test_sendable_conformance_static() {
// Compile-time assertion: RigidDictionary conforms to Sendable when its contents do.
func requireSendable<T: Sendable & ~Copyable>(_ type: T.Type) {}
requireSendable(RigidDictionary<Int, String>.self)
requireSendable(RigidDictionary<String, Int>.self)
}
func test_empty() {
let s = RigidDictionary<Int, String>()
expectEqual(s.count, 0)
expectTrue(s.isEmpty)
expectTrue(s.isFull)
expectEqual(s.capacity, 0)
expectEqual(s.freeCapacity, 0)
}
func test_init_capacity() {
withSome("capacity", in: 0 ..< 1000) { capacity in
let d = RigidDictionary<Int, String>(capacity: capacity)
expectEqual(d.count, 0)
expectTrue(d.isEmpty)
expectEqual(d.isFull, capacity == 0)
expectEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, capacity)
}
}
func test_mutableValueForKey() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: 20)
let firstKey = tracker.instance(for: 67)
let firstValue = tracker.instance(for: "sixty-seven")
expectNil(d.insertValue(firstValue, forKey: firstKey))
if #available(SwiftStdlib 6.4, *) {
let secondKey = tracker.instance(for: 67)
expectNotNil(d.mutableValue(forKey: secondKey)) { tmpRef in
// FIXME: The language needs to grow up to allow us to elide this
// binding definition.
var valueRef = tmpRef
expectIdentical(valueRef.value, firstValue)
let secondValue = tracker.instance(for: "six-seven")
valueRef.value = secondValue
expectNotIdentical(valueRef.value, firstValue)
expectIdentical(valueRef.value, secondValue)
}
}
}
}
func test_insertValue_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: 20)
let firstKey = tracker.instance(for: 42)
let firstValue = tracker.instance(for: "forty-two")
expectNil(d.insertValue(firstValue, forKey: firstKey))
let secondKey = tracker.instance(for: 42)
let secondValue = tracker.instance(for: "forty-two")
expectIdentical(
d.insertValue(secondValue, forKey: secondKey),
secondValue)
if #available(SwiftStdlib 6.4, *) {
let thirdKey = tracker.instance(for: 42)
expectNotNil(d.value(forKey: thirdKey)) { valueRef in
expectIdentical(valueRef.value, firstValue)
}
_ = consume firstValue
expectNotNil(d.value(forKey: thirdKey)) { valueRef in
expectEqual(valueRef.value.payload, "forty-two")
}
}
expectEqual(d.count, 1)
expectFalse(d.isEmpty)
expectFalse(d.isFull)
expectEqual(d.capacity, 20)
expectEqual(d.freeCapacity, 19)
}
}
func test_updateValue_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: 20)
let firstKey = tracker.instance(for: 42)
let firstValue = tracker.instance(for: "forty-two")
expectNil(d.updateValue(firstValue, forKey: firstKey))
let secondKey = tracker.instance(for: 42)
let secondValue = tracker.instance(for: "forty-two")
let res = d.updateValue(secondValue, forKey: secondKey)
expectIdentical(res, firstValue)
if #available(SwiftStdlib 6.4, *) {
let thirdKey = tracker.instance(for: 42)
expectNotNil(d.value(forKey: thirdKey)) { valueRef in
expectIdentical(valueRef.value, secondValue)
}
_ = consume secondValue
expectNotNil(d.value(forKey: thirdKey)) { valueRef in
expectEqual(valueRef.value.payload, "forty-two")
}
}
expectEqual(d.count, 1)
expectFalse(d.isEmpty)
expectFalse(d.isFull)
expectEqual(d.capacity, 20)
expectEqual(d.freeCapacity, 19)
}
}
func test_insertValue_full() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
let remnant = d.insertValue(value, forKey: key)
expectNil(remnant)
}
expectEqual(d.count, capacity)
expectEqual(d.isEmpty, capacity == 0)
expectTrue(d.isFull)
expectEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, 0)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
let remnant = d.insertValue(value, forKey: key)
expectIdentical(remnant, value)
}
expectEqual(d.count, capacity)
expectEqual(d.isEmpty, capacity == 0)
expectTrue(d.isFull)
expectEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, 0)
if #available(SwiftStdlib 6.4, *) {
for i in 0 ..< capacity {
let dupe = tracker.instance(for: i)
expectNotNil(
d.value(forKey: dupe),
"\(dupe.payload) not found"
) { valueRef in
expectEqual(valueRef.value.payload, "\(i)")
}
}
}
}
}
}
func test_updateValue_full() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
let old = d.updateValue(value, forKey: key)
expectNil(old)
}
expectEqual(d.count, capacity)
expectEqual(d.isEmpty, capacity == 0)
expectTrue(d.isFull)
expectEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, 0)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
let old = d.updateValue(value, forKey: key)
expectNotNil(old) { old in
expectNotIdentical(old, value)
expectEqual(old, value)
}
}
expectEqual(d.count, capacity)
expectEqual(d.isEmpty, capacity == 0)
expectTrue(d.isFull)
expectEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, 0)
if #available(SwiftStdlib 6.4, *) {
for i in 0 ..< capacity {
let dupe = tracker.instance(for: i)
expectNotNil(
d.value(forKey: dupe),
"\(dupe.payload) not found"
) { valueRef in
expectEqual(valueRef.value.payload, "\(i)")
}
}
}
}
}
}
@available(SwiftStdlib 6.4, *)
func test_memoizedValue_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: 20)
let key1 = tracker.instance(for: 42)
let value1 = tracker.instance(for: "\(42)")
do {
let v1 = d.memoizedValue(forKey: key1) { key in
expectIdentical(key, key1)
return value1
}
expectIdentical(v1.value, value1)
}
let key2 = tracker.instance(for: 42)
let value2 = tracker.instance(for: "\(42)")
do {
let v2 = d.memoizedValue(forKey: key2) { key in
expectFailure("Cached value not found")
return value2
}
expectIdentical(v2.value, value1)
}
let key3 = tracker.instance(for: 42)
expectNotNil(d.value(forKey: key3)) { valueRef in
expectIdentical(valueRef.value, value1)
}
expectEqual(d.count, 1)
expectFalse(d.isEmpty)
expectFalse(d.isFull)
expectEqual(d.capacity, 20)
expectEqual(d.freeCapacity, 19)
}
}
@available(SwiftStdlib 6.4, *)
func test_memoizedValue_returnsNewlyInsertedValue() {
// Inserting a new key can displace existing entries (by swapping them
// around in both small and hashed storage); `memoizedValue` must still
// return the value that was just inserted.
withEvery("capacity", in: [1, 2, 5, 7, 8, 20, 100]) { capacity in
var d = RigidDictionary<Int, String>(capacity: capacity)
withEvery("key", in: 0 ..< capacity) { key in
let v = d.memoizedValue(forKey: key) { "\($0)" }
expectEqual(v.value, "\(key)")
}
withEvery("key", in: 0 ..< capacity) { key in
let v = d.memoizedValue(forKey: key) { _ in
expectFailure("Cached value not found")
return ""
}
expectEqual(v.value, "\(key)")
}
}
}
func test_keys() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
withEvery("i", in: 0 ..< capacity) { i in
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
d.insertValue(value, forKey: key)
expectEqual(d.keys.count, i + 1)
expectEqual(d.keys.capacity, capacity)
#if UnstableContainersPreview
var actual: Set<Int> = []
_with(d.keys) { keys in // FIXME: Sigh, borrow accessors are limited in 6.4
var it = keys.makeBorrowingIterator()
while true {
let next = it.nextSpan()
guard !next.isEmpty else { break }
for i in next.indices {
expectTrue(
actual.insert(next[i].payload).inserted,
"Duplicate value \(next[i].payload)")
}
}
}
expectEqualElements(actual.sorted(), 0 ... i)
#else
for j in 0 ... i {
expectTrue(d.keys.contains(tracker.instance(for: j)))
}
#endif
}
}
}
}
@available(*, deprecated)
func test_withKeys() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
withEvery("i", in: 0 ..< capacity) { i in
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
d.insertValue(value, forKey: key)
d.withKeys { keys in
expectEqual(keys.count, i + 1)
expectEqual(keys.capacity, capacity)
#if UnstableContainersPreview
var it = keys.makeBorrowingIterator()
var actual: Set<Int> = []
while true {
let next = it.nextSpan()
guard !next.isEmpty else { break }
for i in next.indices {
expectTrue(
actual.insert(next[i].payload).inserted,
"Duplicate value \(next[i].payload)")
}
}
expectEqualElements(actual.sorted(), 0 ... i)
#else
for j in 0 ... i {
expectTrue(keys.contains(tracker.instance(for: j)))
}
#endif
}
}
}
}
}
func test_iteration_indexAfter() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
withEvery("payload", in: 0 ..< capacity) { payload in
let key = tracker.instance(for: payload)
let value = tracker.instance(for: "\(payload)")
d.insertValue(value, forKey: key)
var seen: Set<Int> = []
var i = d.startIndex
while i != d.endIndex {
let item = d[i]
let payload = item.key.payload
expectEqual(item.value.payload, "\(payload)")
expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)")
i = d.index(after: i)
}
expectEqual(seen.count, payload + 1)
}
}
}
}
#if UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
func test_iteration_indices() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
withEvery("payload", in: 0 ..< capacity) { payload in
let key = tracker.instance(for: payload)
let value = tracker.instance(for: "\(payload)")
d.insertValue(value, forKey: key)
var seen: Set<Int> = []
let indices = d.indices
var it = indices.makeBorrowingIterator()
while true {
let next = it.nextSpan()
if next.isEmpty { break }
expectEqual(next.count, 1)
var i = 0
while i < next.count {
let index = next[i]
let item = d[index]
expectTrue(
seen.insert(item.key.payload).inserted,
"Duplicate item \(item.key.payload)")
expectEqual(item.value.payload, "\(item.key.payload)")
i += 1
}
}
expectEqual(seen.count, payload + 1)
}
}
}
}
#endif
func test_updateValue_with_remove() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withEvery("count", in: [1, capacity / 3, capacity / 2, 2 * capacity / 3, capacity - 1, capacity] as Set) { count in
guard count > 0 else { return }
withSome("key", in: 0 ..< count) { key in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< count {
d.insertValue(tracker.instance(for: "\(i)"), forKey: tracker.instance(for: i))
}
d.updateValue(forKey: tracker.instance(for: key)) { value in
expectNotNil(value) { expectEqual($0.payload, "\(key)") }
value = nil
}
expectEqual(d.count, count - 1)
expectEqual(d.capacity, capacity)
for i in 0 ..< count {
let found = d.withValue(forKey: tracker.instance(for: i)) { $0 }
if i == key {
expectNil(found)
} else {
expectEqual(found?.payload, "\(i)")
}
}
}
}
}
}
}
func test_removeValueForKey_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withEvery("count", in: [1, capacity / 3, capacity / 2, 2 * capacity / 3, capacity - 1, capacity] as Set) { count in
guard count > 0 else { return }
withSome("key", in: 0 ..< count) { key in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< count {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
d.insertValue(value, forKey: key)
}
let oldValue = d.removeValue(forKey: tracker.instance(for: key))
expectNotNil(oldValue, "\(key)") {
expectEqual($0.payload, "\(key)")
}
expectEqual(d.count, count - 1)
expectEqual(d.capacity, capacity)
for i in 0 ..< count {
let value = d.withValue(forKey: tracker.instance(for: i)) { $0 }
if i == key {
expectNil(value)
} else {
expectEqual(value?.payload, "\(i)")
}
}
}
}
}
}
}
func test_removeValueForKey_all() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
d.insertValue(value, forKey: key)
}
withEvery("key", in: 0 ..< capacity) { key in
let oldValue = d.removeValue(forKey: tracker.instance(for: key))
expectNotNil(oldValue) {
expectEqual($0.payload, "\(key)")
}
expectEqual(d.count, capacity - key - 1)
expectEqual(d.capacity, capacity)
}
expectEqual(d.count, 0)
}
}
}
func test_removeAll() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withEvery("capacity", in: [1, 2, 10, 100, 200]) { capacity in
withLifetimeTracking { tracker in
var d = RigidDictionary<Key, Value>(capacity: capacity)
for i in 0 ..< capacity {
let key = tracker.instance(for: i)
let value = tracker.instance(for: "\(i)")
d.insertValue(value, forKey: key)
}
d.removeAll()
expectEqual(d.count, 0)
expectEqual(d.capacity, capacity)
}
}
}
func test_deinit_empty_doesNotLeak() {
withEvery("emptiedByRemoval", in: [false, true]) { emptiedByRemoval in
withEvery("capacity", in: [0, 1, 10, 100]) { capacity in
expectNoHeapGrowth(tolerance: 64 * 1024) {
for _ in 0 ..< 10_000 {
var d = RigidDictionary<Int, Int>(capacity: capacity)
if emptiedByRemoval {
for i in 0 ..< capacity { d.insertValue(i, forKey: i) }
d.removeAll()
}
_ = consume d
}
}
}
}
}
func test_hashable() {
typealias Key = LifetimeTrackedStruct<Int>
typealias Value = LifetimeTrackedStruct<Int>
let contents: [[Int: Int]] = [
[:], [0: 0], [1: 0], [0: 1], [0: 0, 1: 1], [0: 1, 1: 0], [0: 0, 1: 1, 2: 2],
Dictionary(uniqueKeysWithValues: (0 ..< 50).map { ($0, 2 * $0) }),
]
withEvery("i", in: contents.indices) { i in
withEvery("j", in: contents.indices) { j in
withLifetimeTracking { tracker in
var a = RigidDictionary<Key, Value>(capacity: 100)
for (key, value) in contents[i].sorted(by: { $0.key < $1.key }) {
a.insertValue(
tracker.structInstance(for: value),
forKey: tracker.structInstance(for: key))
}
var b = RigidDictionary<Key, Value>(capacity: contents[j].count)
for (key, value) in contents[j].sorted(by: { $0.key > $1.key }) {
b.insertValue(
tracker.structInstance(for: value),
forKey: tracker.structInstance(for: key))
}
// FIXME: Use checkHashable when possible
var goodHash = false
for seed in 0 ..< 10 {
var ha = Hasher()
ha.combine(seed)
a.hash(into: &ha)
let hashValueA = ha.finalize()
var hb = Hasher()
hb.combine(seed)
b.hash(into: &hb)
let hashValueB = hb.finalize()
if contents[i] == contents[j] {
expectEqual(hashValueA, hashValueB)
goodHash = true
} else if hashValueA != hashValueB {
goodHash = true
}
}
expectTrue(goodHash, "Hashing does not differentiate between inequal containers")
}
}
}
}
}
#endif