More eraseBetween optimizations

This commit is contained in:
2024-08-14 16:40:29 -07:00
parent e64ebabced
commit 7e49888bec

View File

@@ -1825,32 +1825,28 @@ void eraseBetween(Node **inTree, Node16 *n, int begin, int end,
void eraseBetween(Node **inTree, Node48 *n, int begin, int end, void eraseBetween(Node **inTree, Node48 *n, int begin, int end,
WriteContext *tls) { WriteContext *tls) {
const unsigned shiftUpperBound = end - begin; for (int i = n->bitSet.firstSetGeq(begin); i >= 0 && i < end;
const unsigned shiftAmount = begin; i = n->bitSet.firstSetGeq(i)) {
auto inBounds = [&](unsigned c) { return c - shiftAmount < shiftUpperBound; }; n->bitSet.reset(i);
Node **nodeOut = n->children; int8_t toRemoveChildrenIndex = std::exchange(n->index[i], -1);
uint8_t *indexOut = n->reverseIndex; int8_t lastChildrenIndex = --n->nextFree;
InternalVersionT *maxVOut = n->childMaxVersion; assert(toRemoveChildrenIndex >= 0);
for (auto &v : n->maxOfMax) { assert(lastChildrenIndex >= 0);
v = tls->zero; eraseTree(n->children[toRemoveChildrenIndex], tls);
} if (toRemoveChildrenIndex != lastChildrenIndex) {
n->bitSet = {}; n->children[toRemoveChildrenIndex] = n->children[lastChildrenIndex];
memset(n->index, -1, sizeof(n->index)); n->childMaxVersion[toRemoveChildrenIndex] =
n->nextFree = 0; n->childMaxVersion[lastChildrenIndex];
for (int i = 0; i < n->numChildren; ++i) { n->maxOfMax[toRemoveChildrenIndex >> Node48::kMaxOfMaxShift] =
if (inBounds(n->reverseIndex[i])) { std::max(n->maxOfMax[toRemoveChildrenIndex >> Node48::kMaxOfMaxShift],
eraseTree(n->children[i], tls); n->childMaxVersion[toRemoveChildrenIndex]);
} else { auto parentIndex = n->children[toRemoveChildrenIndex]->parentsIndex;
*nodeOut++ = n->children[i]; n->index[parentIndex] = toRemoveChildrenIndex;
*indexOut++ = n->reverseIndex[i]; n->reverseIndex[toRemoveChildrenIndex] = parentIndex;
*maxVOut++ = n->childMaxVersion[i];
n->maxOfMax[i >> Node48::kMaxOfMaxShift] = std::max(
n->maxOfMax[i >> Node48::kMaxOfMaxShift], n->childMaxVersion[i]);
n->bitSet.set(n->reverseIndex[i]);
n->index[n->reverseIndex[i]] = n->nextFree++;
} }
n->childMaxVersion[lastChildrenIndex] = tls->zero;
--n->numChildren;
} }
n->numChildren = n->nextFree;
if (n->numChildren > Node16::kMaxNodes) { if (n->numChildren > Node16::kMaxNodes) {
// nop // nop