mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
- Allow bitmap visualization to be included/omitted as needed
- Visualize chain length distribution for both successful and unsuccessful lookups
Check out this sample test output for Robin Hood hashing:
```
⌗ large@11 1536/1792/2048, maxProbeLength: 11, totalProbeLength: 3712, maxOccupiedRun: 68
├╴[00000000] □□■■■■□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■■■■■∙□□□■□■□□∙□□□■■□■■∙■■■■■■■■
├╴[00000064] □□■□□■■■∙■■■■□■□□∙□■■■■■■■∙■■■■■■■■∙■■□□□□■□∙■□■□■■■■∙■■■■■■□□∙■■■■■■■■
├╴[00000128] ■■■■■■■■∙■■■□□□■□∙■■□■□■■□∙□■■■■■□■∙■■■■■■■■∙□■■■■■■■∙■■■■□■□■∙■□■□□■□□
├╴[00000192] ■□■■□■■■∙■■■■□■□■∙□□□□□■■□∙■■■■■■■■∙■■■□■■□■∙□■□■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000256] ■■■■■■□□∙■■■■■■■□∙□■■■■■■□∙□■■■■■■□∙□□■■□□■□∙□□□■■■■□∙□■■■■□■■∙■■■■■■■■
├╴[00000320] ■■■■■■■■∙□■■■■■■■∙■■■■■■■□∙□■■□□■□□∙□□□■■□□■∙■■■■■□■□∙■□■■■■■□∙■■■■■■■■
├╴[00000384] ■■■■■■■■∙■■■■■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙□■■□□■□■∙□□■■□■■■
├╴[00000448] ■■■■□■■■∙■■■□■■■□∙□□□■□■■■∙■■■□□■■■∙■■■□□□□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00000512] ■■■■■□□□∙□□■■□■■■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■□∙□■■■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000576] □□□■■■□■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■■∙■■□■■■■■∙■■■■■■■■∙■■□□■□□□∙■■■□□□■□
├╴[00000640] □■■□□■■□∙□□□□■■■■∙□■■□■■■□∙□■■□■■■■∙■■■■■■■■∙■□□■■■■■∙■■■■■□□□∙■■■■■■■□
├╴[00000704] ■■■■■■■□∙■■■■□□□■∙■■□■□■■■∙■■■■■■■■∙■■■■□■■■∙□□□■□■■■∙□□□■□□□□∙□□□■□■■■
├╴[00000768] □■■■■■□■∙□■■□□□■■∙■■■■■■■□∙■■■■■■■■∙■■■■■■■□∙□■■■□■■□∙■□■■□■■■∙■■■■□■□■
├╴[00000832] ■□□■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■□□∙□□■■■■■■∙□□■■■■■■∙■■■■■■■■∙□■□■■■■■
├╴[00000896] ■■■■■■■■∙■■■■■□□■∙■■■□□□□■∙■■□□■■■■∙■□□■■■■□∙■■■□□■■□∙□□■■□■■■∙□■□□■■■■
├╴[00000960] ■■■■■□■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■□∙□■□■■■■■∙■■■■□□■■
├╴[00001024] ■■■■■■□□∙■■■■□■■□∙■■■■■■■□∙□□□■■■□□∙■■■□■■□■∙■□■■□□□■∙■■■■■■■■∙□□□□□□□□
├╴[00001088] ■■□□□□■□∙■■□□□□□■∙■□□■■■■■∙■■■□□□□■∙■■■■■■■■∙■■■■■■■■∙■■■■□□□□∙■■□■□■■■
├╴[00001152] ■■■□□■■□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■□■■■∙■□■■□■■□∙□■■■■□■■
├╴[00001216] ■■■■■■■■∙■■□■□■■■∙■■■■■■■■∙□□■■□□□■∙□□□□□□□□∙□□■■■■■■∙■■■■■■■■∙■■■□□■■■
├╴[00001280] ■■■■■■■□∙■■□■■■■■∙■■■■■■□□∙■□□■■■■■∙■■■■■■■■∙■□□■□■□■∙■■■■■■■■∙■■■■■■■■
├╴[00001344] ■■■■■■■■∙■□■□■■□□∙■■■■□■□□∙□■■□■□□■∙□■□■■■■■∙■■■□■■■□∙□□■■■■■■∙■■■■■■■■
├╴[00001408] ■■■■■■■■∙■■■■■■■■∙■■■■■□□■∙■■■□■■■■∙■■□■■■■■∙■■■□■■■■∙■■□□■□□□∙■□■■■■■■
├╴[00001472] ■■■■■□■□∙■■□□□□□■∙□□□□□■■□∙■□□■■□■□∙■■■■■■■■∙■■■■■■■■∙□□□□■■■■∙■□■□□■■■
├╴[00001536] ■■■■■■■■∙□□□■■□■□∙□■■□■■□□∙□■□■■□□□∙■■■■■□□■∙■□■■■■■■∙■■■■■■■■∙■□□■□□□□
├╴[00001600] ■■■■■■■■∙■■■■□□■□∙□□■■□■■■∙■■■■■■■□∙□■□□■■□■∙■□■■■□■■∙■■■■■■■■∙□□■■■■■■
├╴[00001664] ■■■■■■■■∙□■■■□□■■∙■■■□■□■□∙■■■■■■□■∙■■■□■■■■∙■■■■■■■□∙□□□□■■■■∙■■■■□■■■
├╴[00001728] ■■■■■■■■∙■□□■■■■■∙■■□□■■□□∙■■■■■■■■∙■■■□□□■■∙□□■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00001792] ■■■■■■■■∙■■■■■■■■∙■□□□■■□■∙□□■■□□□□∙□■■□■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■
├╴[00001856] ■■■■■■■□∙■■■■■■■□∙□■□■□■■□∙■□□□■■■■∙■□■■□■■□∙□□■□■□■■∙■□□□□■■■∙■■■■■■■■
├╴[00001920] ■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□□■□■□□
└╴[00001984] ■□□■□□■■∙□■■■■■■□∙□□□□■■■■∙■□□□■■■■∙■■■■■■■■∙■■□■□■■■∙■■■□■■□□∙■□□■■□■□
Chain length distribution (for successful lookups):
AVG: 2.416
1: **************************************************************************** 549
2: ******************************************************** 408
3: ************************************* 264
4: ******************** 142
5: ************** 96
6: ******* 51
7: *** 17
8: * 4
9: * 1
10: * 3
11: * 1
Chain length distribution (for unsuccessful lookups):
AVG: 4.428
0: **************************************************************************** 511
1: **************************************** 269
2: ***************************** 191
3: ******************* 129
4: ***************** 111
5: ************** 94
6: ************* 85
7: *********** 70
8: ********* 60
9: ********* 55
10: ******** 50
11: ************************************************************** 422
```
120 lines
3.8 KiB
Swift
120 lines
3.8 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidSet where Element: ~Copyable {
|
|
public func _describe(
|
|
bitmap: Bool = false,
|
|
chains: Bool = false,
|
|
) -> String {
|
|
var s = _table.describe(bitmap: bitmap)
|
|
if chains {
|
|
s += "\n"
|
|
s += _chainDescription
|
|
}
|
|
return s
|
|
}
|
|
|
|
public func _dump(
|
|
bitmap: Bool = false,
|
|
chains: Bool = false,
|
|
) {
|
|
print(self._describe(bitmap: bitmap, chains: chains))
|
|
}
|
|
|
|
public func _probeLengthCounts() -> (successful: [Int], unsuccessful: [Int]) {
|
|
var positiveLengths: [Int: Int] = [:]
|
|
var negativeLengths: [Int: Int] = [:]
|
|
var it = self._table.makeBucketIterator()
|
|
var prev = self._table.startBucket
|
|
while let next = it.nextOccupiedRegion() {
|
|
negativeLengths[0, default: 0] += next.lowerBound.offset - prev.offset
|
|
var b = next.lowerBound
|
|
while b != next.upperBound {
|
|
let hashValue = self._hashValue(at: b)
|
|
let pl = _table.probeLength(forHashValue: hashValue, in: b)
|
|
positiveLengths[pl, default: 0] += 1
|
|
|
|
var nl = next.upperBound.offset - b.offset
|
|
#if !COLLECTIONS_NO_ROBIN_HOOD_HASHING
|
|
nl = Swift.min(nl, _table._maxProbeLength)
|
|
#endif
|
|
negativeLengths[nl, default: 0] += 1
|
|
_table.formBucket(after: &b)
|
|
}
|
|
prev = next.upperBound
|
|
}
|
|
var successful = Array(repeating: 0, count: positiveLengths.keys.max() ?? 0)
|
|
assert(positiveLengths[0] == nil)
|
|
var totalChainLength = 0
|
|
var totalCount = 0
|
|
for (length, count) in positiveLengths {
|
|
successful[length - 1] = count
|
|
totalChainLength += length * count
|
|
totalCount += count
|
|
}
|
|
assert(totalCount == self.count)
|
|
|
|
var unsuccessful = Array(
|
|
repeating: 0,
|
|
count: 1 + (negativeLengths.keys.max() ?? 0))
|
|
for (length, count) in negativeLengths {
|
|
unsuccessful[length] = count
|
|
}
|
|
//assert(totalChainLength == _table._totalProbeLength)
|
|
return (successful, unsuccessful)
|
|
}
|
|
|
|
package var _chainDescription: String {
|
|
let data = self._probeLengthCounts()
|
|
var str = ""
|
|
do {
|
|
str += "Chain length distribution (for successful lookups):\n"
|
|
let max = data.successful.max() ?? 1
|
|
var sum = 0
|
|
var histogram = ""
|
|
for length in data.successful.indices {
|
|
let count = data.successful[length]
|
|
histogram += "\(String(length + 1).lpad(6)): "
|
|
let dotCount = (75 * count + max) / max
|
|
histogram += String(repeating: "*", count: dotCount)
|
|
histogram += " \(count)\n"
|
|
sum += (length + 1) * count
|
|
}
|
|
let avg = Double((sum * 1000 + 500) / _table.count) / 1000.0
|
|
str += " AVG: \(avg)\n"
|
|
str += histogram
|
|
}
|
|
do {
|
|
str += "Chain length distribution (for unsuccessful lookups):\n"
|
|
let max = data.unsuccessful.max() ?? 1
|
|
var sum = 0
|
|
var histogram = ""
|
|
for length in data.unsuccessful.indices {
|
|
let count = data.unsuccessful[length]
|
|
histogram += "\(String(length).lpad(6)): "
|
|
let dotCount = (75 * count + max) / max
|
|
histogram += String(repeating: "*", count: dotCount)
|
|
histogram += " \(count)\n"
|
|
sum += length * count
|
|
}
|
|
let avg = Double((sum * 1000 + 500) / _table.storageCapacity) / 1000.0
|
|
str += " AVG: \(avg)\n"
|
|
str += histogram
|
|
}
|
|
return str
|
|
}
|
|
}
|
|
|
|
#endif
|