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