Remove "Child" struct

This commit is contained in:
2024-06-27 10:03:14 -07:00
parent 9108ee209a
commit bff7b85de2

View File

@@ -236,10 +236,6 @@ constexpr int kNodeCopyBegin = offsetof(Node, entry);
constexpr int kNodeCopySize = constexpr int kNodeCopySize =
offsetof(Node, parentsIndex) + sizeof(Node::parentsIndex) - kNodeCopyBegin; offsetof(Node, parentsIndex) + sizeof(Node::parentsIndex) - kNodeCopyBegin;
struct Child {
Node *child;
};
// copyChildrenAndKeyFrom is responsible for copying all // copyChildrenAndKeyFrom is responsible for copying all
// public members of Node, copying the partial key, logically copying the // public members of Node, copying the partial key, logically copying the
// children (converting representation if necessary), and updating all the // children (converting representation if necessary), and updating all the
@@ -261,7 +257,7 @@ struct Node3 : Node {
constexpr static auto kType = Type_Node3; constexpr static auto kType = Type_Node3;
// Sorted // Sorted
uint8_t index[kMaxNodes]; uint8_t index[kMaxNodes];
Child children[kMaxNodes]; Node *children[kMaxNodes];
int64_t childMaxVersion[kMaxNodes]; int64_t childMaxVersion[kMaxNodes];
uint8_t *partialKey() { return (uint8_t *)(this + 1); } uint8_t *partialKey() { return (uint8_t *)(this + 1); }
@@ -277,7 +273,7 @@ struct Node16 : Node {
constexpr static auto kMaxNodes = 16; constexpr static auto kMaxNodes = 16;
// Sorted // Sorted
uint8_t index[kMaxNodes]; uint8_t index[kMaxNodes];
Child children[kMaxNodes]; Node *children[kMaxNodes];
int64_t childMaxVersion[kMaxNodes]; int64_t childMaxVersion[kMaxNodes];
uint8_t *partialKey() { return (uint8_t *)(this + 1); } uint8_t *partialKey() { return (uint8_t *)(this + 1); }
@@ -294,7 +290,7 @@ struct Node48 : Node {
BitSet bitSet; BitSet bitSet;
int8_t nextFree; int8_t nextFree;
int8_t index[256]; int8_t index[256];
Child children[kMaxNodes]; Node *children[kMaxNodes];
int64_t childMaxVersion[kMaxNodes]; int64_t childMaxVersion[kMaxNodes];
uint8_t reverseIndex[kMaxNodes]; uint8_t reverseIndex[kMaxNodes];
constexpr static int kMaxOfMaxPageSize = 8; constexpr static int kMaxOfMaxPageSize = 8;
@@ -315,7 +311,7 @@ struct Node48 : Node {
struct Node256 : Node { struct Node256 : Node {
constexpr static auto kType = Type_Node256; constexpr static auto kType = Type_Node256;
BitSet bitSet; BitSet bitSet;
Child children[256]; Node *children[256];
int64_t childMaxVersion[256]; int64_t childMaxVersion[256];
constexpr static int kMaxOfMaxPageSize = 8; constexpr static int kMaxOfMaxPageSize = 8;
constexpr static int kMaxOfMaxShift = constexpr static int kMaxOfMaxShift =
@@ -353,8 +349,8 @@ inline void Node3::copyChildrenAndKeyFrom(const Node3 &other) {
memcpy(index, other.index, sizeof(*this) - sizeof(Node)); memcpy(index, other.index, sizeof(*this) - sizeof(Node));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
for (int i = 0; i < numChildren; ++i) { for (int i = 0; i < numChildren; ++i) {
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
} }
} }
@@ -362,13 +358,13 @@ inline void Node3::copyChildrenAndKeyFrom(const Node16 &other) {
memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin, memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin,
kNodeCopySize); kNodeCopySize);
memcpy(index, other.index, kMaxNodes); memcpy(index, other.index, kMaxNodes);
memcpy(children, other.children, kMaxNodes * sizeof(children[0])); memcpy(children, other.children, kMaxNodes * sizeof(children[0])); // NOLINT
memcpy(childMaxVersion, other.childMaxVersion, memcpy(childMaxVersion, other.childMaxVersion,
kMaxNodes * sizeof(childMaxVersion[0])); kMaxNodes * sizeof(childMaxVersion[0]));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
for (int i = 0; i < numChildren; ++i) { for (int i = 0; i < numChildren; ++i) {
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
} }
} }
@@ -376,14 +372,15 @@ inline void Node16::copyChildrenAndKeyFrom(const Node3 &other) {
memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin, memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin,
kNodeCopySize); kNodeCopySize);
memcpy(index, other.index, Node3::kMaxNodes); memcpy(index, other.index, Node3::kMaxNodes);
memcpy(children, other.children, Node3::kMaxNodes * sizeof(children[0])); memcpy(children, other.children,
Node3::kMaxNodes * sizeof(children[0])); // NOLINT
memcpy(childMaxVersion, other.childMaxVersion, memcpy(childMaxVersion, other.childMaxVersion,
Node3::kMaxNodes * sizeof(childMaxVersion[0])); Node3::kMaxNodes * sizeof(childMaxVersion[0]));
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
assert(numChildren == Node3::kMaxNodes); assert(numChildren == Node3::kMaxNodes);
for (int i = 0; i < Node3::kMaxNodes; ++i) { for (int i = 0; i < Node3::kMaxNodes; ++i) {
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
} }
} }
@@ -394,8 +391,8 @@ inline void Node16::copyChildrenAndKeyFrom(const Node16 &other) {
for (int i = 0; i < numChildren; ++i) { for (int i = 0; i < numChildren; ++i) {
children[i] = other.children[i]; children[i] = other.children[i];
childMaxVersion[i] = other.childMaxVersion[i]; childMaxVersion[i] = other.childMaxVersion[i];
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
} }
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
} }
@@ -412,8 +409,8 @@ inline void Node16::copyChildrenAndKeyFrom(const Node48 &other) {
index[i] = c; index[i] = c;
children[i] = other.children[other.index[c]]; children[i] = other.children[other.index[c]];
childMaxVersion[i] = other.childMaxVersion[other.index[c]]; childMaxVersion[i] = other.childMaxVersion[other.index[c]];
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
++i; ++i;
}, },
0, 256); 0, 256);
@@ -436,8 +433,8 @@ inline void Node48::copyChildrenAndKeyFrom(const Node16 &other) {
index[x] = i; index[x] = i;
children[i] = other.children[i]; children[i] = other.children[i];
childMaxVersion[i] = other.childMaxVersion[i]; childMaxVersion[i] = other.childMaxVersion[i];
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
reverseIndex[i] = x; reverseIndex[i] = x;
maxOfMax[i >> Node48::kMaxOfMaxShift] = maxOfMax[i >> Node48::kMaxOfMaxShift] =
std::max(maxOfMax[i >> Node48::kMaxOfMaxShift], childMaxVersion[i]); std::max(maxOfMax[i >> Node48::kMaxOfMaxShift], childMaxVersion[i]);
@@ -456,8 +453,8 @@ inline void Node48::copyChildrenAndKeyFrom(const Node48 &other) {
for (int i = 0; i < numChildren; ++i) { for (int i = 0; i < numChildren; ++i) {
children[i] = other.children[i]; children[i] = other.children[i];
childMaxVersion[i] = other.childMaxVersion[i]; childMaxVersion[i] = other.childMaxVersion[i];
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
} }
memcpy(reverseIndex, other.reverseIndex, sizeof(reverseIndex)); memcpy(reverseIndex, other.reverseIndex, sizeof(reverseIndex));
memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax)); memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax));
@@ -481,8 +478,8 @@ inline void Node48::copyChildrenAndKeyFrom(const Node256 &other) {
index[c] = i; index[c] = i;
children[i] = other.children[c]; children[i] = other.children[c];
childMaxVersion[i] = other.childMaxVersion[c]; childMaxVersion[i] = other.childMaxVersion[c];
assert(children[i].child->parent == &other); assert(children[i]->parent == &other);
children[i].child->parent = this; children[i]->parent = this;
reverseIndex[i] = c; reverseIndex[i] = c;
maxOfMax[i >> Node48::kMaxOfMaxShift] = maxOfMax[i >> Node48::kMaxOfMaxShift] =
std::max(maxOfMax[i >> Node48::kMaxOfMaxShift], childMaxVersion[i]); std::max(maxOfMax[i >> Node48::kMaxOfMaxShift], childMaxVersion[i]);
@@ -503,8 +500,8 @@ inline void Node256::copyChildrenAndKeyFrom(const Node48 &other) {
[&](int c) { [&](int c) {
children[c] = other.children[other.index[c]]; children[c] = other.children[other.index[c]];
childMaxVersion[c] = other.childMaxVersion[other.index[c]]; childMaxVersion[c] = other.childMaxVersion[other.index[c]];
assert(children[c].child->parent == &other); assert(children[c]->parent == &other);
children[c].child->parent = this; children[c]->parent = this;
maxOfMax[c >> Node256::kMaxOfMaxShift] = std::max( maxOfMax[c >> Node256::kMaxOfMaxShift] = std::max(
maxOfMax[c >> Node256::kMaxOfMaxShift], childMaxVersion[c]); maxOfMax[c >> Node256::kMaxOfMaxShift], childMaxVersion[c]);
}, },
@@ -522,8 +519,8 @@ inline void Node256::copyChildrenAndKeyFrom(const Node256 &other) {
[&](int c) { [&](int c) {
children[c] = other.children[c]; children[c] = other.children[c];
childMaxVersion[c] = other.childMaxVersion[c]; childMaxVersion[c] = other.childMaxVersion[c];
assert(children[c].child->parent == &other); assert(children[c]->parent == &other);
children[c].child->parent = this; children[c]->parent = this;
}, },
0, 256); 0, 256);
memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax)); memcpy(maxOfMax, other.maxOfMax, sizeof(maxOfMax));
@@ -769,21 +766,21 @@ Node *&getChildExists(Node *self, uint8_t index) {
__builtin_unreachable(); // GCOVR_EXCL_LINE __builtin_unreachable(); // GCOVR_EXCL_LINE
case Type_Node3: { case Type_Node3: {
auto *self3 = static_cast<Node3 *>(self); auto *self3 = static_cast<Node3 *>(self);
return self3->children[getNodeIndex(self3, index)].child; return self3->children[getNodeIndex(self3, index)];
} }
case Type_Node16: { case Type_Node16: {
auto *self16 = static_cast<Node16 *>(self); auto *self16 = static_cast<Node16 *>(self);
return self16->children[getNodeIndex(self16, index)].child; return self16->children[getNodeIndex(self16, index)];
} }
case Type_Node48: { case Type_Node48: {
auto *self48 = static_cast<Node48 *>(self); auto *self48 = static_cast<Node48 *>(self);
assert(self48->bitSet.test(index)); assert(self48->bitSet.test(index));
return self48->children[self48->index[index]].child; return self48->children[self48->index[index]];
} }
case Type_Node256: { case Type_Node256: {
auto *self256 = static_cast<Node256 *>(self); auto *self256 = static_cast<Node256 *>(self);
assert(self256->bitSet.test(index)); assert(self256->bitSet.test(index));
return self256->children[index].child; return self256->children[index];
} }
default: // GCOVR_EXCL_LINE default: // GCOVR_EXCL_LINE
__builtin_unreachable(); // GCOVR_EXCL_LINE __builtin_unreachable(); // GCOVR_EXCL_LINE
@@ -803,21 +800,21 @@ Node *getChild(Node *self, uint8_t index) {
case Type_Node3: { case Type_Node3: {
auto *self3 = static_cast<Node3 *>(self); auto *self3 = static_cast<Node3 *>(self);
int i = getNodeIndex(self3, index); int i = getNodeIndex(self3, index);
return i < 0 ? nullptr : self3->children[i].child; return i < 0 ? nullptr : self3->children[i];
} }
case Type_Node16: { case Type_Node16: {
auto *self16 = static_cast<Node16 *>(self); auto *self16 = static_cast<Node16 *>(self);
int i = getNodeIndex(self16, index); int i = getNodeIndex(self16, index);
return i < 0 ? nullptr : self16->children[i].child; return i < 0 ? nullptr : self16->children[i];
} }
case Type_Node48: { case Type_Node48: {
auto *self48 = static_cast<Node48 *>(self); auto *self48 = static_cast<Node48 *>(self);
int i = self48->index[index]; int i = self48->index[index];
return i < 0 ? nullptr : self48->children[i].child; return i < 0 ? nullptr : self48->children[i];
} }
case Type_Node256: { case Type_Node256: {
auto *self256 = static_cast<Node256 *>(self); auto *self256 = static_cast<Node256 *>(self);
return self256->children[index].child; return self256->children[index];
} }
default: // GCOVR_EXCL_LINE default: // GCOVR_EXCL_LINE
__builtin_unreachable(); // GCOVR_EXCL_LINE __builtin_unreachable(); // GCOVR_EXCL_LINE
@@ -933,26 +930,26 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
auto *self3 = static_cast<Node3 *>(self); auto *self3 = static_cast<Node3 *>(self);
int i = getNodeIndex(self3, index); int i = getNodeIndex(self3, index);
if (i >= 0) { if (i >= 0) {
return self3->children[i].child; return self3->children[i];
} }
} break; } break;
case Type_Node16: { case Type_Node16: {
auto *self16 = static_cast<Node16 *>(self); auto *self16 = static_cast<Node16 *>(self);
int i = getNodeIndex(self16, index); int i = getNodeIndex(self16, index);
if (i >= 0) { if (i >= 0) {
return self16->children[i].child; return self16->children[i];
} }
} break; } break;
case Type_Node48: { case Type_Node48: {
auto *self48 = static_cast<Node48 *>(self); auto *self48 = static_cast<Node48 *>(self);
int secondIndex = self48->index[index]; int secondIndex = self48->index[index];
if (secondIndex >= 0) { if (secondIndex >= 0) {
return self48->children[secondIndex].child; return self48->children[secondIndex];
} }
} break; } break;
case Type_Node256: { case Type_Node256: {
auto *self256 = static_cast<Node256 *>(self); auto *self256 = static_cast<Node256 *>(self);
if (auto &result = self256->children[index].child; result != nullptr) { if (auto &result = self256->children[index]; result != nullptr) {
return result; return result;
} }
} break; } break;
@@ -990,7 +987,8 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
memmove(self3->index + i + 1, self3->index + i, memmove(self3->index + i + 1, self3->index + i,
self->numChildren - (i + 1)); self->numChildren - (i + 1));
memmove(self3->children + i + 1, self3->children + i, memmove(self3->children + i + 1, self3->children + i,
(self->numChildren - (i + 1)) * sizeof(self3->children[0])); (self->numChildren - (i + 1)) *
sizeof(self3->children[0])); // NOLINT
memmove(self3->childMaxVersion + i + 1, self3->childMaxVersion + i, memmove(self3->childMaxVersion + i + 1, self3->childMaxVersion + i,
(self->numChildren - (i + 1)) * (self->numChildren - (i + 1)) *
sizeof(self3->childMaxVersion[0])); sizeof(self3->childMaxVersion[0]));
@@ -998,7 +996,7 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
} }
} }
self3->index[i] = index; self3->index[i] = index;
auto &result = self3->children[i].child; auto &result = self3->children[i];
result = nullptr; result = nullptr;
return result; return result;
} }
@@ -1053,7 +1051,8 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
memmove(self16->index + i + 1, self16->index + i, memmove(self16->index + i + 1, self16->index + i,
self->numChildren - (i + 1)); self->numChildren - (i + 1));
memmove(self16->children + i + 1, self16->children + i, memmove(self16->children + i + 1, self16->children + i,
(self->numChildren - (i + 1)) * sizeof(self16->children[0])); (self->numChildren - (i + 1)) *
sizeof(self16->children[0])); // NOLINT
memmove(self16->childMaxVersion + i + 1, self16->childMaxVersion + i, memmove(self16->childMaxVersion + i + 1, self16->childMaxVersion + i,
(self->numChildren - (i + 1)) * (self->numChildren - (i + 1)) *
sizeof(self16->childMaxVersion[0])); sizeof(self16->childMaxVersion[0]));
@@ -1074,7 +1073,7 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
} }
#endif #endif
self16->index[i] = index; self16->index[i] = index;
auto &result = self16->children[i].child; auto &result = self16->children[i];
result = nullptr; result = nullptr;
return result; return result;
} }
@@ -1097,7 +1096,7 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
int nextFree = self48->nextFree++; int nextFree = self48->nextFree++;
self48->index[index] = nextFree; self48->index[index] = nextFree;
self48->reverseIndex[nextFree] = index; self48->reverseIndex[nextFree] = index;
auto &result = self48->children[nextFree].child; auto &result = self48->children[nextFree];
result = nullptr; result = nullptr;
return result; return result;
} }
@@ -1107,7 +1106,7 @@ Node *&getOrCreateChild(Node *&self, uint8_t index,
auto *self256 = static_cast<Node256 *>(self); auto *self256 = static_cast<Node256 *>(self);
++self->numChildren; ++self->numChildren;
self256->bitSet.set(index); self256->bitSet.set(index);
return self256->children[index].child; return self256->children[index];
} }
default: // GCOVR_EXCL_LINE default: // GCOVR_EXCL_LINE
__builtin_unreachable(); // GCOVR_EXCL_LINE __builtin_unreachable(); // GCOVR_EXCL_LINE
@@ -1250,7 +1249,7 @@ void maybeDownsize(Node *self, NodeAllocators *allocators,
getInTree(self, impl) = newSelf; getInTree(self, impl) = newSelf;
allocators->node3.release(self3); allocators->node3.release(self3);
} else if (self->numChildren == 1 && !self->entryPresent) { } else if (self->numChildren == 1 && !self->entryPresent) {
auto *child = self3->children[0].child; auto *child = self3->children[0];
int minCapacity = self3->partialKeyLen + 1 + child->partialKeyLen; int minCapacity = self3->partialKeyLen + 1 + child->partialKeyLen;
if (minCapacity > child->getCapacity()) { if (minCapacity > child->getCapacity()) {
@@ -1363,7 +1362,7 @@ Node *erase(Node *self, NodeAllocators *allocators, ConflictSet::Impl *impl,
sizeof(parent3->index[0]) * sizeof(parent3->index[0]) *
(parent->numChildren - (nodeIndex + 1))); (parent->numChildren - (nodeIndex + 1)));
memmove(parent3->children + nodeIndex, parent3->children + nodeIndex + 1, memmove(parent3->children + nodeIndex, parent3->children + nodeIndex + 1,
sizeof(parent3->children[0]) * sizeof(parent3->children[0]) * // NOLINT
(parent->numChildren - (nodeIndex + 1))); (parent->numChildren - (nodeIndex + 1)));
memmove(parent3->childMaxVersion + nodeIndex, memmove(parent3->childMaxVersion + nodeIndex,
parent3->childMaxVersion + nodeIndex + 1, parent3->childMaxVersion + nodeIndex + 1,
@@ -1381,7 +1380,7 @@ Node *erase(Node *self, NodeAllocators *allocators, ConflictSet::Impl *impl,
sizeof(parent16->index[0]) * sizeof(parent16->index[0]) *
(parent->numChildren - (nodeIndex + 1))); (parent->numChildren - (nodeIndex + 1)));
memmove(parent16->children + nodeIndex, parent16->children + nodeIndex + 1, memmove(parent16->children + nodeIndex, parent16->children + nodeIndex + 1,
sizeof(parent16->children[0]) * sizeof(parent16->children[0]) * // NOLINT
(parent->numChildren - (nodeIndex + 1))); (parent->numChildren - (nodeIndex + 1)));
memmove(parent16->childMaxVersion + nodeIndex, memmove(parent16->childMaxVersion + nodeIndex,
parent16->childMaxVersion + nodeIndex + 1, parent16->childMaxVersion + nodeIndex + 1,
@@ -1411,7 +1410,7 @@ Node *erase(Node *self, NodeAllocators *allocators, ConflictSet::Impl *impl,
Node48::kMaxOfMaxShift], Node48::kMaxOfMaxShift],
parent48->childMaxVersion[toRemoveChildrenIndex]); parent48->childMaxVersion[toRemoveChildrenIndex]);
auto parentIndex = auto parentIndex =
parent48->children[toRemoveChildrenIndex].child->parentsIndex; parent48->children[toRemoveChildrenIndex]->parentsIndex;
parent48->index[parentIndex] = toRemoveChildrenIndex; parent48->index[parentIndex] = toRemoveChildrenIndex;
parent48->reverseIndex[toRemoveChildrenIndex] = parentIndex; parent48->reverseIndex[toRemoveChildrenIndex] = parentIndex;
} }
@@ -1425,7 +1424,7 @@ Node *erase(Node *self, NodeAllocators *allocators, ConflictSet::Impl *impl,
case Type_Node256: { case Type_Node256: {
auto *parent256 = static_cast<Node256 *>(parent); auto *parent256 = static_cast<Node256 *>(parent);
parent256->bitSet.reset(parentsIndex); parent256->bitSet.reset(parentsIndex);
parent256->children[parentsIndex].child = nullptr; parent256->children[parentsIndex] = nullptr;
--parent->numChildren; --parent->numChildren;