Interleave checks for point reads

This doesn't actually seem faster, but it should prepare us to implement
range reads non-naively. It probably should be faster. To be
investigated.
This commit is contained in:
2024-02-05 16:44:57 -08:00
parent 57ec97f2ee
commit d78afe0823

View File

@@ -656,80 +656,109 @@ std::string_view getSearchPath(Arena &arena, Node *n) {
} }
} }
Iterator lastLeq(Node *n, const std::span<const uint8_t> key) { struct StepwiseLastLeq {
auto remaining = key; StepwiseLastLeq() {}
for (;;) {
if (n->partialKeyLen > 0) { Node *n;
int commonLen = std::min<int>(n->partialKeyLen, remaining.size()); std::span<const uint8_t> remaining;
for (int i = 0; i < commonLen; ++i) { StepwiseLastLeq(Node *n, const std::span<const uint8_t> key)
auto c = n->partialKey[i] <=> remaining[i]; : n(n), remaining(key) {}
if (c == 0) {
continue; int cmp;
enum Phase { Search, ScanBackward, DownRightSpine };
Phase phase = Search;
bool step() {
switch (phase) {
case Search:
if (n->partialKeyLen > 0) {
int commonLen = std::min<int>(n->partialKeyLen, remaining.size());
for (int i = 0; i < commonLen; ++i) {
auto c = n->partialKey[i] <=> remaining[i];
if (c == 0) {
continue;
}
if (c > 0) {
n = prevPhysical(n);
phase = ScanBackward;
return false;
} else {
phase = DownRightSpine;
return false;
}
} }
if (c > 0) { if (commonLen == n->partialKeyLen) {
// partial key matches
remaining =
remaining.subspan(commonLen, remaining.size() - commonLen);
} else if (n->partialKeyLen > int(remaining.size())) {
// n is the first physical node greater than remaining, and there's no
// eq node
n = prevPhysical(n); n = prevPhysical(n);
goto scanBackward; phase = ScanBackward;
} else { return false;
goto downRightSpine;
} }
} }
if (commonLen == n->partialKeyLen) { if (remaining.size() == 0) {
// partial key matches // We've found the physical node corresponding to search path `key`
remaining = remaining.subspan(commonLen, remaining.size() - commonLen); if (n->entryPresent) {
} else if (n->partialKeyLen > int(remaining.size())) { cmp = 0;
// n is the first physical node greater than remaining, and there's no return true;
// eq node } else {
n = prevPhysical(n); phase = ScanBackward;
goto scanBackward; return false;
} }
}
{
Arena arena;
assert((std::string(getSearchPath(arena, n)) +
std::string((const char *)remaining.data(), remaining.size()))
.ends_with(std::string((const char *)key.data(), key.size())));
}
if (remaining.size() == 0) {
// We've found the physical node corresponding to search path `key`
if (n->entryPresent) {
return {n, 0};
} else { } else {
goto scanBackward; int c = getChildLeq(n, remaining[0]);
if (c == remaining[0]) {
n = getChildExists(n, c);
remaining = remaining.subspan(1, remaining.size() - 1);
} else {
if (c >= 0) {
n = getChildExists(n, c);
phase = DownRightSpine;
return false;
} else {
phase = ScanBackward;
return false;
}
}
} }
} else { return false;
int c = getChildLeq(n, remaining[0]); case DownRightSpine:
if (c == remaining[0]) { // The physical node corresponding to search path `key` does not
n = getChildExists(n, c); // exist. Let's find the physical node corresponding to the highest
remaining = remaining.subspan(1, remaining.size() - 1); // search key (not necessarily present) less than key.
} else { // Move down the right spine
{
int c = getChildLeq(n, 255);
if (c >= 0) { if (c >= 0) {
n = getChildExists(n, c); n = getChildExists(n, c);
goto downRightSpine;
} else { } else {
goto scanBackward; phase = ScanBackward;
} }
return false;
} }
case ScanBackward:
// Iterate backwards along existing physical nodes until we find a present
// entry
if (!n->entryPresent) {
n = prevPhysical(n);
return false;
}
cmp = -1;
return true;
} }
__builtin_unreachable(); // GCOVR_EXCL_LINE
} }
downRightSpine: };
// The physical node corresponding to search path `key` does not
// exist. Let's find the physical node corresponding to the highest Iterator lastLeq(Node *n, const std::span<const uint8_t> key) {
// search key (not necessarily present) less than key. StepwiseLastLeq l{n, key};
// Move down the right spine while (!l.step())
for (;;) { ;
int c = getChildLeq(n, 255); return {l.n, l.cmp};
if (c >= 0) {
n = getChildExists(n, c);
} else {
goto scanBackward;
}
}
scanBackward:
// Iterate backwards along existing physical nodes until we find a present
// entry
for (; !n->entryPresent; n = prevPhysical(n)) {
}
return {n, -1};
} }
// Returns a pointer to the newly inserted node. caller is reponsible for // Returns a pointer to the newly inserted node. caller is reponsible for
@@ -807,51 +836,94 @@ void destroyTree(Node *root) {
} }
} }
struct __attribute__((visibility("hidden"))) ConflictSet::Impl { struct CheckStepWise {
void check(const ReadRange *reads, Result *result, int count) const { ConflictSet::Result *result;
for (int i = 0; i < count; ++i) { const ConflictSet::ReadRange *read;
const auto &r = reads[i]; StepwiseLastLeq left;
if (r.readVersion < oldestVersion) { StepwiseLastLeq right;
result[i] = TooOld;
continue; CheckStepWise() {}
CheckStepWise(Node *root, ConflictSet::Result *result,
const ConflictSet::ReadRange *r)
: result(result), read(r),
left(root, std::span<const uint8_t>(r->begin.p, r->begin.len)),
right(root, std::span<const uint8_t>(r->end.p, r->end.len)),
phase(r->end.len == 0 ? PointRead : RangeRead) {}
enum Phase {
PointRead,
RangeRead,
};
Phase phase;
bool step() {
switch (phase) {
case PointRead:
if (left.step()) {
auto *l = left.n;
int c = left.cmp;
assert(l != nullptr);
assert(l->entryPresent);
*result = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) >
read->readVersion
? ConflictSet::Conflict
: ConflictSet::Commit;
return true;
} }
auto [l, c] = return false;
lastLeq(root, std::span<const uint8_t>(r.begin.p, r.begin.len)); case RangeRead: {
#if DEBUG_VERBOSE && !defined(NDEBUG) while (!left.step())
Arena arena; ;
fprintf(stderr, "LastLeq for `%s' got `%s'\n", printable(r.begin).c_str(), while (!right.step())
printable(getSearchPath(arena, l)).c_str()); ;
#endif auto *l = left.n;
assert(l != nullptr); auto c = left.cmp;
assert(l->entryPresent); *result = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) >
result[i] = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) > read->readVersion
r.readVersion ? ConflictSet::Conflict
? Conflict : ConflictSet::Commit;
: Commit; if (*result == ConflictSet::Commit) {
if (result[i] == Commit && r.end.len > 0) { auto *e = right.n;
auto [e, c] = auto c = right.cmp;
lastLeq(root, std::span<const uint8_t>(r.end.p, r.end.len));
#if DEBUG_VERBOSE && !defined(NDEBUG)
Arena arena;
fprintf(stderr, "LastLeq for `%s' got `%s'\n", printable(r.end).c_str(),
printable(getSearchPath(arena, e)).c_str());
#endif
if (l == e) { if (l == e) {
continue; return true;
} }
if (c != 0) { if (c != 0) {
e = nextLogical(e); e = nextLogical(e);
} }
for (auto iter = nextLogical(l); iter != e; iter = nextLogical(iter)) { for (auto iter = nextLogical(l); iter != e; iter = nextLogical(iter)) {
if (iter->entry.pointVersion > r.readVersion || if (iter->entry.pointVersion > read->readVersion ||
iter->entry.rangeVersion > r.readVersion) { iter->entry.rangeVersion > read->readVersion) {
result[i] = Conflict; *result = ConflictSet::Conflict;
break; return true;
} }
} }
} }
return true;
} }
}
__builtin_unreachable(); // GCOVR_EXCL_LINE
} }
};
struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
void check(const ReadRange *reads, Result *result, int count) const {
Arena arena;
CheckStepWise *checks = new (arena) CheckStepWise[count];
int index = 0;
for (int i = 0; i < count; ++i) {
if (reads[i].readVersion < oldestVersion) {
result[i] = TooOld;
} else {
checks[index++] = CheckStepWise(root, result + i, reads + i);
}
}
runInterleaved(std::span<CheckStepWise>(checks, index));
}
void addWrites(const WriteRange *writes, int count) { void addWrites(const WriteRange *writes, int count) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
const auto &w = writes[i]; const auto &w = writes[i];