Use entry bytes in partial key if entry not present

Closes #8
This commit is contained in:
2024-03-07 13:33:39 -08:00
parent d1a6b293e9
commit 5e1fb1dac5
2 changed files with 25 additions and 14 deletions

View File

@@ -227,23 +227,28 @@ enum class Type : int8_t {
Node256, Node256,
Invalid, Invalid,
}; };
constexpr static int kPartialKeyMaxLenEntryPresent = 24;
struct Node { struct Node {
/* begin section that's copied to the next node */ /* begin section that's copied to the next node */
Node *parent = nullptr; Node *parent = nullptr;
// The max write version over all keys that start with the search path up to uint8_t partialKey[kPartialKeyMaxLenEntryPresent];
// this point // If not entryPresent, then the partial key might spill over into entry
Entry entry; Entry entry;
int16_t numChildren = 0; int32_t numChildren = 0;
bool entryPresent = false; bool entryPresent = false;
uint8_t parentsIndex = 0; uint8_t parentsIndex = 0;
constexpr static auto kPartialKeyMaxLen = 26;
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 */
Type type = Type::Invalid; Type type = Type::Invalid;
}; };
static_assert(offsetof(Node, entry) ==
offsetof(Node, partialKey) + kPartialKeyMaxLenEntryPresent);
static_assert(std::is_pod_v<Entry>);
struct Child { struct Child {
int64_t childMaxVersion; int64_t childMaxVersion;
Node *child; Node *child;
@@ -888,7 +893,7 @@ bytes:
int longestCommonPrefixPartialKey(const uint8_t *ap, const uint8_t *bp, int longestCommonPrefixPartialKey(const uint8_t *ap, const uint8_t *bp,
int cl) { int cl) {
assert(cl <= Node::kPartialKeyMaxLen); assert(cl <= kPartialKeyMaxLenEntryPresent + int(sizeof(Entry)));
int i = 0; int i = 0;
for (; i < cl; ++i) { for (; i < cl; ++i) {
if (*ap++ != *bp++) { if (*ap++ != *bp++) {
@@ -1515,10 +1520,10 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
return checkRangeLeftSide.ok & checkRangeRightSide.ok; return checkRangeLeftSide.ok & checkRangeRightSide.ok;
} }
// Returns a pointer to the newly inserted node. caller is reponsible for // Returns a pointer to the newly inserted node. Caller must set
// setting 'entry' fields and `maxVersion` on the result, which may have // `entryPresent`, `entry` fields and `maxVersion` on the result. The search
// !entryPresent. The search path of the result's parent will have // path of the result's parent will have `maxVersion` at least `writeVersion` as
// `maxVersion` at least `writeVersion` as a postcondition. // a postcondition.
template <bool kBegin> template <bool kBegin>
[[nodiscard]] Node *insert(Node **self, std::span<const uint8_t> key, [[nodiscard]] Node *insert(Node **self, std::span<const uint8_t> key,
int64_t writeVersion, NodeAllocators *allocators, int64_t writeVersion, NodeAllocators *allocators,
@@ -1557,8 +1562,12 @@ template <bool kBegin>
} else { } else {
// Consider adding a partial key // Consider adding a partial key
if ((*self)->numChildren == 0 && !(*self)->entryPresent) { if ((*self)->numChildren == 0 && !(*self)->entryPresent) {
(*self)->partialKeyLen = const bool willNotBePresent =
std::min<int>(key.size(), (*self)->kPartialKeyMaxLen); key.size() > kPartialKeyMaxLenEntryPresent + int(sizeof(Entry));
(*self)->partialKeyLen = std::min<int>(
key.size(), willNotBePresent
? kPartialKeyMaxLenEntryPresent + int(sizeof(Entry))
: kPartialKeyMaxLenEntryPresent);
memcpy((*self)->partialKey, key.data(), (*self)->partialKeyLen); memcpy((*self)->partialKey, key.data(), (*self)->partialKeyLen);
key = key.subspan((*self)->partialKeyLen, key = key.subspan((*self)->partialKeyLen,
key.size() - (*self)->partialKeyLen); key.size() - (*self)->partialKeyLen);
@@ -1713,6 +1722,7 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
if (insertedEnd) { if (insertedEnd) {
// beginNode may have been invalidated // beginNode may have been invalidated
beginNode = insert<true>(useAsRoot, begin, writeVersion, allocators, impl); beginNode = insert<true>(useAsRoot, begin, writeVersion, allocators, impl);
assert(beginNode->entryPresent);
} }
for (beginNode = nextLogical(beginNode); beginNode != endNode;) { for (beginNode = nextLogical(beginNode); beginNode != endNode;) {
@@ -2280,7 +2290,8 @@ int main(void) {
#ifdef ENABLE_FUZZ #ifdef ENABLE_FUZZ
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
TestDriver<ConflictSet::Impl> driver{data, size}; TestDriver<ConflictSet::Impl> driver{data, size};
static_assert(driver.kMaxKeyLen > Node::kPartialKeyMaxLen); static_assert(driver.kMaxKeyLen >
kPartialKeyMaxLenEntryPresent + sizeof(Entry));
for (;;) { for (;;) {
bool done = driver.next(); bool done = driver.next();

View File

@@ -482,7 +482,7 @@ template <class ConflictSetImpl> struct TestDriver {
ConflictSetImpl cs{oldestVersion}; ConflictSetImpl cs{oldestVersion};
ReferenceImpl refImpl{oldestVersion}; ReferenceImpl refImpl{oldestVersion};
constexpr static auto kMaxKeyLen = 32; constexpr static auto kMaxKeyLen = 64;
bool ok = true; bool ok = true;