Avoid re-inserting begin if begin is not a prefix of end

This commit is contained in:
2024-03-11 22:43:52 -07:00
parent 1dcb380c73
commit 5e4eab55fb

View File

@@ -1577,7 +1577,7 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
// Returns a pointer to the newly inserted node. Caller must set
// `entryPresent`, `entry` fields and `maxVersion` on the result. The search
// path of the result's parent will have `maxVersion` at least `writeVersion` as
// a postcondition.
// a postcondition. Nodes along the search path may be invalidated.
template <bool kBegin>
[[nodiscard]] Node *insert(Node **self, std::span<const uint8_t> key,
int64_t writeVersion, NodeAllocators *allocators,
@@ -1704,6 +1704,7 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
return addPointWrite(root, oldestVersion, begin, writeVersion, allocators,
impl);
}
const bool beginIsPrefix = lcp == int(begin.size());
auto remaining = begin.subspan(0, lcp);
auto *n = root;
@@ -1772,8 +1773,9 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
}
endNode->entry.rangeVersion = writeVersion;
if (insertedEnd) {
// beginNode may have been invalidated
if (beginIsPrefix && insertedEnd) {
// beginNode may have been invalidated when inserting end. TODO can we do
// better?
beginNode = insert<true>(useAsRoot, begin, writeVersion, allocators, impl);
assert(beginNode->entryPresent);
}