Files
swift-collections/Sources/PersistentCollections/Node/_Hash.swift
T
Karoy Lorentey c9ca59fbb9 [PersistentCollections] Improved storage representation
- Switch to using `ManagedBuffer` to represent node storage. This has
  useful performance benefits, but it significantly complicates access
  to node data. We no longer have direct access to the underlying raw
  storage: everything needs to go through closures passed to
  `ManagedBuffer.withUnsafeMutablePointers`.

- Introduce `struct _Node` and `struct _Node.UnsafeHandle` to
  represent a (strong) reference to a hash tree node, and a
  convenient handle for node storage, respectively. Use these instead
  of directly accessing the ManagedBuffer object, whenever feasible.

- Implement separate paths for in-place mutations vs copy-on-write
  operation, optimizing both flavors.

- Go to great lengths to eliminate unnecessary retain-release traffic,
  whenever possible.

- Push subtree counts into parent nodes by moving the corresponding
  field from the node header into the `_Node` struct. (This results in
  a cool ~20x speedup for jumping around in the tree by integer
  offsets.)

- Implement a type-punned empty root singleton, to allow creating
  empty trees without allocating anything.
2022-09-13 23:48:54 -07:00

90 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
/// An abstract representation of a hash value.
@usableFromInline
@frozen
internal struct _Hash {
@usableFromInline
internal var value: UInt
@inlinable
internal init<Key: Hashable>(_ key: Key) {
let hashValue = key._rawHashValue(seed: 0)
self.value = UInt(bitPattern: hashValue)
}
@inlinable
internal init(_value: UInt) {
self.value = _value
}
}
extension _Hash: Equatable {
@inlinable @inline(__always)
internal static func ==(left: Self, right: Self) -> Bool {
left.value == right.value
}
}
extension _Hash: CustomStringConvertible {
@usableFromInline
internal var description: String {
// Print hash values in radix 32 & reversed, so that the path in the hash
// tree is readily visible.
let p = String(value, radix: _Bitmap.capacity, uppercase: true)
let c = _Level.limit
let path = String(repeating: "0", count: Swift.max(0, c - p.count)) + p
return String(path.reversed())
}
}
extension _Hash {
@inlinable @inline(__always)
internal static var bitWidth: Int { UInt.bitWidth }
}
extension _Hash {
@inlinable
internal subscript(_ level: _Level) -> _Bucket {
get {
assert(!level.isAtBottom)
return _Bucket((value &>> level.shift) & _Bucket.bitMask)
}
set {
let mask = _Bucket.bitMask &<< level.shift
self.value &= ~mask
self.value |= newValue.value &<< level.shift
}
}
}
extension _Hash {
@inlinable
internal func appending(_ bucket: _Bucket, at level: _Level) -> Self {
assert(value >> level.shift == 0)
var copy = self
copy[level] = bucket
return copy
}
@inlinable
internal func isEqual(to other: _Hash, upTo level: _Level) -> Bool {
if level.isAtRoot { return true }
if level.isAtBottom { return self == other }
let s = UInt(UInt.bitWidth) - level.shift
let v1 = self.value &<< s
let v2 = self.value &<< s
return v1 == v2
}
}