Remove Node48::nextFree, and improve padding to save 8 bytes

This commit is contained in:
2024-08-20 09:51:29 -07:00
parent f11720f5ae
commit 7261c91492

View File

@@ -264,17 +264,17 @@ struct Node16 : Node {
struct Node48 : Node { struct Node48 : Node {
constexpr static auto kType = Type_Node48; constexpr static auto kType = Type_Node48;
constexpr static auto kMaxNodes = 48; constexpr static auto kMaxNodes = 48;
BitSet bitSet;
int8_t nextFree;
int8_t index[256];
Node *children[kMaxNodes];
InternalVersionT childMaxVersion[kMaxNodes];
uint8_t reverseIndex[kMaxNodes];
constexpr static int kMaxOfMaxPageSize = 16; constexpr static int kMaxOfMaxPageSize = 16;
constexpr static int kMaxOfMaxShift = constexpr static int kMaxOfMaxShift =
std::countr_zero(uint32_t(kMaxOfMaxPageSize)); std::countr_zero(uint32_t(kMaxOfMaxPageSize));
constexpr static int kMaxOfMaxTotalPages = kMaxNodes / kMaxOfMaxPageSize; constexpr static int kMaxOfMaxTotalPages = kMaxNodes / kMaxOfMaxPageSize;
BitSet bitSet;
Node *children[kMaxNodes];
InternalVersionT childMaxVersion[kMaxNodes];
InternalVersionT maxOfMax[kMaxOfMaxTotalPages]; InternalVersionT maxOfMax[kMaxOfMaxTotalPages];
uint8_t reverseIndex[kMaxNodes];
int8_t index[256];
uint8_t *partialKey() { return (uint8_t *)(this + 1); } uint8_t *partialKey() { return (uint8_t *)(this + 1); }
@@ -404,7 +404,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node16 &other) {
} }
memcpy(partialKey(), &other + 1, partialKeyLen); memcpy(partialKey(), &other + 1, partialKeyLen);
bitSet.init(); bitSet.init();
nextFree = Node16::kMaxNodes;
int i = 0; int i = 0;
for (auto x : other.index) { for (auto x : other.index) {
bitSet.set(x); bitSet.set(x);
@@ -424,7 +423,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node48 &other) {
memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin, memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin,
kNodeCopySize); kNodeCopySize);
bitSet = other.bitSet; bitSet = other.bitSet;
nextFree = other.nextFree;
memcpy(index, other.index, sizeof(index)); memcpy(index, other.index, sizeof(index));
memset(children, 0, sizeof(children)); memset(children, 0, sizeof(children));
const auto z = InternalVersionT::zero; const auto z = InternalVersionT::zero;
@@ -451,7 +449,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node256 &other) {
for (auto &v : childMaxVersion) { for (auto &v : childMaxVersion) {
v = z; v = z;
} }
nextFree = other.numChildren;
bitSet = other.bitSet; bitSet = other.bitSet;
int i = 0; int i = 0;
bitSet.forEachSet([&](int c) { bitSet.forEachSet([&](int c) {
@@ -1419,9 +1416,7 @@ Node *&getOrCreateChild(Node *&self, std::span<const uint8_t> &key,
insert48: insert48:
auto *self48 = static_cast<Node48 *>(self); auto *self48 = static_cast<Node48 *>(self);
self48->bitSet.set(index); self48->bitSet.set(index);
++self->numChildren; auto nextFree = self48->numChildren++;
assert(self48->nextFree < 48);
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]; auto &result = self48->children[nextFree];
@@ -1812,7 +1807,7 @@ Node *erase(Node *self, WriteContext *tls, ConflictSet::Impl *impl,
parent48->bitSet.reset(parentsIndex); parent48->bitSet.reset(parentsIndex);
int8_t toRemoveChildrenIndex = int8_t toRemoveChildrenIndex =
std::exchange(parent48->index[parentsIndex], -1); std::exchange(parent48->index[parentsIndex], -1);
int8_t lastChildrenIndex = --parent48->nextFree; auto lastChildrenIndex = --parent48->numChildren;
assert(toRemoveChildrenIndex >= 0); assert(toRemoveChildrenIndex >= 0);
assert(lastChildrenIndex >= 0); assert(lastChildrenIndex >= 0);
if (toRemoveChildrenIndex != lastChildrenIndex) { if (toRemoveChildrenIndex != lastChildrenIndex) {
@@ -1831,8 +1826,6 @@ Node *erase(Node *self, WriteContext *tls, ConflictSet::Impl *impl,
} }
parent48->childMaxVersion[lastChildrenIndex] = tls->zero; parent48->childMaxVersion[lastChildrenIndex] = tls->zero;
--parent->numChildren;
if (needsDownsize(parent48)) { if (needsDownsize(parent48)) {
downsize(parent48, tls, impl, result); downsize(parent48, tls, impl, result);
} }