Re-use search implementation in insert

This commit is contained in:
2024-05-24 14:23:32 -07:00
parent d11150926b
commit 37f972145e

View File

@@ -587,18 +587,18 @@ struct __attribute__((__visibility__("hidden"))) VersionedMap::Impl {
int len;
};
// Initialize finger to the insertion path of `key`. If `key` is not present,
// then the finger ends on a null entry.
template <std::memory_order kOrder, class T>
void search(Key key, T root, int64_t version, Finger &finger) const {
// Prevent integer promotion etc
static_assert(std::is_same_v<T, uint32_t>);
finger.clear();
if (root == 0) {
return;
}
bool ignored;
finger.push(root, ignored);
// Initialize finger to the search path of `key`
for (;;) {
auto n = finger.backNode();
if (n == 0) {
@@ -617,27 +617,9 @@ struct __attribute__((__visibility__("hidden"))) VersionedMap::Impl {
// Otherwise it's the end of a range mutation at `latestVersion`.
// `finger` becomes the search path of `key`
void insert(Key key, std::optional<Val> val, Finger &finger) {
bool ignored;
finger.clear();
finger.push(latestRoot, ignored);
bool inserted;
// Initialize finger to the search path of `key`
for (;;) {
auto n = finger.backNode();
if (n == 0) {
inserted = true;
break;
}
auto c = key <=> mm.base[n];
if (c == 0) {
// No duplicates
inserted = false;
break;
}
finger.push(child<std::memory_order_relaxed>(n, c > 0, latestVersion),
c > 0);
}
search<std::memory_order_relaxed>(key, latestRoot, latestVersion, finger);
const bool inserted = finger.backNode() == 0;
int64_t pointVersion, rangeVersion;
if (val.has_value()) {