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,9 +656,22 @@ 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 (;;) {
Node *n;
std::span<const uint8_t> remaining;
StepwiseLastLeq(Node *n, const std::span<const uint8_t> key)
: n(n), remaining(key) {}
int cmp;
enum Phase { Search, ScanBackward, DownRightSpine };
Phase 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) {
@@ -668,33 +681,33 @@ Iterator lastLeq(Node *n, const std::span<const uint8_t> key) {
} }
if (c > 0) { if (c > 0) {
n = prevPhysical(n); n = prevPhysical(n);
goto scanBackward; phase = ScanBackward;
return false;
} else { } else {
goto downRightSpine; phase = DownRightSpine;
return false;
} }
} }
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
n = prevPhysical(n); n = prevPhysical(n);
goto scanBackward; phase = 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) { if (remaining.size() == 0) {
// We've found the physical node corresponding to search path `key` // We've found the physical node corresponding to search path `key`
if (n->entryPresent) { if (n->entryPresent) {
return {n, 0}; cmp = 0;
return true;
} else { } else {
goto scanBackward; phase = ScanBackward;
return false;
} }
} else { } else {
int c = getChildLeq(n, remaining[0]); int c = getChildLeq(n, remaining[0]);
@@ -704,32 +717,48 @@ Iterator lastLeq(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 downRightSpine; phase = DownRightSpine;
return false;
} else { } else {
goto scanBackward; phase = ScanBackward;
return false;
} }
} }
} }
} return false;
downRightSpine: case DownRightSpine:
// The physical node corresponding to search path `key` does not // The physical node corresponding to search path `key` does not
// exist. Let's find the physical node corresponding to the highest // exist. Let's find the physical node corresponding to the highest
// search key (not necessarily present) less than key. // search key (not necessarily present) less than key.
// Move down the right spine // Move down the right spine
for (;;) { {
int c = getChildLeq(n, 255); int c = getChildLeq(n, 255);
if (c >= 0) { if (c >= 0) {
n = getChildExists(n, c); n = getChildExists(n, c);
} else { } else {
goto scanBackward; phase = ScanBackward;
} }
return false;
} }
scanBackward: case ScanBackward:
// Iterate backwards along existing physical nodes until we find a present // Iterate backwards along existing physical nodes until we find a present
// entry // entry
for (; !n->entryPresent; n = prevPhysical(n)) { if (!n->entryPresent) {
n = prevPhysical(n);
return false;
} }
return {n, -1}; cmp = -1;
return true;
}
__builtin_unreachable(); // GCOVR_EXCL_LINE
}
};
Iterator lastLeq(Node *n, const std::span<const uint8_t> key) {
StepwiseLastLeq l{n, key};
while (!l.step())
;
return {l.n, l.cmp};
} }
// 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() {}
}
auto [l, c] = CheckStepWise(Node *root, ConflictSet::Result *result,
lastLeq(root, std::span<const uint8_t>(r.begin.p, r.begin.len)); const ConflictSet::ReadRange *r)
#if DEBUG_VERBOSE && !defined(NDEBUG) : result(result), read(r),
Arena arena; left(root, std::span<const uint8_t>(r->begin.p, r->begin.len)),
fprintf(stderr, "LastLeq for `%s' got `%s'\n", printable(r.begin).c_str(), right(root, std::span<const uint8_t>(r->end.p, r->end.len)),
printable(getSearchPath(arena, l)).c_str()); phase(r->end.len == 0 ? PointRead : RangeRead) {}
#endif
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 != nullptr);
assert(l->entryPresent); assert(l->entryPresent);
result[i] = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) > *result = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) >
r.readVersion read->readVersion
? Conflict ? ConflictSet::Conflict
: Commit; : ConflictSet::Commit;
if (result[i] == Commit && r.end.len > 0) { return true;
auto [e, c] = }
lastLeq(root, std::span<const uint8_t>(r.end.p, r.end.len)); return false;
#if DEBUG_VERBOSE && !defined(NDEBUG) case RangeRead: {
Arena arena; while (!left.step())
fprintf(stderr, "LastLeq for `%s' got `%s'\n", printable(r.end).c_str(), ;
printable(getSearchPath(arena, e)).c_str()); while (!right.step())
#endif ;
auto *l = left.n;
auto c = left.cmp;
*result = (c == 0 ? l->entry.pointVersion : l->entry.rangeVersion) >
read->readVersion
? ConflictSet::Conflict
: ConflictSet::Commit;
if (*result == ConflictSet::Commit) {
auto *e = right.n;
auto c = right.cmp;
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];