Sources: Add unsafe keywords where needed

This commit is contained in:
Karoy Lorentey committed 2026-10-01 11:14:29 -07:00
1 parent 13f71ad927
commit d07dc32dfa
301 files changed
+6051 -5498

No files matched your search

@@ -36,68 +36,68 @@ extension _HashNode {
return _intersection_slow(level, other)
}
return self.read { l in
other.read { r in
return unsafe self.read { l in
unsafe other.read { r in
var result: Builder = .empty(level)
var removing = false
for (bucket, lslot) in l.itemMap {
let lp = l.itemPtr(at: lslot)
for (bucket, lslot) in unsafe l.itemMap {
let lp = unsafe l.itemPtr(at: lslot)
let include: Bool
if r.itemMap.contains(bucket) {
let rslot = r.itemMap.slot(of: bucket)
include = (lp.pointee.key == r[item: rslot].key)
if unsafe r.itemMap.contains(bucket) {
let rslot = unsafe r.itemMap.slot(of: bucket)
include = unsafe (lp.pointee.key == r[item: rslot].key)
}
else if r.childMap.contains(bucket) {
let rslot = r.childMap.slot(of: bucket)
let h = _Hash(lp.pointee.key)
include = r[child: rslot]
else if unsafe r.childMap.contains(bucket) {
let rslot = unsafe r.childMap.slot(of: bucket)
let h = unsafe _Hash(lp.pointee.key)
include = unsafe r[child: rslot]
.containsKey(level.descend(), lp.pointee.key, h)
}
else { include = false}
if include, removing {
result.addNewItem(level, lp.pointee, at: bucket)
unsafe result.addNewItem(level, lp.pointee, at: bucket)
}
else if !include, !removing {
removing = true
result.copyItems(level, from: l, upTo: bucket)
unsafe result.copyItems(level, from: l, upTo: bucket)
}
}
for (bucket, lslot) in l.childMap {
if r.itemMap.contains(bucket) {
for (bucket, lslot) in unsafe l.childMap {
if unsafe r.itemMap.contains(bucket) {
if !removing {
removing = true
result.copyItemsAndChildren(level, from: l, upTo: bucket)
unsafe result.copyItemsAndChildren(level, from: l, upTo: bucket)
}
let rslot = r.itemMap.slot(of: bucket)
let rp = r.itemPtr(at: rslot)
let h = _Hash(rp.pointee.key)
let res = l[child: lslot].lookup(level.descend(), rp.pointee.key, h)
if let res = res {
let item = UnsafeHandle.read(res.node) { $0[item: res.slot] }
result.addNewItem(level, item, at: bucket)
let rslot = unsafe r.itemMap.slot(of: bucket)
let rp = unsafe r.itemPtr(at: rslot)
let h = unsafe _Hash(rp.pointee.key)
let res = unsafe l[child: lslot].lookup(level.descend(), rp.pointee.key, h)
if let res = unsafe res {
let item = unsafe UnsafeHandle.read(res.node) { unsafe $0[item: res.slot] }
unsafe result.addNewItem(level, item, at: bucket)
}
}
else if r.childMap.contains(bucket) {
let rslot = r.childMap.slot(of: bucket)
let branch = l[child: lslot]
else if unsafe r.childMap.contains(bucket) {
let rslot = unsafe r.childMap.slot(of: bucket)
let branch = unsafe l[child: lslot]
._intersection(level.descend(), r[child: rslot])
if let branch = branch {
assert(branch.count < self.count)
if !removing {
removing = true
result.copyItemsAndChildren(level, from: l, upTo: bucket)
unsafe result.copyItemsAndChildren(level, from: l, upTo: bucket)
}
result.addNewChildBranch(level, branch, at: bucket)
unsafe result.addNewChildBranch(level, branch, at: bucket)
} else if removing {
result.addNewChildNode(level, l[child: lslot], at: bucket)
unsafe result.addNewChildNode(level, l[child: lslot], at: bucket)
}
}
else if !removing {
removing = true
result.copyItemsAndChildren(level, from: l, upTo: bucket)
unsafe result.copyItemsAndChildren(level, from: l, upTo: bucket)
}
}
guard removing else { return nil }
@@ -114,22 +114,22 @@ extension _HashNode {
let lc = self.isCollisionNode
let rc = other.isCollisionNode
if lc && rc {
return read { l in
other.read { r in
return unsafe read { l in
unsafe other.read { r in
var result: Builder = .empty(level)
guard l.collisionHash == r.collisionHash else { return result }
guard unsafe l.collisionHash == r.collisionHash else { return result }
var removing = false
let ritems = r.reverseItems
for lslot: _HashSlot in stride(from: .zero, to: l.itemsEndSlot, by: 1) {
let lp = l.itemPtr(at: lslot)
let include = ritems.contains { $0.key == lp.pointee.key }
let ritems = unsafe r.reverseItems
for lslot: _HashSlot in stride(from: .zero, to: unsafe l.itemsEndSlot, by: 1) {
let lp = unsafe l.itemPtr(at: lslot)
let include = unsafe ritems.contains { unsafe $0.key == lp.pointee.key }
if include, removing {
result.addNewCollision(level, lp.pointee, l.collisionHash)
unsafe result.addNewCollision(level, lp.pointee, l.collisionHash)
}
else if !include, !removing {
removing = true
result.copyCollisions(from: l, upTo: lslot)
unsafe result.copyCollisions(from: l, upTo: lslot)
}
}
guard removing else { return nil }
@@ -145,21 +145,21 @@ extension _HashNode {
if lc {
// `self` is a collision node on a compressed path. The other tree might
// have the same set of collisions, just expanded a bit deeper.
return read { l in
other.read { r in
let bucket = l.collisionHash[level]
if r.itemMap.contains(bucket) {
let rslot = r.itemMap.slot(of: bucket)
let ritem = r.itemPtr(at: rslot)
let litems = l.reverseItems
let i = litems.firstIndex { $0.key == ritem.pointee.key }
return unsafe read { l in
unsafe other.read { r in
let bucket = unsafe l.collisionHash[level]
if unsafe r.itemMap.contains(bucket) {
let rslot = unsafe r.itemMap.slot(of: bucket)
let ritem = unsafe r.itemPtr(at: rslot)
let litems = unsafe l.reverseItems
let i = unsafe litems.firstIndex { unsafe $0.key == ritem.pointee.key }
guard let i = i else { return .empty(level) }
return .item(level, litems[i], at: l.collisionHash[level])
return unsafe .item(level, litems[i], at: l.collisionHash[level])
}
if r.childMap.contains(bucket) {
let rslot = r.childMap.slot(of: bucket)
return _intersection(level.descend(), r[child: rslot])
.map { .childBranch(level, $0, at: bucket) }
if unsafe r.childMap.contains(bucket) {
let rslot = unsafe r.childMap.slot(of: bucket)
return unsafe _intersection(level.descend(), r[child: rslot])
.map { unsafe .childBranch(level, $0, at: bucket) }
}
return .empty(level)
}
@@ -168,27 +168,27 @@ extension _HashNode {
assert(rc)
// `other` is a collision node on a compressed path.
return read { l in
other.read { r in
let bucket = r.collisionHash[level]
if l.itemMap.contains(bucket) {
let lslot = l.itemMap.slot(of: bucket)
let litem = l.itemPtr(at: lslot)
let ritems = r.reverseItems
let found = ritems.contains { $0.key == litem.pointee.key }
return unsafe read { l in
unsafe other.read { r in
let bucket = unsafe r.collisionHash[level]
if unsafe l.itemMap.contains(bucket) {
let lslot = unsafe l.itemMap.slot(of: bucket)
let litem = unsafe l.itemPtr(at: lslot)
let ritems = unsafe r.reverseItems
let found = unsafe ritems.contains { unsafe $0.key == litem.pointee.key }
guard found else { return .empty(level) }
return .item(level, litem.pointee, at: bucket)
return unsafe .item(level, litem.pointee, at: bucket)
}
if l.childMap.contains(bucket) {
let lslot = l.childMap.slot(of: bucket)
let branch = l[child: lslot]._intersection(level.descend(), other)
if unsafe l.childMap.contains(bucket) {
let lslot = unsafe l.childMap.slot(of: bucket)
let branch = unsafe l[child: lslot]._intersection(level.descend(), other)
guard let branch = branch else {
assert(l[child: lslot].isCollisionNode)
assert(l[child: lslot].collisionHash == r.collisionHash)
assert(unsafe l[child: lslot].isCollisionNode)
assert(unsafe l[child: lslot].collisionHash == r.collisionHash)
// Compression
return .collisionNode(level, l[child: lslot])
return .collisionNode(level, unsafe l[child: lslot])
}
return .childBranch(level, branch, at: bucket)
return unsafe .childBranch(level, branch, at: bucket)
}
return .empty(level)
}