Make "begin" a template parameter to insert

cachegrind says this saves instructions
This commit is contained in:
2024-02-23 14:00:04 -08:00
parent f84aa88202
commit 4f32ecc26e

View File

@@ -1408,9 +1408,9 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
// setting 'entry' fields and `maxVersion` on the result, which may have // setting 'entry' fields and `maxVersion` on the result, which may have
// !entryPresent. The search path of the result's parent will have // !entryPresent. The search path of the result's parent will have
// `maxVersion` at least `writeVersion` as a postcondition. // `maxVersion` at least `writeVersion` as a postcondition.
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, bool begin, int64_t writeVersion, NodeAllocators *allocators) {
NodeAllocators *allocators) {
for (;;) { for (;;) {
auto &self = *self_; auto &self = *self_;
// Handle an existing partial key // Handle an existing partial key
@@ -1445,7 +1445,7 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
key = key.subspan(self->partialKeyLen, key.size() - self->partialKeyLen); key = key.subspan(self->partialKeyLen, key.size() - self->partialKeyLen);
} }
if (begin) { if constexpr (kBegin) {
self->maxVersion = std::max(self->maxVersion, writeVersion); self->maxVersion = std::max(self->maxVersion, writeVersion);
} }
@@ -1453,7 +1453,7 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
return self; return self;
} }
if (!begin) { if constexpr (!kBegin) {
self->maxVersion = std::max(self->maxVersion, writeVersion); self->maxVersion = std::max(self->maxVersion, writeVersion);
} }
@@ -1463,7 +1463,7 @@ bool checkRangeRead(Node *n, std::span<const uint8_t> begin,
child->parent = self; child->parent = self;
child->parentsIndex = key.front(); child->parentsIndex = key.front();
child->maxVersion = child->maxVersion =
begin ? writeVersion : std::numeric_limits<int64_t>::lowest(); kBegin ? writeVersion : std::numeric_limits<int64_t>::lowest();
} }
self_ = &child; self_ = &child;
@@ -1492,7 +1492,7 @@ void destroyTree(Node *root) {
void addPointWrite(Node *&root, int64_t oldestVersion, void addPointWrite(Node *&root, int64_t oldestVersion,
std::span<const uint8_t> key, int64_t writeVersion, std::span<const uint8_t> key, int64_t writeVersion,
NodeAllocators *allocators) { NodeAllocators *allocators) {
auto *n = insert(&root, key, writeVersion, true, allocators); auto *n = insert<true>(&root, key, writeVersion, allocators);
if (!n->entryPresent) { if (!n->entryPresent) {
auto *p = nextLogical(n); auto *p = nextLogical(n);
n->entryPresent = true; n->entryPresent = true;
@@ -1550,7 +1550,7 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
begin = begin.subspan(consumed, begin.size() - consumed); begin = begin.subspan(consumed, begin.size() - consumed);
end = end.subspan(consumed, end.size() - consumed); end = end.subspan(consumed, end.size() - consumed);
auto *beginNode = insert(useAsRoot, begin, writeVersion, true, allocators); auto *beginNode = insert<true>(useAsRoot, begin, writeVersion, allocators);
const bool insertedBegin = !std::exchange(beginNode->entryPresent, true); const bool insertedBegin = !std::exchange(beginNode->entryPresent, true);
@@ -1565,7 +1565,7 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
beginNode->entry.pointVersion = beginNode->entry.pointVersion =
std::max(beginNode->entry.pointVersion, writeVersion); std::max(beginNode->entry.pointVersion, writeVersion);
auto *endNode = insert(useAsRoot, end, writeVersion, false, allocators); auto *endNode = insert<false>(useAsRoot, end, writeVersion, allocators);
const bool insertedEnd = !std::exchange(endNode->entryPresent, true); const bool insertedEnd = !std::exchange(endNode->entryPresent, true);
@@ -1580,7 +1580,7 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
if (insertedEnd) { if (insertedEnd) {
// beginNode may have been invalidated // beginNode may have been invalidated
beginNode = insert(useAsRoot, begin, writeVersion, true, allocators); beginNode = insert<true>(useAsRoot, begin, writeVersion, allocators);
} }
for (beginNode = nextLogical(beginNode); beginNode != endNode;) { for (beginNode = nextLogical(beginNode); beginNode != endNode;) {