Maintain "reverseIndex" in Node48

This commit is contained in:
2024-06-26 18:57:08 -07:00
parent bfea4384ba
commit c882d7663d

View File

@@ -294,8 +294,7 @@ struct Node48 : Node {
int8_t nextFree; int8_t nextFree;
int8_t index[256]; int8_t index[256];
Child children[kMaxNodes]; Child children[kMaxNodes];
// Max of each "page" of 16 children uint8_t reverseIndex[kMaxNodes];
int64_t maxOfMax[16];
uint8_t *partialKey() { return (uint8_t *)(this + 1); } uint8_t *partialKey() { return (uint8_t *)(this + 1); }
@@ -419,7 +418,7 @@ inline void Node48::copyChildrenAndKeyFrom(const Node16 &other) {
children[i] = other.children[i]; children[i] = other.children[i];
assert(children[i].child->parent == &other); assert(children[i].child->parent == &other);
children[i].child->parent = this; children[i].child->parent = this;
maxOfMax[x >> 4] = std::max(maxOfMax[x >> 4], children[i].childMaxVersion); reverseIndex[i] = x;
++i; ++i;
} }
} }
@@ -434,7 +433,7 @@ inline void Node48::copyChildrenAndKeyFrom(const Node48 &other) {
assert(children[i].child->parent == &other); assert(children[i].child->parent == &other);
children[i].child->parent = this; children[i].child->parent = this;
} }
memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax)); memcpy(reverseIndex, other.reverseIndex, sizeof(reverseIndex));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
} }
@@ -454,10 +453,10 @@ inline void Node48::copyChildrenAndKeyFrom(const Node256 &other) {
children[i] = other.children[c]; children[i] = other.children[c];
assert(children[i].child->parent == &other); assert(children[i].child->parent == &other);
children[i].child->parent = this; children[i].child->parent = this;
reverseIndex[i] = c;
++i; ++i;
}, },
0, 256); 0, 256);
memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
} }
@@ -472,9 +471,10 @@ inline void Node256::copyChildrenAndKeyFrom(const Node48 &other) {
children[c] = other.children[other.index[c]]; children[c] = other.children[other.index[c]];
assert(children[c].child->parent == &other); assert(children[c].child->parent == &other);
children[c].child->parent = this; children[c].child->parent = this;
maxOfMax[c >> 4] =
std::max(maxOfMax[c >> 4], children[c].childMaxVersion);
}, },
0, 256); 0, 256);
memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
} }
@@ -575,7 +575,7 @@ template <class T> struct BoundedFreeListAllocator {
if constexpr (!std::is_same_v<T, Node0>) { if constexpr (!std::is_same_v<T, Node0>) {
memset(result->children, 0, sizeof(result->children)); memset(result->children, 0, sizeof(result->children));
} }
if constexpr (std::is_same_v<T, Node48> || std::is_same_v<T, Node256>) { if constexpr (std::is_same_v<T, Node256>) {
memset(result->maxOfMax, 0, sizeof(result->maxOfMax)); memset(result->maxOfMax, 0, sizeof(result->maxOfMax));
} }
return result; return result;
@@ -1047,6 +1047,7 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
assert(self48->nextFree < 48); assert(self48->nextFree < 48);
int nextFree = self48->nextFree++; int nextFree = self48->nextFree++;
self48->index[index] = nextFree; self48->index[index] = nextFree;
self48->reverseIndex[nextFree] = index;
auto &result = self48->children[nextFree].child; auto &result = self48->children[nextFree].child;
result = nullptr; result = nullptr;
return result; return result;
@@ -1346,8 +1347,10 @@ Node *erase(Node *self, NodeAllocators *allocators, ConflictSet::Impl *impl,
if (toRemoveChildrenIndex != lastChildrenIndex) { if (toRemoveChildrenIndex != lastChildrenIndex) {
parent48->children[toRemoveChildrenIndex] = parent48->children[toRemoveChildrenIndex] =
parent48->children[lastChildrenIndex]; parent48->children[lastChildrenIndex];
parent48->index[parent48->children[toRemoveChildrenIndex] auto parentIndex =
.child->parentsIndex] = toRemoveChildrenIndex; parent48->children[toRemoveChildrenIndex].child->parentsIndex;
parent48->index[parentIndex] = toRemoveChildrenIndex;
parent48->reverseIndex[toRemoveChildrenIndex] = parentIndex;
} }
--parent->numChildren; --parent->numChildren;
@@ -1692,36 +1695,18 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
} break; } break;
case Type_Node48: { case Type_Node48: {
auto *self = static_cast<Node48 *>(n); auto *self = static_cast<Node48 *>(n);
// Check the first page bool conflict[Node48::kMaxNodes] = {};
if (self->maxOfMax[begin >> 4] > readVersion) { assume(self->numChildren >= kMinChildrenNode48 - 1 /* entry present */);
bool result = true; assume(self->numChildren <= Node48::kMaxNodes);
self->bitSet.forEachInRange( for (int i = 0; i < self->numChildren; ++i) {
[&](int i) { assert(self->index[self->reverseIndex[i]] == i);
result &= conflict[i] = (begin <= self->reverseIndex[i]) &
self->children[self->index[i]].childMaxVersion <= readVersion; (self->reverseIndex[i] < end) &
}, (self->children[i].childMaxVersion > readVersion);
begin, std::min((begin & ~15) + 16, end));
if (!result) {
return result;
}
} }
// Check the last page
if (end >= 1 && self->maxOfMax[(end - 1) >> 4] > readVersion) {
bool result = true;
self->bitSet.forEachInRange(
[&](int i) {
result &=
self->children[self->index[i]].childMaxVersion <= readVersion;
},
std::max(begin, end & ~15), end);
if (!result) {
return result;
}
}
// Check inner pages
bool result = true; bool result = true;
for (int i = (begin >> 4) + 1; i < (end - 1) >> 4; ++i) { for (auto c : conflict) {
result &= self->maxOfMax[i] <= readVersion; result &= !c;
} }
return result; return result;
} }
@@ -2685,7 +2670,6 @@ void setMaxVersion(Node *n, ConflictSet::Impl *impl, int64_t newMax) {
auto *n48 = static_cast<Node48 *>(n); auto *n48 = static_cast<Node48 *>(n);
assert(n48->bitSet.test(index)); assert(n48->bitSet.test(index));
n48->children[n48->index[index]].childMaxVersion = newMax; n48->children[n48->index[index]].childMaxVersion = newMax;
n48->maxOfMax[index >> 4] = std::max(n48->maxOfMax[index >> 4], newMax);
return; return;
} }
case Type_Node256: { case Type_Node256: {