Remove call to memcmp for partial keys

This commit is contained in:
2024-01-31 14:32:25 -08:00
parent e84ea10d20
commit bafe1edfa4

View File

@@ -37,10 +37,10 @@ struct Node {
Node *parent = nullptr;
int64_t maxVersion = std::numeric_limits<int64_t>::lowest();
Entry entry;
constexpr static auto kPartialKeyMaxLen = 18;
int16_t numChildren = 0;
bool entryPresent = false;
uint8_t parentsIndex = 0;
constexpr static auto kPartialKeyMaxLen = 10;
uint8_t partialKey[kPartialKeyMaxLen];
int8_t partialKeyLen = 0;
/* end section that's copied to the next node */
@@ -748,33 +748,40 @@ std::string_view getSearchPath(Arena &arena, Node *n) {
Iterator lastLeq(Node *n, const std::span<const uint8_t> key) {
auto remaining = key;
for (;;) {
Arena arena;
if (n->partialKeyLen > 0) {
int commonLen = std::min<int>(n->partialKeyLen, remaining.size());
if (commonLen > Node::kPartialKeyMaxLen) {
__builtin_unreachable();
for (int i = 0; i < commonLen; ++i) {
auto c = n->partialKey[i] <=> remaining[i];
if (c == 0) {
continue;
}
int c = memcmp(n->partialKey, remaining.data(), commonLen);
if (c == 0 && commonLen == n->partialKeyLen) {
if (c > 0) {
n = prevPhysical(n);
}
goto outerLoop;
}
if (commonLen == n->partialKeyLen) {
// partial key matches
remaining = remaining.subspan(commonLen, remaining.size() - commonLen);
} else if (c < 0 || (c == 0 && n->partialKeyLen < int(remaining.size()))) {
// n is the last physical node less than remaining, and there's no eq node
break;
} else if (c > 0 || (c == 0 && n->partialKeyLen > int(remaining.size()))) {
// n is the first physical node greater than remaining, and there's no eq
// node
} 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);
break;
goto outerLoop;
}
}
{
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 {
break;
goto outerLoop;
}
} else {
int c = getChildLeq(n, remaining[0]);
@@ -790,14 +797,14 @@ Iterator lastLeq(Node *n, const std::span<const uint8_t> key) {
if (c >= 0) {
n = getChildExists(n, c);
} else {
break;
goto outerLoop;
}
c = getChildLeq(n, 255);
}
break;
}
}
}
outerLoop:
// Iterate backwards along existing physical nodes until we find a present
// entry
for (; !n->entryPresent; n = prevPhysical(n)) {