Compare commits
5 Commits
b4b469a175
...
09cf807747
Author | SHA1 | Date | |
---|---|---|---|
09cf807747 | |||
051eb5919d | |||
ed5589e4ed | |||
a7b3d8fe4c | |||
c3a047fdf8 |
152
ConflictSet.cpp
152
ConflictSet.cpp
@@ -902,6 +902,8 @@ Node *&getChildExists(Node *self, uint8_t index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
InternalVersionT maxVersion(Node *n, ConflictSet::Impl *);
|
InternalVersionT maxVersion(Node *n, ConflictSet::Impl *);
|
||||||
|
InternalVersionT exchangeMaxVersion(Node *n, InternalVersionT newMax,
|
||||||
|
ConflictSet::Impl *);
|
||||||
|
|
||||||
void setMaxVersion(Node *n, ConflictSet::Impl *, InternalVersionT maxVersion);
|
void setMaxVersion(Node *n, ConflictSet::Impl *, InternalVersionT maxVersion);
|
||||||
|
|
||||||
@@ -1123,8 +1125,10 @@ Node *getFirstChildExists(Node *self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Caller is responsible for assigning a non-null pointer to the returned
|
// Caller is responsible for assigning a non-null pointer to the returned
|
||||||
// reference if null
|
// reference if null. Updates child's max version to `newMaxVersion` if child
|
||||||
Node *&getOrCreateChild(Node *&self, uint8_t index, WriteContext *tls) {
|
// exists but does not have a partial key.
|
||||||
|
Node *&getOrCreateChild(Node *&self, uint8_t index,
|
||||||
|
InternalVersionT newMaxVersion, WriteContext *tls) {
|
||||||
|
|
||||||
// Fast path for if it exists already
|
// Fast path for if it exists already
|
||||||
switch (self->getType()) {
|
switch (self->getType()) {
|
||||||
@@ -1134,6 +1138,9 @@ Node *&getOrCreateChild(Node *&self, uint8_t index, WriteContext *tls) {
|
|||||||
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) {
|
||||||
|
if (self3->children[i]->partialKeyLen == 0) {
|
||||||
|
self3->childMaxVersion[i] = newMaxVersion;
|
||||||
|
}
|
||||||
return self3->children[i];
|
return self3->children[i];
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
@@ -1141,6 +1148,9 @@ Node *&getOrCreateChild(Node *&self, uint8_t index, WriteContext *tls) {
|
|||||||
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) {
|
||||||
|
if (self16->children[i]->partialKeyLen == 0) {
|
||||||
|
self16->childMaxVersion[i] = newMaxVersion;
|
||||||
|
}
|
||||||
return self16->children[i];
|
return self16->children[i];
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
@@ -1148,12 +1158,23 @@ Node *&getOrCreateChild(Node *&self, uint8_t index, WriteContext *tls) {
|
|||||||
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) {
|
||||||
|
if (self48->children[secondIndex]->partialKeyLen == 0) {
|
||||||
|
self48->childMaxVersion[secondIndex] = newMaxVersion;
|
||||||
|
self48->maxOfMax[secondIndex >> Node48::kMaxOfMaxShift] =
|
||||||
|
std::max(self48->maxOfMax[secondIndex >> Node48::kMaxOfMaxShift],
|
||||||
|
newMaxVersion);
|
||||||
|
}
|
||||||
return self48->children[secondIndex];
|
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]; result != nullptr) {
|
if (auto &result = self256->children[index]; result != nullptr) {
|
||||||
|
if (self256->children[index]->partialKeyLen == 0) {
|
||||||
|
self256->childMaxVersion[index] = newMaxVersion;
|
||||||
|
self256->maxOfMax[index >> Node256::kMaxOfMaxShift] = std::max(
|
||||||
|
self256->maxOfMax[index >> Node256::kMaxOfMaxShift], newMaxVersion);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
@@ -2881,43 +2902,39 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
|
|||||||
InternalVersionT readVersion, ReadContext *);
|
InternalVersionT readVersion, ReadContext *);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Returns a pointer to the newly inserted node. Caller must set
|
// Consume the partial key of `self` (which must exist), and update `self` and
|
||||||
// `entryPresent`, and `entry` fields. The search path of the result will have
|
// `key` such that `self` is along the search path of `key`
|
||||||
// `maxVersion` at least `writeVersion` as a postcondition. Nodes along the
|
void consumePartialKey(Node *&self, std::span<const uint8_t> &key,
|
||||||
// search path to `key` may be invalidated.
|
|
||||||
[[nodiscard]] Node *insert(Node **self, std::span<const uint8_t> key,
|
|
||||||
InternalVersionT writeVersion, WriteContext *tls,
|
InternalVersionT writeVersion, WriteContext *tls,
|
||||||
ConflictSet::Impl *impl) {
|
ConflictSet::Impl *impl) {
|
||||||
|
assert(self->partialKeyLen > 0);
|
||||||
for (;; ++tls->accum.insert_iterations) {
|
|
||||||
|
|
||||||
if ((*self)->partialKeyLen > 0) {
|
|
||||||
// Handle an existing partial key
|
// Handle an existing partial key
|
||||||
int commonLen = std::min<int>((*self)->partialKeyLen, key.size());
|
int commonLen = std::min<int>(self->partialKeyLen, key.size());
|
||||||
int partialKeyIndex =
|
int partialKeyIndex =
|
||||||
longestCommonPrefix((*self)->partialKey(), key.data(), commonLen);
|
longestCommonPrefix(self->partialKey(), key.data(), commonLen);
|
||||||
if (partialKeyIndex < (*self)->partialKeyLen) {
|
if (partialKeyIndex < self->partialKeyLen) {
|
||||||
auto *old = *self;
|
auto *old = self;
|
||||||
InternalVersionT oldMaxVersion = maxVersion(old, impl);
|
InternalVersionT oldMaxVersion =
|
||||||
|
exchangeMaxVersion(old, writeVersion, impl);
|
||||||
|
|
||||||
// *self will have one child
|
// *self will have one child (old)
|
||||||
*self = tls->allocate<Node3>(partialKeyIndex);
|
auto *newSelf = tls->allocate<Node3>(partialKeyIndex);
|
||||||
|
|
||||||
memcpy((char *)*self + kNodeCopyBegin, (char *)old + kNodeCopyBegin,
|
newSelf->parent = old->parent;
|
||||||
kNodeCopySize);
|
newSelf->parentsIndex = old->parentsIndex;
|
||||||
(*self)->partialKeyLen = partialKeyIndex;
|
newSelf->partialKeyLen = partialKeyIndex;
|
||||||
|
newSelf->entryPresent = false;
|
||||||
|
newSelf->numChildren = 1;
|
||||||
|
|
||||||
// Not necessary to call removeKey here, since this node is "synthetic"
|
memcpy(newSelf->partialKey(), old->partialKey(), newSelf->partialKeyLen);
|
||||||
(*self)->entryPresent = false;
|
|
||||||
|
|
||||||
(*self)->numChildren = 0;
|
uint8_t oldDistinguishingByte = old->partialKey()[partialKeyIndex];
|
||||||
memcpy((*self)->partialKey(), old->partialKey(),
|
old->parent = newSelf;
|
||||||
(*self)->partialKeyLen);
|
old->parentsIndex = oldDistinguishingByte;
|
||||||
|
newSelf->index[0] = oldDistinguishingByte;
|
||||||
getOrCreateChild(*self, old->partialKey()[partialKeyIndex], tls) = old;
|
newSelf->children[0] = old;
|
||||||
old->parent = *self;
|
newSelf->childMaxVersion[0] = oldMaxVersion;
|
||||||
old->parentsIndex = old->partialKey()[partialKeyIndex];
|
self = newSelf;
|
||||||
setMaxVersion(old, impl, oldMaxVersion);
|
|
||||||
|
|
||||||
memmove(old->partialKey(), old->partialKey() + partialKeyIndex + 1,
|
memmove(old->partialKey(), old->partialKey() + partialKeyIndex + 1,
|
||||||
old->partialKeyLen - (partialKeyIndex + 1));
|
old->partialKeyLen - (partialKeyIndex + 1));
|
||||||
@@ -2928,38 +2945,50 @@ checkMaxBetweenExclusiveImpl<true>(Node *n, int begin, int end,
|
|||||||
// up.
|
// up.
|
||||||
}
|
}
|
||||||
key = key.subspan(partialKeyIndex, key.size() - partialKeyIndex);
|
key = key.subspan(partialKeyIndex, key.size() - partialKeyIndex);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
// Returns a pointer to the newly inserted node. Caller must set
|
||||||
// Consider adding a partial key
|
// `entryPresent`, and `entry` fields. All nodes along the search path of the
|
||||||
if ((*self)->numChildren == 0 && !(*self)->entryPresent) {
|
// result will have `maxVersion` set to `writeVersion` as a postcondition. Nodes
|
||||||
assert((*self)->getCapacity() >= int(key.size()));
|
// along the search path may be invalidated.
|
||||||
(*self)->partialKeyLen = key.size();
|
[[nodiscard]]
|
||||||
memcpy((*self)->partialKey(), key.data(), (*self)->partialKeyLen);
|
Node *insert(Node **self, std::span<const uint8_t> key,
|
||||||
key = key.subspan((*self)->partialKeyLen,
|
InternalVersionT writeVersion, WriteContext *tls,
|
||||||
key.size() - (*self)->partialKeyLen);
|
ConflictSet::Impl *impl) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ((*self)->partialKeyLen > 0) {
|
||||||
|
consumePartialKey(*self, key, writeVersion, tls, impl);
|
||||||
|
}
|
||||||
assert(maxVersion(*self, impl) <= writeVersion);
|
assert(maxVersion(*self, impl) <= writeVersion);
|
||||||
setMaxVersion(*self, impl, writeVersion);
|
setMaxVersion(*self, impl, writeVersion);
|
||||||
|
|
||||||
|
for (;; ++tls->accum.insert_iterations) {
|
||||||
|
|
||||||
if (key.size() == 0) {
|
if (key.size() == 0) {
|
||||||
return *self;
|
return *self;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &child = getOrCreateChild(*self, key.front(), tls);
|
auto &child = getOrCreateChild(*self, key.front(), writeVersion, tls);
|
||||||
if (!child) {
|
if (!child) {
|
||||||
child = tls->allocate<Node0>(key.size() - 1);
|
child = tls->allocate<Node0>(key.size() - 1);
|
||||||
child->numChildren = 0;
|
child->numChildren = 0;
|
||||||
child->entryPresent = false;
|
child->entryPresent = false;
|
||||||
child->partialKeyLen = 0;
|
child->partialKeyLen = key.size() - 1;
|
||||||
child->parent = *self;
|
child->parent = *self;
|
||||||
child->parentsIndex = key.front();
|
child->parentsIndex = key.front();
|
||||||
setMaxVersion(child, impl, tls->zero);
|
setMaxVersion(child, impl, writeVersion);
|
||||||
|
memcpy(child->partialKey(), key.data() + 1, child->partialKeyLen);
|
||||||
|
return child;
|
||||||
}
|
}
|
||||||
|
|
||||||
self = &child;
|
self = &child;
|
||||||
key = key.subspan(1, key.size() - 1);
|
key = key.subspan(1, key.size() - 1);
|
||||||
|
|
||||||
|
if ((*self)->partialKeyLen > 0) {
|
||||||
|
consumePartialKey(*self, key, writeVersion, tls, impl);
|
||||||
|
assert(maxVersion(*self, impl) <= writeVersion);
|
||||||
|
setMaxVersion(*self, impl, writeVersion);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3556,6 +3585,41 @@ InternalVersionT maxVersion(Node *n, ConflictSet::Impl *impl) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InternalVersionT exchangeMaxVersion(Node *n, InternalVersionT newMax,
|
||||||
|
ConflictSet::Impl *impl) {
|
||||||
|
int index = n->parentsIndex;
|
||||||
|
n = n->parent;
|
||||||
|
if (n == nullptr) {
|
||||||
|
return std::exchange(impl->rootMaxVersion, newMax);
|
||||||
|
}
|
||||||
|
switch (n->getType()) {
|
||||||
|
case Type_Node0: // GCOVR_EXCL_LINE
|
||||||
|
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||||
|
case Type_Node3: {
|
||||||
|
auto *n3 = static_cast<Node3 *>(n);
|
||||||
|
int i = getNodeIndex(n3, index);
|
||||||
|
return std::exchange(n3->childMaxVersion[i], newMax);
|
||||||
|
}
|
||||||
|
case Type_Node16: {
|
||||||
|
auto *n16 = static_cast<Node16 *>(n);
|
||||||
|
int i = getNodeIndex(n16, index);
|
||||||
|
return std::exchange(n16->childMaxVersion[i], newMax);
|
||||||
|
}
|
||||||
|
case Type_Node48: {
|
||||||
|
auto *n48 = static_cast<Node48 *>(n);
|
||||||
|
assert(n48->bitSet.test(index));
|
||||||
|
return std::exchange(n48->childMaxVersion[n48->index[index]], newMax);
|
||||||
|
}
|
||||||
|
case Type_Node256: {
|
||||||
|
auto *n256 = static_cast<Node256 *>(n);
|
||||||
|
assert(n256->bitSet.test(index));
|
||||||
|
return std::exchange(n256->childMaxVersion[index], newMax);
|
||||||
|
}
|
||||||
|
default: // GCOVR_EXCL_LINE
|
||||||
|
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setMaxVersion(Node *n, ConflictSet::Impl *impl, InternalVersionT newMax) {
|
void setMaxVersion(Node *n, ConflictSet::Impl *impl, InternalVersionT newMax) {
|
||||||
int index = n->parentsIndex;
|
int index = n->parentsIndex;
|
||||||
n = n->parent;
|
n = n->parent;
|
||||||
|
Reference in New Issue
Block a user