[BasicContainers] Improved debug output for hashed containers

- 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
```
This commit is contained in:
Karoy Lorentey committed 2026-02-19 12:10:26 -08:00
1 parent 099aa3b312
commit 0cf34cc3a9
9 files changed
+230 -37

No files matched your search

@@ -35,6 +35,7 @@ extension _HTable {
tester: (Bucket) -> Bool,
) -> Bucket? {
assert(!isSmall)
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
// Naive find
let start = idealBucket(forHashValue: hashValue)
var it = makeBucketIterator(from: start)
@@ -45,6 +46,19 @@ extension _HTable {
it.wrapToNextBit()
}
return nil
#else
let start = idealBucket(forHashValue: hashValue)
var it = makeBucketIterator(from: start)
var i = 0
while it.isOccupied, i <= _maxProbeLength {
if tester(it.currentBucket) {
return it.currentBucket
}
it.wrapToNextBit()
i &+= 1
}
return nil
#endif
}
}
#endif
@@ -19,6 +19,8 @@ extension _HTable {
// FIXME: Scramble order by swapping things in some way
let r = Bucket(offset: _count)
_count &+= 1
_totalProbeLength += _count
_maxProbeLength = _count
return r
}
@@ -34,9 +36,13 @@ extension _HTable {
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
let ideal = idealBucket(forHashValue: hashValue)
let actual = _bitmap.firstUnoccupiedBucket(from: b)
_totalProbeLength += probeLength(from: ideal, to: actual)
_bitmap.setOccupied(actual)
let bitmap = self.bitmap
let actual = bitmap.firstUnoccupiedBucket(from: ideal)
bitmap.setOccupied(actual)
let probeLength = self.probeLength(from: ideal, to: actual)
_totalProbeLength += probeLength
_maxProbeLength = Swift.max(_maxProbeLength, probeLength)
_count &+= 1
return actual
#else
var b = idealBucket(forHashValue: hashValue)
@@ -50,13 +56,19 @@ extension _HTable {
in: b)
if probeLength > oldProbeLength {
swapper(b)
if probeLength > _maxProbeLength {
_maxProbeLength = probeLength
}
probeLength = oldProbeLength
}
self.wrapBucket(after: &b)
}
_totalProbeLength &+= 1
bitmap.setOccupied(b)
_count &+= 1
if probeLength > _maxProbeLength {
_maxProbeLength = probeLength
}
_totalProbeLength &+= 1
return b
#endif
}
@@ -17,7 +17,7 @@ import InternalCollectionsUtilities
extension _HTable {
/// The minimum hash table scale.
@_alwaysEmitIntoClient
internal static var minimumScale: UInt8 {
package static var minimumScale: UInt8 {
@_effects(readnone)
@_transparent
get {
@@ -27,7 +27,7 @@ extension _HTable {
/// The maximum hash table scale.
@_alwaysEmitIntoClient
internal static var maximumScale: UInt8 {
package static var maximumScale: UInt8 {
@_effects(readnone)
@_transparent
get {
@@ -37,7 +37,7 @@ extension _HTable {
/// The maximum number of items for which we do not create a hash table.
@usableFromInline
internal static var maximumUnhashedCount: Int {
package static var maximumUnhashedCount: Int {
@_effects(readnone)
get {
maximumCapacity(forScale: 0)
@@ -53,17 +53,26 @@ extension _HTable {
}
/// The numerator of the maximum hash table load factor.
@_alwaysEmitIntoClient
@_transparent
internal static var _maxLFNum: UInt { 7 }
internal static var _maxLFNum: UInt {
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
3
#else
7
#endif
}
/// The denominator of the maximum hash table load factor.
@_alwaysEmitIntoClient
@_transparent
internal static var _maxLFDenom: UInt { 8 }
internal static var _maxLFDenom: UInt {
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
4
#else
8
#endif
}
/// The numerator of the minimum hash table load factor.
@_alwaysEmitIntoClient
@_transparent
internal static var _minLFNum: UInt { 1 }
@@ -75,7 +84,7 @@ extension _HTable {
/// The minimum number of items that can be held in a hash table of the given scale.
@usableFromInline
@_effects(readnone)
internal static func minimumCapacity(forScale scale: UInt8) -> Int {
package static func minimumCapacity(forScale scale: UInt8) -> Int {
guard scale >= minimumScale else { return 0 }
precondition(scale <= maximumScale)
let bucketCount: UInt = 1 &<< scale
@@ -85,7 +94,7 @@ extension _HTable {
/// The maximum number of items that can be held in a hash table of the given scale.
@usableFromInline
@_effects(readnone)
internal static func maximumCapacity(forScale scale: UInt8) -> Int {
package static func maximumCapacity(forScale scale: UInt8) -> Int {
let scale = Swift.max(scale, minimumScale &- 1)
let bucketCount: UInt = 1 &<< scale
return Int(bucketCount * _maxLFNum / _maxLFDenom)
@@ -95,7 +104,7 @@ extension _HTable {
/// The minimum hash table scale that can hold the specified number of elements.
@usableFromInline
@_effects(readnone)
internal static func minimumScale(forCapacity capacity: Int) -> UInt8 {
package static func minimumScale(forCapacity capacity: Int) -> UInt8 {
guard capacity > maximumUnhashedCount else { return 0 }
let capacity = UInt(truncatingIfNeeded: Swift.max(capacity, 1))
// Calculate the minimum number of entries we need to allocate to satisfy
@@ -115,7 +124,7 @@ extension _HTable {
@usableFromInline
@_effects(readnone)
internal static func dynamicStorageParameters(
package static func dynamicStorageParameters(
minimumCapacity: Int
) -> (scale: UInt8, capacity: Int) {
let maximumUnhashedCount = self.maximumUnhashedCount
@@ -32,6 +32,9 @@ package struct _HTable: ~Copyable {
@_alwaysEmitIntoClient
package var _totalProbeLength: Int
@_alwaysEmitIntoClient
package var _maxProbeLength: Int
@_alwaysEmitIntoClient
package let scale: UInt8
@@ -46,6 +49,7 @@ package struct _HTable: ~Copyable {
self._count = 0
self._capacity = _capacity
self._totalProbeLength = 0
self._maxProbeLength = 0
self.scale = scale
if scale == 0 {
self._bitmap = nil
@@ -15,39 +15,73 @@ import InternalCollectionsUtilities
#if compiler(>=6.2)
extension _HTable {
package var description: String {
let multiline = self.endBucket.offset > Word.capacity
package func maxOccupiedRun() -> Int {
if isSmall {
return self.count
}
var maxOccupiedRun = 0
var it = self.makeBucketIterator()
var first = true
var wrap = 0
while let next = it.nextOccupiedRegion() {
if first, next.lowerBound.offset == 0 {
wrap = next._offsets.count
}
first = false
var c = next._offsets.count
if next.upperBound == self.endBucket {
c += wrap
}
maxOccupiedRun = max(maxOccupiedRun, c)
}
return maxOccupiedRun
}
var buckets = ""
package var _bitmapDescription: String {
let multiline = self.endBucket.offset > Word.capacity
var str = ""
if !self.isSmall {
let lastWord = self.endBucket.bit > 0 ? self.endBucket.word : self.endBucket.word - 1
var it = self.makeBucketIterator()
while !it.isAtEnd {
if it.currentBucket.isOnWordBoundary {
if multiline {
buckets += "\n "
buckets += it.currentBucket.word == lastWord ? "└╴" : "├╴"
buckets += "[" + String(it.currentBucket.offset).lpad(8, with: "0") + "] "
str += "\n "
str += it.currentBucket.word == lastWord ? "└╴" : "├╴"
str += "[" + String(it.currentBucket.offset).lpad(8, with: "0") + "] "
} else {
buckets += " "
str += " "
}
} else if it.currentBucket.bit % 8 == 0 {
buckets += "∙"
str += "∙"
}
buckets.append(it.isOccupied ? "■" : "□")
str.append(it.isOccupied ? "■" : "□")
it.advanceToNextBit()
}
}
return str
}
package func describe(
bitmap: Bool = false,
) -> String {
let bitmapDesc = bitmap ? self._bitmapDescription : ""
let multiline = bitmapDesc.unicodeScalars.contains("\n")
var s = "⌗ \(scale == 0 ? "small" : "large@\(scale)")"
s += "\(multiline ? "" : buckets) "
s += "\(count)/\(capacity), "
s += "totalProbeLength: \(_totalProbeLength)"
s += "\(multiline ? "" : bitmapDesc) "
s += "\(count)/\(capacity)/\(bucketCount), "
s += "maxProbeLength: \(_maxProbeLength), "
s += "totalProbeLength: \(_totalProbeLength), "
s += "maxOccupiedRun: \(maxOccupiedRun())"
if !self.isSmall, multiline {
s += buckets
s += bitmapDesc
}
return s
}
package var description: String {
describe()
}
}
#endif