From 7ff00e78468c06b7fea12a4e7d606ff632fa6681 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 15 Aug 2024 11:40:52 -0700 Subject: [PATCH] Extract mergeWithChild to function --- ConflictSet.cpp | 82 ++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/ConflictSet.cpp b/ConflictSet.cpp index db74829..eed9c8a 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -1604,6 +1604,48 @@ void rezero(Node *n, InternalVersionT z) { } } +void mergeWithChild(Node *&self, WriteContext *tls, ConflictSet::Impl *impl, + Node *&dontInvalidate, Node3 *self3) { + auto *child = self3->children[0]; + int minCapacity = self3->partialKeyLen + 1 + child->partialKeyLen; + + if (minCapacity > child->getCapacity()) { + const bool update = child == dontInvalidate; + freeAndMakeCapacityAtLeast(child, minCapacity, tls, impl, true); + if (update) { + dontInvalidate = child; + } + } + + // Merge partial key with child +#if DEBUG_VERBOSE && !defined(NDEBUG) + fprintf(stderr, "Merge %s into %s\n", getSearchPathPrintable(self).c_str(), + getSearchPathPrintable(child).c_str()); +#endif + + InternalVersionT childMaxVersion = maxVersion(child, impl); + + // Construct new partial key for child + memmove(child->partialKey() + self3->partialKeyLen + 1, child->partialKey(), + child->partialKeyLen); + memcpy(child->partialKey(), self3->partialKey(), self->partialKeyLen); + child->partialKey()[self3->partialKeyLen] = self3->index[0]; + child->partialKeyLen += 1 + self3->partialKeyLen; + + child->parent = self->parent; + child->parentsIndex = self->parentsIndex; + + // Max versions are stored in the parent, so we need to update it now + // that we have a new parent. + setMaxVersion(child, impl, childMaxVersion); + if (child->parent) { + rezero(child->parent, tls->zero); + } + + getInTree(self, impl) = child; + tls->release(self3); +} + void maybeDownsize(Node *self, WriteContext *tls, ConflictSet::Impl *impl, Node *&dontInvalidate) { @@ -1623,45 +1665,7 @@ void maybeDownsize(Node *self, WriteContext *tls, ConflictSet::Impl *impl, getInTree(self, impl) = newSelf; tls->release(self3); } else if (self->numChildren == 1 && !self->entryPresent) { - auto *child = self3->children[0]; - int minCapacity = self3->partialKeyLen + 1 + child->partialKeyLen; - - if (minCapacity > child->getCapacity()) { - const bool update = child == dontInvalidate; - freeAndMakeCapacityAtLeast(child, minCapacity, tls, impl, true); - if (update) { - dontInvalidate = child; - } - } - - // Merge partial key with child -#if DEBUG_VERBOSE && !defined(NDEBUG) - fprintf(stderr, "Merge %s into %s\n", - getSearchPathPrintable(self).c_str(), - getSearchPathPrintable(child).c_str()); -#endif - - InternalVersionT childMaxVersion = maxVersion(child, impl); - - // Construct new partial key for child - memmove(child->partialKey() + self3->partialKeyLen + 1, - child->partialKey(), child->partialKeyLen); - memcpy(child->partialKey(), self3->partialKey(), self->partialKeyLen); - child->partialKey()[self3->partialKeyLen] = self3->index[0]; - child->partialKeyLen += 1 + self3->partialKeyLen; - - child->parent = self->parent; - child->parentsIndex = self->parentsIndex; - - // Max versions are stored in the parent, so we need to update it now - // that we have a new parent. - setMaxVersion(child, impl, childMaxVersion); - if (child->parent) { - rezero(child->parent, tls->zero); - } - - getInTree(self, impl) = child; - tls->release(self3); + mergeWithChild(self, tls, impl, dontInvalidate, self3); } } break; case Type_Node16: