From 7261c914928a38c06b1ae38e0f45d13276de4c70 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Tue, 20 Aug 2024 09:51:29 -0700 Subject: [PATCH] Remove Node48::nextFree, and improve padding to save 8 bytes --- ConflictSet.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/ConflictSet.cpp b/ConflictSet.cpp index ee09ed7..1604587 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -264,17 +264,17 @@ struct Node16 : Node { struct Node48 : Node { constexpr static auto kType = Type_Node48; 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 kMaxOfMaxShift = std::countr_zero(uint32_t(kMaxOfMaxPageSize)); constexpr static int kMaxOfMaxTotalPages = kMaxNodes / kMaxOfMaxPageSize; + + BitSet bitSet; + Node *children[kMaxNodes]; + InternalVersionT childMaxVersion[kMaxNodes]; InternalVersionT maxOfMax[kMaxOfMaxTotalPages]; + uint8_t reverseIndex[kMaxNodes]; + int8_t index[256]; uint8_t *partialKey() { return (uint8_t *)(this + 1); } @@ -404,7 +404,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node16 &other) { } memcpy(partialKey(), &other + 1, partialKeyLen); bitSet.init(); - nextFree = Node16::kMaxNodes; int i = 0; for (auto x : other.index) { bitSet.set(x); @@ -424,7 +423,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node48 &other) { memcpy((char *)this + kNodeCopyBegin, (char *)&other + kNodeCopyBegin, kNodeCopySize); bitSet = other.bitSet; - nextFree = other.nextFree; memcpy(index, other.index, sizeof(index)); memset(children, 0, sizeof(children)); const auto z = InternalVersionT::zero; @@ -451,7 +449,6 @@ inline void Node48::copyChildrenAndKeyFrom(const Node256 &other) { for (auto &v : childMaxVersion) { v = z; } - nextFree = other.numChildren; bitSet = other.bitSet; int i = 0; bitSet.forEachSet([&](int c) { @@ -1419,9 +1416,7 @@ Node *&getOrCreateChild(Node *&self, std::span &key, insert48: auto *self48 = static_cast(self); self48->bitSet.set(index); - ++self->numChildren; - assert(self48->nextFree < 48); - int nextFree = self48->nextFree++; + auto nextFree = self48->numChildren++; self48->index[index] = nextFree; self48->reverseIndex[nextFree] = index; auto &result = self48->children[nextFree]; @@ -1812,7 +1807,7 @@ Node *erase(Node *self, WriteContext *tls, ConflictSet::Impl *impl, parent48->bitSet.reset(parentsIndex); int8_t toRemoveChildrenIndex = std::exchange(parent48->index[parentsIndex], -1); - int8_t lastChildrenIndex = --parent48->nextFree; + auto lastChildrenIndex = --parent48->numChildren; assert(toRemoveChildrenIndex >= 0); assert(lastChildrenIndex >= 0); if (toRemoveChildrenIndex != lastChildrenIndex) { @@ -1831,8 +1826,6 @@ Node *erase(Node *self, WriteContext *tls, ConflictSet::Impl *impl, } parent48->childMaxVersion[lastChildrenIndex] = tls->zero; - --parent->numChildren; - if (needsDownsize(parent48)) { downsize(parent48, tls, impl, result); }