From 505c060a2809068fbcd6a1ba768d8d9422bb0741 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 22 Feb 2024 14:46:52 -0800 Subject: [PATCH] Use longestCommonPrefixPartialKey in insert --- ConflictSet.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/ConflictSet.cpp b/ConflictSet.cpp index f077080..ec635c9 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -1389,28 +1389,26 @@ bool checkRangeRead(Node *n, std::span begin, for (;;) { auto &self = *self_; // Handle an existing partial key - int partialKeyIndex = 0; - for (; partialKeyIndex < self->partialKeyLen; ++partialKeyIndex) { - if (partialKeyIndex == int(key.size()) || - self->partialKey[partialKeyIndex] != key[partialKeyIndex]) { - auto *old = self; - self = allocators->node4.allocate(); - self->maxVersion = old->maxVersion; - self->partialKeyLen = partialKeyIndex; - self->parent = old->parent; - self->parentsIndex = old->parentsIndex; - memcpy(self->partialKey, old->partialKey, partialKeyIndex); + int commonLen = std::min(self->partialKeyLen, key.size()); + int partialKeyIndex = + longestCommonPrefixPartialKey(self->partialKey, key.data(), commonLen); + if (partialKeyIndex < self->partialKeyLen) { + auto *old = self; + self = allocators->node4.allocate(); + self->maxVersion = old->maxVersion; + self->partialKeyLen = partialKeyIndex; + self->parent = old->parent; + self->parentsIndex = old->parentsIndex; + memcpy(self->partialKey, old->partialKey, partialKeyIndex); - getOrCreateChild(self, old->partialKey[partialKeyIndex], allocators) = - old; - old->parent = self; - old->parentsIndex = old->partialKey[partialKeyIndex]; + getOrCreateChild(self, old->partialKey[partialKeyIndex], allocators) = + old; + old->parent = self; + old->parentsIndex = old->partialKey[partialKeyIndex]; - memmove(old->partialKey, old->partialKey + partialKeyIndex + 1, - old->partialKeyLen - (partialKeyIndex + 1)); - old->partialKeyLen -= partialKeyIndex + 1; - break; - } + memmove(old->partialKey, old->partialKey + partialKeyIndex + 1, + old->partialKeyLen - (partialKeyIndex + 1)); + old->partialKeyLen -= partialKeyIndex + 1; } key = key.subspan(partialKeyIndex, key.size() - partialKeyIndex);