From 3aa03a559a5d9d61da414c56f526d3c06172b9a7 Mon Sep 17 00:00:00 2001 From: FlavioMili Date: Sat, 21 Mar 2026 13:25:02 +0100 Subject: [PATCH] Solved a TODO on barrier free Insert() and cleaned some unused headers --- db/db_impl.cc | 2 -- db/db_impl.h | 1 - db/memtable.cc | 4 ++-- db/memtable.h | 1 - db/skiplist.h | 34 ++++++++++++++++++++++++++++------ 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index ce4ef8276a..76d89c2f5c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -27,11 +27,9 @@ #include "leveldb/status.h" #include "leveldb/table.h" #include "leveldb/table_builder.h" -#include "port/port.h" #include "table/block.h" #include "table/merger.h" #include "table/two_level_iterator.h" -#include "util/coding.h" #include "util/logging.h" #include "util/mutexlock.h" diff --git a/db/db_impl.h b/db/db_impl.h index c7b01721b8..39de77ad51 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -15,7 +15,6 @@ #include "db/snapshot.h" #include "leveldb/db.h" #include "leveldb/env.h" -#include "port/port.h" #include "port/thread_annotations.h" namespace leveldb { diff --git a/db/memtable.cc b/db/memtable.cc index 4f09340e0e..dc636ac568 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -115,8 +115,8 @@ bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) { // all entries with overly large sequence numbers. const char* entry = iter.key(); uint32_t key_length; - const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length); - if (comparator_.comparator.user_comparator()->Compare( + if (const char* key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length); + comparator_.comparator.user_comparator()->Compare( Slice(key_ptr, key_length - 8), key.user_key()) == 0) { // Correct user key const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8); diff --git a/db/memtable.h b/db/memtable.h index 9d986b1070..eb9c2dc6f0 100644 --- a/db/memtable.h +++ b/db/memtable.h @@ -9,7 +9,6 @@ #include "db/dbformat.h" #include "db/skiplist.h" -#include "leveldb/db.h" #include "util/arena.h" namespace leveldb { diff --git a/db/skiplist.h b/db/skiplist.h index 1140e59a6a..375403c9f5 100644 --- a/db/skiplist.h +++ b/db/skiplist.h @@ -29,7 +29,6 @@ #include #include -#include #include "util/arena.h" #include "util/random.h" @@ -117,6 +116,9 @@ class SkipList { // node at "level" for every level in [0..max_height_-1]. Node* FindGreaterOrEqual(const Key& key, Node** prev) const; + // used inside of Insert() as it is externally synchronized + Node* FindGreaterOrEqualBarrierFree(const Key& key, Node** prev) const; + // Return the latest node with a key < key. // Return head_ if there is no such node. Node* FindLessThan(const Key& key) const; @@ -257,8 +259,7 @@ bool SkipList::KeyIsAfterNode(const Key& key, Node* n) const { template typename SkipList::Node* -SkipList::FindGreaterOrEqual(const Key& key, - Node** prev) const { +SkipList::FindGreaterOrEqual(const Key& key, Node** prev) const { Node* x = head_; int level = GetMaxHeight() - 1; while (true) { @@ -278,6 +279,28 @@ SkipList::FindGreaterOrEqual(const Key& key, } } +template +typename SkipList::Node* +SkipList::FindGreaterOrEqualBarrierFree(const Key& key, Node** prev) const { + Node* x = head_; + int level = GetMaxHeight() - 1; + while (true) { + Node* next = x->NoBarrier_Next(level); + if (KeyIsAfterNode(key, next)) { + // Keep searching in this list + x = next; + } else { + if (prev != nullptr) prev[level] = x; + if (level == 0) { + return next; + } else { + // Switch to next list + level--; + } + } + } +} + template typename SkipList::Node* SkipList::FindLessThan(const Key& key) const { @@ -333,10 +356,9 @@ SkipList::SkipList(Comparator cmp, Arena* arena) template void SkipList::Insert(const Key& key) { - // TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual() - // here since Insert() is externally synchronized. Node* prev[kMaxHeight]; - Node* x = FindGreaterOrEqual(key, prev); + // Use the barrier-free variant since Insert() is externally synchronized. + Node* x = FindGreaterOrEqualBarrierFree(key, prev); // Our data structure does not allow duplicate insertion assert(x == nullptr || !Equal(key, x->key));