Short-circuiting, efficient checkRangeRead

This commit is contained in:
2024-02-07 16:31:54 -08:00
parent 9363d7866c
commit 2cbb6e0170

View File

@@ -680,10 +680,21 @@ std::string getSearchPath(Node *n) {
} }
} // namespace } // namespace
Iterator firstGeq(Node *n, const std::span<const uint8_t> key) { struct FirstGeqStepwise {
auto remaining = key; Node *n;
std::span<const uint8_t> remaining;
Node *nextSib = nullptr; Node *nextSib = nullptr;
for (;;) { int cmp;
enum Phase { Search, DownLeftSpine };
Phase phase;
FirstGeqStepwise(Node *n, std::span<const uint8_t> remaining)
: n(n), remaining(remaining), phase(Search) {}
bool step() {
switch (phase) {
case Search:
if (n->partialKeyLen > 0) { if (n->partialKeyLen > 0) {
int commonLen = std::min<int>(n->partialKeyLen, remaining.size()); int commonLen = std::min<int>(n->partialKeyLen, remaining.size());
for (int i = 0; i < commonLen; ++i) { for (int i = 0; i < commonLen; ++i) {
@@ -692,29 +703,31 @@ Iterator firstGeq(Node *n, const std::span<const uint8_t> key) {
continue; continue;
} }
if (c > 0) { if (c > 0) {
goto downLeftSpine; return downLeftSpine();
} else { } else {
n = nextSib; n = nextSib;
goto downLeftSpine; return downLeftSpine();
} }
} }
if (commonLen == n->partialKeyLen) { if (commonLen == n->partialKeyLen) {
// partial key matches // partial key matches
remaining = remaining.subspan(commonLen, remaining.size() - commonLen); remaining =
remaining.subspan(commonLen, remaining.size() - commonLen);
} else if (n->partialKeyLen > int(remaining.size())) { } else if (n->partialKeyLen > int(remaining.size())) {
// n is the first physical node greater than remaining, and there's no // n is the first physical node greater than remaining, and there's no
// eq node // eq node
goto downLeftSpine; return downLeftSpine();
} }
} }
if (remaining.size() == 0) { if (remaining.size() == 0) {
if (n->entryPresent) { if (n->entryPresent) {
return {n, 0}; cmp = 0;
return true;
} }
int c = getChildGeq(n, 0); int c = getChildGeq(n, 0);
assert(c >= 0); assert(c >= 0);
n = getChildExists(n, c); n = getChildExists(n, c);
goto downLeftSpine; return downLeftSpine();
} else { } else {
int c = getChildGeq(n, remaining[0]); int c = getChildGeq(n, remaining[0]);
int c2 = getChildGeq(n, int(remaining[0]) + 1); int c2 = getChildGeq(n, int(remaining[0]) + 1);
@@ -727,26 +740,41 @@ Iterator firstGeq(Node *n, const std::span<const uint8_t> key) {
} else { } else {
if (c >= 0) { if (c >= 0) {
n = getChildExists(n, c); n = getChildExists(n, c);
goto downLeftSpine; return downLeftSpine();
} else { } else {
n = nextSib; n = nextSib;
goto downLeftSpine; return downLeftSpine();
} }
} }
} }
} return false;
downLeftSpine: case DownLeftSpine:
if (n == nullptr) {
return {nullptr, 1};
}
for (;;) {
if (n->entryPresent) { if (n->entryPresent) {
return {n, 1}; cmp = 1;
return true;
} }
int c = getChildGeq(n, 0); int c = getChildGeq(n, 0);
assert(c >= 0); assert(c >= 0);
n = getChildExists(n, c); n = getChildExists(n, c);
return false;
} }
}
bool downLeftSpine() {
if (n == nullptr) {
cmp = 1;
return true;
}
phase = DownLeftSpine;
return step();
}
};
Iterator firstGeq(Node *n, const std::span<const uint8_t> key) {
FirstGeqStepwise stepwise{n, key};
while (!stepwise.step())
;
return {stepwise.n, stepwise.cmp};
} }
Iterator firstGeq(Node *n, std::string_view key) { Iterator firstGeq(Node *n, std::string_view key) {
@@ -832,84 +860,82 @@ downLeftSpine:
bool checkRangeRead(Node *n, const std::span<const uint8_t> begin, bool checkRangeRead(Node *n, const std::span<const uint8_t> begin,
const std::span<const uint8_t> end, int64_t readVersion) { const std::span<const uint8_t> end, int64_t readVersion) {
auto left = firstGeq(n, begin); auto left = FirstGeqStepwise{n, begin};
auto right = firstGeq(n, end); auto right = FirstGeqStepwise{n, end};
bool leftDone;
Arena arena; bool rightDone;
auto leftPath = vector<Node *>(arena); for (;;) {
auto rightPath = vector<Node *>(arena); if (left.phase == FirstGeqStepwise::Search &&
for (auto *iter = left.n; iter != nullptr; iter = iter->parent) { right.phase == FirstGeqStepwise::Search &&
leftPath.push_back(iter); left.n->maxVersion <= readVersion) {
}
for (auto *iter = right.n; iter != nullptr; iter = iter->parent) {
rightPath.push_back(iter);
}
Node *lca = n;
for (int i = 0; int(leftPath.size()) - 1 - i >= 0 &&
int(rightPath.size()) - 1 - i >= 0 &&
leftPath[int(leftPath.size()) - 1 - i] ==
rightPath[(rightPath.size()) - 1 - i];
++i) {
lca = leftPath[int(leftPath.size()) - 1 - i];
}
#if DEBUG_VERBOSE && !defined(NDEBUG)
fprintf(stderr, "firstGeq for `%s' got `%s'\n", printable(begin).c_str(),
getSearchPathPrintable(left.n).c_str());
fprintf(stderr, "firstGeq for `%s' got `%s'\n", printable(end).c_str(),
getSearchPathPrintable(right.n).c_str());
fprintf(stderr, "lca `%s'\n", getSearchPathPrintable(lca).c_str());
#endif
if (left.n != nullptr && left.cmp != 0 &&
left.n->entry.rangeVersion > readVersion) {
return false;
}
if (left.n == right.n) {
return true; return true;
} }
assert(left.n != nullptr); leftDone = left.step();
auto boundaryVersion = left.n->entry.pointVersion; rightDone = right.step();
if (left.cmp != 0) { if (leftDone || rightDone) {
boundaryVersion = std::max(boundaryVersion, left.n->entry.rangeVersion); break;
} }
if (right.n != nullptr) { if (left.n != right.n) {
boundaryVersion = std::max(boundaryVersion, right.n->entry.rangeVersion); break;
} }
if (boundaryVersion > readVersion) {
return false;
} }
if (left.n != lca) { if (!leftDone && !rightDone) {
while (left.n->parent != lca) { assert(left.n->parent == right.n->parent);
for (int c = getChildGeq(left.n->parent, int(left.n->parentsIndex) + 1); for (int c = left.n->parentsIndex; c < right.n->parentsIndex;
c >= 0; c = getChildGeq(left.n->parent, c + 1)) { c = getChildGeq(left.n->parent, c + 1)) {
assert(c >= 0);
if (getChildExists(left.n->parent, c)->maxVersion > readVersion) { if (getChildExists(left.n->parent, c)->maxVersion > readVersion) {
return false; return false;
} }
} }
left.n = left.n->parent; // We've checked everything from begin to the search path of right.n
} // Now check from the search path of right.n to end
} for (;;) {
if (right.n != nullptr && right.n != lca) { if (right.phase == FirstGeqStepwise::Search &&
while (right.n->parent != lca) { right.n->maxVersion <= readVersion) {
for (int c = getChildLeq(right.n->parent, int(right.n->parentsIndex) - 1);
c >= 0; c = getChildLeq(right.n->parent, c - 1)) {
if (getChildExists(right.n->parent, c)->maxVersion > readVersion) {
return false;
}
}
right.n = right.n->parent;
}
}
for (int c = left.n != lca ? getChildGeq(lca, int(left.n->parentsIndex) + 1)
: getChildGeq(lca, 0);
c >= 0 && (right.n == nullptr || c < right.n->parentsIndex);
c = getChildGeq(lca, c + 1)) {
if (getChildExists(lca, c)->maxVersion > readVersion) {
return false;
}
}
return true; return true;
}
rightDone = right.step();
if (rightDone) {
return right.n->entry.rangeVersion <= readVersion;
}
}
}
if (leftDone) {
if (left.n == nullptr) {
return true;
}
if (right.phase == FirstGeqStepwise::DownLeftSpine) {
if (left.n->maxVersion > readVersion) {
return false;
}
}
// We've checked everything from begin to the search path of right.n
// Now check from the search path of right.n to end
for (;;) {
if (right.phase == FirstGeqStepwise::Search &&
right.n->maxVersion <= readVersion) {
return true;
}
rightDone = right.step();
if (rightDone) {
return right.n->entry.rangeVersion <= readVersion;
}
}
}
if (rightDone) {
// This would mean that left is overtaking right
__builtin_unreachable();
}
{
assert(left.n == right.n);
return true;
}
} }
// Returns a pointer to the newly inserted node. caller is reponsible for // Returns a pointer to the newly inserted node. caller is reponsible for