inlineskiplist.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved. Use of
  7. // this source code is governed by a BSD-style license that can be found
  8. // in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. // InlineSkipList is derived from SkipList (skiplist.h), but it optimizes
  11. // the memory layout by requiring that the key storage be allocated through
  12. // the skip list instance. For the common case of SkipList<const char*,
  13. // Cmp> this saves 1 pointer per skip list node and gives better cache
  14. // locality, at the expense of wasted padding from using AllocateAligned
  15. // instead of Allocate for the keys. The unused padding will be from
  16. // 0 to sizeof(void*)-1 bytes, and the space savings are sizeof(void*)
  17. // bytes, so despite the padding the space used is always less than
  18. // SkipList<const char*, ..>.
  19. //
  20. // Thread safety -------------
  21. //
  22. // Writes via Insert require external synchronization, most likely a mutex.
  23. // InsertConcurrently can be safely called concurrently with reads and
  24. // with other concurrent inserts. Reads require a guarantee that the
  25. // InlineSkipList will not be destroyed while the read is in progress.
  26. // Apart from that, reads progress without any internal locking or
  27. // synchronization.
  28. //
  29. // Invariants:
  30. //
  31. // (1) Allocated nodes are never deleted until the InlineSkipList is
  32. // destroyed. This is trivially guaranteed by the code since we never
  33. // delete any skip list nodes.
  34. //
  35. // (2) The contents of a Node except for the next/prev pointers are
  36. // immutable after the Node has been linked into the InlineSkipList.
  37. // Only Insert() modifies the list, and it is careful to initialize a
  38. // node and use release-stores to publish the nodes in one or more lists.
  39. //
  40. // ... prev vs. next pointer ordering ...
  41. //
  42. #pragma once
  43. #include <assert.h>
  44. #include <stdlib.h>
  45. #include <algorithm>
  46. #include <atomic>
  47. #include <type_traits>
  48. #include "memory/allocator.h"
  49. #include "port/likely.h"
  50. #include "port/port.h"
  51. #include "rocksdb/slice.h"
  52. #include "util/coding.h"
  53. #include "util/random.h"
  54. namespace ROCKSDB_NAMESPACE {
  55. template <class Comparator>
  56. class InlineSkipList {
  57. private:
  58. struct Node;
  59. struct Splice;
  60. public:
  61. using DecodedKey = \
  62. typename std::remove_reference<Comparator>::type::DecodedType;
  63. static const uint16_t kMaxPossibleHeight = 32;
  64. // Create a new InlineSkipList object that will use "cmp" for comparing
  65. // keys, and will allocate memory using "*allocator". Objects allocated
  66. // in the allocator must remain allocated for the lifetime of the
  67. // skiplist object.
  68. explicit InlineSkipList(Comparator cmp, Allocator* allocator,
  69. int32_t max_height = 12,
  70. int32_t branching_factor = 4);
  71. // No copying allowed
  72. InlineSkipList(const InlineSkipList&) = delete;
  73. InlineSkipList& operator=(const InlineSkipList&) = delete;
  74. // Allocates a key and a skip-list node, returning a pointer to the key
  75. // portion of the node. This method is thread-safe if the allocator
  76. // is thread-safe.
  77. char* AllocateKey(size_t key_size);
  78. // Allocate a splice using allocator.
  79. Splice* AllocateSplice();
  80. // Allocate a splice on heap.
  81. Splice* AllocateSpliceOnHeap();
  82. // Inserts a key allocated by AllocateKey, after the actual key value
  83. // has been filled in.
  84. //
  85. // REQUIRES: nothing that compares equal to key is currently in the list.
  86. // REQUIRES: no concurrent calls to any of inserts.
  87. bool Insert(const char* key);
  88. // Inserts a key allocated by AllocateKey with a hint of last insert
  89. // position in the skip-list. If hint points to nullptr, a new hint will be
  90. // populated, which can be used in subsequent calls.
  91. //
  92. // It can be used to optimize the workload where there are multiple groups
  93. // of keys, and each key is likely to insert to a location close to the last
  94. // inserted key in the same group. One example is sequential inserts.
  95. //
  96. // REQUIRES: nothing that compares equal to key is currently in the list.
  97. // REQUIRES: no concurrent calls to any of inserts.
  98. bool InsertWithHint(const char* key, void** hint);
  99. // Like InsertConcurrently, but with a hint
  100. //
  101. // REQUIRES: nothing that compares equal to key is currently in the list.
  102. // REQUIRES: no concurrent calls that use same hint
  103. bool InsertWithHintConcurrently(const char* key, void** hint);
  104. // Like Insert, but external synchronization is not required.
  105. bool InsertConcurrently(const char* key);
  106. // Inserts a node into the skip list. key must have been allocated by
  107. // AllocateKey and then filled in by the caller. If UseCAS is true,
  108. // then external synchronization is not required, otherwise this method
  109. // may not be called concurrently with any other insertions.
  110. //
  111. // Regardless of whether UseCAS is true, the splice must be owned
  112. // exclusively by the current thread. If allow_partial_splice_fix is
  113. // true, then the cost of insertion is amortized O(log D), where D is
  114. // the distance from the splice to the inserted key (measured as the
  115. // number of intervening nodes). Note that this bound is very good for
  116. // sequential insertions! If allow_partial_splice_fix is false then
  117. // the existing splice will be ignored unless the current key is being
  118. // inserted immediately after the splice. allow_partial_splice_fix ==
  119. // false has worse running time for the non-sequential case O(log N),
  120. // but a better constant factor.
  121. template <bool UseCAS>
  122. bool Insert(const char* key, Splice* splice, bool allow_partial_splice_fix);
  123. // Returns true iff an entry that compares equal to key is in the list.
  124. bool Contains(const char* key) const;
  125. // Return estimated number of entries smaller than `key`.
  126. uint64_t EstimateCount(const char* key) const;
  127. // Validate correctness of the skip-list.
  128. void TEST_Validate() const;
  129. // Iteration over the contents of a skip list
  130. class Iterator {
  131. public:
  132. // Initialize an iterator over the specified list.
  133. // The returned iterator is not valid.
  134. explicit Iterator(const InlineSkipList* list);
  135. // Change the underlying skiplist used for this iterator
  136. // This enables us not changing the iterator without deallocating
  137. // an old one and then allocating a new one
  138. void SetList(const InlineSkipList* list);
  139. // Returns true iff the iterator is positioned at a valid node.
  140. bool Valid() const;
  141. // Returns the key at the current position.
  142. // REQUIRES: Valid()
  143. const char* key() const;
  144. // Advances to the next position.
  145. // REQUIRES: Valid()
  146. void Next();
  147. // Advances to the previous position.
  148. // REQUIRES: Valid()
  149. void Prev();
  150. // Advance to the first entry with a key >= target
  151. void Seek(const char* target);
  152. // Retreat to the last entry with a key <= target
  153. void SeekForPrev(const char* target);
  154. // Position at the first entry in list.
  155. // Final state of iterator is Valid() iff list is not empty.
  156. void SeekToFirst();
  157. // Position at the last entry in list.
  158. // Final state of iterator is Valid() iff list is not empty.
  159. void SeekToLast();
  160. private:
  161. const InlineSkipList* list_;
  162. Node* node_;
  163. // Intentionally copyable
  164. };
  165. private:
  166. const uint16_t kMaxHeight_;
  167. const uint16_t kBranching_;
  168. const uint32_t kScaledInverseBranching_;
  169. Allocator* const allocator_; // Allocator used for allocations of nodes
  170. // Immutable after construction
  171. Comparator const compare_;
  172. Node* const head_;
  173. // Modified only by Insert(). Read racily by readers, but stale
  174. // values are ok.
  175. std::atomic<int> max_height_; // Height of the entire list
  176. // seq_splice_ is a Splice used for insertions in the non-concurrent
  177. // case. It caches the prev and next found during the most recent
  178. // non-concurrent insertion.
  179. Splice* seq_splice_;
  180. inline int GetMaxHeight() const {
  181. return max_height_.load(std::memory_order_relaxed);
  182. }
  183. int RandomHeight();
  184. Node* AllocateNode(size_t key_size, int height);
  185. bool Equal(const char* a, const char* b) const {
  186. return (compare_(a, b) == 0);
  187. }
  188. bool LessThan(const char* a, const char* b) const {
  189. return (compare_(a, b) < 0);
  190. }
  191. // Return true if key is greater than the data stored in "n". Null n
  192. // is considered infinite. n should not be head_.
  193. bool KeyIsAfterNode(const char* key, Node* n) const;
  194. bool KeyIsAfterNode(const DecodedKey& key, Node* n) const;
  195. // Returns the earliest node with a key >= key.
  196. // Return nullptr if there is no such node.
  197. Node* FindGreaterOrEqual(const char* key) const;
  198. // Return the latest node with a key < key.
  199. // Return head_ if there is no such node.
  200. // Fills prev[level] with pointer to previous node at "level" for every
  201. // level in [0..max_height_-1], if prev is non-null.
  202. Node* FindLessThan(const char* key, Node** prev = nullptr) const;
  203. // Return the latest node with a key < key on bottom_level. Start searching
  204. // from root node on the level below top_level.
  205. // Fills prev[level] with pointer to previous node at "level" for every
  206. // level in [bottom_level..top_level-1], if prev is non-null.
  207. Node* FindLessThan(const char* key, Node** prev, Node* root, int top_level,
  208. int bottom_level) const;
  209. // Return the last node in the list.
  210. // Return head_ if list is empty.
  211. Node* FindLast() const;
  212. // Traverses a single level of the list, setting *out_prev to the last
  213. // node before the key and *out_next to the first node after. Assumes
  214. // that the key is not present in the skip list. On entry, before should
  215. // point to a node that is before the key, and after should point to
  216. // a node that is after the key. after should be nullptr if a good after
  217. // node isn't conveniently available.
  218. template<bool prefetch_before>
  219. void FindSpliceForLevel(const DecodedKey& key, Node* before, Node* after, int level,
  220. Node** out_prev, Node** out_next);
  221. // Recomputes Splice levels from highest_level (inclusive) down to
  222. // lowest_level (inclusive).
  223. void RecomputeSpliceLevels(const DecodedKey& key, Splice* splice,
  224. int recompute_level);
  225. };
  226. // Implementation details follow
  227. template <class Comparator>
  228. struct InlineSkipList<Comparator>::Splice {
  229. // The invariant of a Splice is that prev_[i+1].key <= prev_[i].key <
  230. // next_[i].key <= next_[i+1].key for all i. That means that if a
  231. // key is bracketed by prev_[i] and next_[i] then it is bracketed by
  232. // all higher levels. It is _not_ required that prev_[i]->Next(i) ==
  233. // next_[i] (it probably did at some point in the past, but intervening
  234. // or concurrent operations might have inserted nodes in between).
  235. int height_ = 0;
  236. Node** prev_;
  237. Node** next_;
  238. };
  239. // The Node data type is more of a pointer into custom-managed memory than
  240. // a traditional C++ struct. The key is stored in the bytes immediately
  241. // after the struct, and the next_ pointers for nodes with height > 1 are
  242. // stored immediately _before_ the struct. This avoids the need to include
  243. // any pointer or sizing data, which reduces per-node memory overheads.
  244. template <class Comparator>
  245. struct InlineSkipList<Comparator>::Node {
  246. // Stores the height of the node in the memory location normally used for
  247. // next_[0]. This is used for passing data from AllocateKey to Insert.
  248. void StashHeight(const int height) {
  249. assert(sizeof(int) <= sizeof(next_[0]));
  250. memcpy(static_cast<void*>(&next_[0]), &height, sizeof(int));
  251. }
  252. // Retrieves the value passed to StashHeight. Undefined after a call
  253. // to SetNext or NoBarrier_SetNext.
  254. int UnstashHeight() const {
  255. int rv;
  256. memcpy(&rv, &next_[0], sizeof(int));
  257. return rv;
  258. }
  259. const char* Key() const { return reinterpret_cast<const char*>(&next_[1]); }
  260. // Accessors/mutators for links. Wrapped in methods so we can add
  261. // the appropriate barriers as necessary, and perform the necessary
  262. // addressing trickery for storing links below the Node in memory.
  263. Node* Next(int n) {
  264. assert(n >= 0);
  265. // Use an 'acquire load' so that we observe a fully initialized
  266. // version of the returned Node.
  267. return ((&next_[0] - n)->load(std::memory_order_acquire));
  268. }
  269. void SetNext(int n, Node* x) {
  270. assert(n >= 0);
  271. // Use a 'release store' so that anybody who reads through this
  272. // pointer observes a fully initialized version of the inserted node.
  273. (&next_[0] - n)->store(x, std::memory_order_release);
  274. }
  275. bool CASNext(int n, Node* expected, Node* x) {
  276. assert(n >= 0);
  277. return (&next_[0] - n)->compare_exchange_strong(expected, x);
  278. }
  279. // No-barrier variants that can be safely used in a few locations.
  280. Node* NoBarrier_Next(int n) {
  281. assert(n >= 0);
  282. return (&next_[0] - n)->load(std::memory_order_relaxed);
  283. }
  284. void NoBarrier_SetNext(int n, Node* x) {
  285. assert(n >= 0);
  286. (&next_[0] - n)->store(x, std::memory_order_relaxed);
  287. }
  288. // Insert node after prev on specific level.
  289. void InsertAfter(Node* prev, int level) {
  290. // NoBarrier_SetNext() suffices since we will add a barrier when
  291. // we publish a pointer to "this" in prev.
  292. NoBarrier_SetNext(level, prev->NoBarrier_Next(level));
  293. prev->SetNext(level, this);
  294. }
  295. private:
  296. // next_[0] is the lowest level link (level 0). Higher levels are
  297. // stored _earlier_, so level 1 is at next_[-1].
  298. std::atomic<Node*> next_[1];
  299. };
  300. template <class Comparator>
  301. inline InlineSkipList<Comparator>::Iterator::Iterator(
  302. const InlineSkipList* list) {
  303. SetList(list);
  304. }
  305. template <class Comparator>
  306. inline void InlineSkipList<Comparator>::Iterator::SetList(
  307. const InlineSkipList* list) {
  308. list_ = list;
  309. node_ = nullptr;
  310. }
  311. template <class Comparator>
  312. inline bool InlineSkipList<Comparator>::Iterator::Valid() const {
  313. return node_ != nullptr;
  314. }
  315. template <class Comparator>
  316. inline const char* InlineSkipList<Comparator>::Iterator::key() const {
  317. assert(Valid());
  318. return node_->Key();
  319. }
  320. template <class Comparator>
  321. inline void InlineSkipList<Comparator>::Iterator::Next() {
  322. assert(Valid());
  323. node_ = node_->Next(0);
  324. }
  325. template <class Comparator>
  326. inline void InlineSkipList<Comparator>::Iterator::Prev() {
  327. // Instead of using explicit "prev" links, we just search for the
  328. // last node that falls before key.
  329. assert(Valid());
  330. node_ = list_->FindLessThan(node_->Key());
  331. if (node_ == list_->head_) {
  332. node_ = nullptr;
  333. }
  334. }
  335. template <class Comparator>
  336. inline void InlineSkipList<Comparator>::Iterator::Seek(const char* target) {
  337. node_ = list_->FindGreaterOrEqual(target);
  338. }
  339. template <class Comparator>
  340. inline void InlineSkipList<Comparator>::Iterator::SeekForPrev(
  341. const char* target) {
  342. Seek(target);
  343. if (!Valid()) {
  344. SeekToLast();
  345. }
  346. while (Valid() && list_->LessThan(target, key())) {
  347. Prev();
  348. }
  349. }
  350. template <class Comparator>
  351. inline void InlineSkipList<Comparator>::Iterator::SeekToFirst() {
  352. node_ = list_->head_->Next(0);
  353. }
  354. template <class Comparator>
  355. inline void InlineSkipList<Comparator>::Iterator::SeekToLast() {
  356. node_ = list_->FindLast();
  357. if (node_ == list_->head_) {
  358. node_ = nullptr;
  359. }
  360. }
  361. template <class Comparator>
  362. int InlineSkipList<Comparator>::RandomHeight() {
  363. auto rnd = Random::GetTLSInstance();
  364. // Increase height with probability 1 in kBranching
  365. int height = 1;
  366. while (height < kMaxHeight_ && height < kMaxPossibleHeight &&
  367. rnd->Next() < kScaledInverseBranching_) {
  368. height++;
  369. }
  370. assert(height > 0);
  371. assert(height <= kMaxHeight_);
  372. assert(height <= kMaxPossibleHeight);
  373. return height;
  374. }
  375. template <class Comparator>
  376. bool InlineSkipList<Comparator>::KeyIsAfterNode(const char* key,
  377. Node* n) const {
  378. // nullptr n is considered infinite
  379. assert(n != head_);
  380. return (n != nullptr) && (compare_(n->Key(), key) < 0);
  381. }
  382. template <class Comparator>
  383. bool InlineSkipList<Comparator>::KeyIsAfterNode(const DecodedKey& key,
  384. Node* n) const {
  385. // nullptr n is considered infinite
  386. assert(n != head_);
  387. return (n != nullptr) && (compare_(n->Key(), key) < 0);
  388. }
  389. template <class Comparator>
  390. typename InlineSkipList<Comparator>::Node*
  391. InlineSkipList<Comparator>::FindGreaterOrEqual(const char* key) const {
  392. // Note: It looks like we could reduce duplication by implementing
  393. // this function as FindLessThan(key)->Next(0), but we wouldn't be able
  394. // to exit early on equality and the result wouldn't even be correct.
  395. // A concurrent insert might occur after FindLessThan(key) but before
  396. // we get a chance to call Next(0).
  397. Node* x = head_;
  398. int level = GetMaxHeight() - 1;
  399. Node* last_bigger = nullptr;
  400. const DecodedKey key_decoded = compare_.decode_key(key);
  401. while (true) {
  402. Node* next = x->Next(level);
  403. if (next != nullptr) {
  404. PREFETCH(next->Next(level), 0, 1);
  405. }
  406. // Make sure the lists are sorted
  407. assert(x == head_ || next == nullptr || KeyIsAfterNode(next->Key(), x));
  408. // Make sure we haven't overshot during our search
  409. assert(x == head_ || KeyIsAfterNode(key_decoded, x));
  410. int cmp = (next == nullptr || next == last_bigger)
  411. ? 1
  412. : compare_(next->Key(), key_decoded);
  413. if (cmp == 0 || (cmp > 0 && level == 0)) {
  414. return next;
  415. } else if (cmp < 0) {
  416. // Keep searching in this list
  417. x = next;
  418. } else {
  419. // Switch to next list, reuse compare_() result
  420. last_bigger = next;
  421. level--;
  422. }
  423. }
  424. }
  425. template <class Comparator>
  426. typename InlineSkipList<Comparator>::Node*
  427. InlineSkipList<Comparator>::FindLessThan(const char* key, Node** prev) const {
  428. return FindLessThan(key, prev, head_, GetMaxHeight(), 0);
  429. }
  430. template <class Comparator>
  431. typename InlineSkipList<Comparator>::Node*
  432. InlineSkipList<Comparator>::FindLessThan(const char* key, Node** prev,
  433. Node* root, int top_level,
  434. int bottom_level) const {
  435. assert(top_level > bottom_level);
  436. int level = top_level - 1;
  437. Node* x = root;
  438. // KeyIsAfter(key, last_not_after) is definitely false
  439. Node* last_not_after = nullptr;
  440. const DecodedKey key_decoded = compare_.decode_key(key);
  441. while (true) {
  442. assert(x != nullptr);
  443. Node* next = x->Next(level);
  444. if (next != nullptr) {
  445. PREFETCH(next->Next(level), 0, 1);
  446. }
  447. assert(x == head_ || next == nullptr || KeyIsAfterNode(next->Key(), x));
  448. assert(x == head_ || KeyIsAfterNode(key_decoded, x));
  449. if (next != last_not_after && KeyIsAfterNode(key_decoded, next)) {
  450. // Keep searching in this list
  451. assert(next != nullptr);
  452. x = next;
  453. } else {
  454. if (prev != nullptr) {
  455. prev[level] = x;
  456. }
  457. if (level == bottom_level) {
  458. return x;
  459. } else {
  460. // Switch to next list, reuse KeyIsAfterNode() result
  461. last_not_after = next;
  462. level--;
  463. }
  464. }
  465. }
  466. }
  467. template <class Comparator>
  468. typename InlineSkipList<Comparator>::Node*
  469. InlineSkipList<Comparator>::FindLast() const {
  470. Node* x = head_;
  471. int level = GetMaxHeight() - 1;
  472. while (true) {
  473. Node* next = x->Next(level);
  474. if (next == nullptr) {
  475. if (level == 0) {
  476. return x;
  477. } else {
  478. // Switch to next list
  479. level--;
  480. }
  481. } else {
  482. x = next;
  483. }
  484. }
  485. }
  486. template <class Comparator>
  487. uint64_t InlineSkipList<Comparator>::EstimateCount(const char* key) const {
  488. uint64_t count = 0;
  489. Node* x = head_;
  490. int level = GetMaxHeight() - 1;
  491. const DecodedKey key_decoded = compare_.decode_key(key);
  492. while (true) {
  493. assert(x == head_ || compare_(x->Key(), key_decoded) < 0);
  494. Node* next = x->Next(level);
  495. if (next != nullptr) {
  496. PREFETCH(next->Next(level), 0, 1);
  497. }
  498. if (next == nullptr || compare_(next->Key(), key_decoded) >= 0) {
  499. if (level == 0) {
  500. return count;
  501. } else {
  502. // Switch to next list
  503. count *= kBranching_;
  504. level--;
  505. }
  506. } else {
  507. x = next;
  508. count++;
  509. }
  510. }
  511. }
  512. template <class Comparator>
  513. InlineSkipList<Comparator>::InlineSkipList(const Comparator cmp,
  514. Allocator* allocator,
  515. int32_t max_height,
  516. int32_t branching_factor)
  517. : kMaxHeight_(static_cast<uint16_t>(max_height)),
  518. kBranching_(static_cast<uint16_t>(branching_factor)),
  519. kScaledInverseBranching_((Random::kMaxNext + 1) / kBranching_),
  520. allocator_(allocator),
  521. compare_(cmp),
  522. head_(AllocateNode(0, max_height)),
  523. max_height_(1),
  524. seq_splice_(AllocateSplice()) {
  525. assert(max_height > 0 && kMaxHeight_ == static_cast<uint32_t>(max_height));
  526. assert(branching_factor > 1 &&
  527. kBranching_ == static_cast<uint32_t>(branching_factor));
  528. assert(kScaledInverseBranching_ > 0);
  529. for (int i = 0; i < kMaxHeight_; ++i) {
  530. head_->SetNext(i, nullptr);
  531. }
  532. }
  533. template <class Comparator>
  534. char* InlineSkipList<Comparator>::AllocateKey(size_t key_size) {
  535. return const_cast<char*>(AllocateNode(key_size, RandomHeight())->Key());
  536. }
  537. template <class Comparator>
  538. typename InlineSkipList<Comparator>::Node*
  539. InlineSkipList<Comparator>::AllocateNode(size_t key_size, int height) {
  540. auto prefix = sizeof(std::atomic<Node*>) * (height - 1);
  541. // prefix is space for the height - 1 pointers that we store before
  542. // the Node instance (next_[-(height - 1) .. -1]). Node starts at
  543. // raw + prefix, and holds the bottom-mode (level 0) skip list pointer
  544. // next_[0]. key_size is the bytes for the key, which comes just after
  545. // the Node.
  546. char* raw = allocator_->AllocateAligned(prefix + sizeof(Node) + key_size);
  547. Node* x = reinterpret_cast<Node*>(raw + prefix);
  548. // Once we've linked the node into the skip list we don't actually need
  549. // to know its height, because we can implicitly use the fact that we
  550. // traversed into a node at level h to known that h is a valid level
  551. // for that node. We need to convey the height to the Insert step,
  552. // however, so that it can perform the proper links. Since we're not
  553. // using the pointers at the moment, StashHeight temporarily borrow
  554. // storage from next_[0] for that purpose.
  555. x->StashHeight(height);
  556. return x;
  557. }
  558. template <class Comparator>
  559. typename InlineSkipList<Comparator>::Splice*
  560. InlineSkipList<Comparator>::AllocateSplice() {
  561. // size of prev_ and next_
  562. size_t array_size = sizeof(Node*) * (kMaxHeight_ + 1);
  563. char* raw = allocator_->AllocateAligned(sizeof(Splice) + array_size * 2);
  564. Splice* splice = reinterpret_cast<Splice*>(raw);
  565. splice->height_ = 0;
  566. splice->prev_ = reinterpret_cast<Node**>(raw + sizeof(Splice));
  567. splice->next_ = reinterpret_cast<Node**>(raw + sizeof(Splice) + array_size);
  568. return splice;
  569. }
  570. template <class Comparator>
  571. typename InlineSkipList<Comparator>::Splice*
  572. InlineSkipList<Comparator>::AllocateSpliceOnHeap() {
  573. size_t array_size = sizeof(Node*) * (kMaxHeight_ + 1);
  574. char* raw = new char[sizeof(Splice) + array_size * 2];
  575. Splice* splice = reinterpret_cast<Splice*>(raw);
  576. splice->height_ = 0;
  577. splice->prev_ = reinterpret_cast<Node**>(raw + sizeof(Splice));
  578. splice->next_ = reinterpret_cast<Node**>(raw + sizeof(Splice) + array_size);
  579. return splice;
  580. }
  581. template <class Comparator>
  582. bool InlineSkipList<Comparator>::Insert(const char* key) {
  583. return Insert<false>(key, seq_splice_, false);
  584. }
  585. template <class Comparator>
  586. bool InlineSkipList<Comparator>::InsertConcurrently(const char* key) {
  587. Node* prev[kMaxPossibleHeight];
  588. Node* next[kMaxPossibleHeight];
  589. Splice splice;
  590. splice.prev_ = prev;
  591. splice.next_ = next;
  592. return Insert<true>(key, &splice, false);
  593. }
  594. template <class Comparator>
  595. bool InlineSkipList<Comparator>::InsertWithHint(const char* key, void** hint) {
  596. assert(hint != nullptr);
  597. Splice* splice = reinterpret_cast<Splice*>(*hint);
  598. if (splice == nullptr) {
  599. splice = AllocateSplice();
  600. *hint = reinterpret_cast<void*>(splice);
  601. }
  602. return Insert<false>(key, splice, true);
  603. }
  604. template <class Comparator>
  605. bool InlineSkipList<Comparator>::InsertWithHintConcurrently(const char* key,
  606. void** hint) {
  607. assert(hint != nullptr);
  608. Splice* splice = reinterpret_cast<Splice*>(*hint);
  609. if (splice == nullptr) {
  610. splice = AllocateSpliceOnHeap();
  611. *hint = reinterpret_cast<void*>(splice);
  612. }
  613. return Insert<true>(key, splice, true);
  614. }
  615. template <class Comparator>
  616. template <bool prefetch_before>
  617. void InlineSkipList<Comparator>::FindSpliceForLevel(const DecodedKey& key,
  618. Node* before, Node* after,
  619. int level, Node** out_prev,
  620. Node** out_next) {
  621. while (true) {
  622. Node* next = before->Next(level);
  623. if (next != nullptr) {
  624. PREFETCH(next->Next(level), 0, 1);
  625. }
  626. if (prefetch_before == true) {
  627. if (next != nullptr && level>0) {
  628. PREFETCH(next->Next(level-1), 0, 1);
  629. }
  630. }
  631. assert(before == head_ || next == nullptr ||
  632. KeyIsAfterNode(next->Key(), before));
  633. assert(before == head_ || KeyIsAfterNode(key, before));
  634. if (next == after || !KeyIsAfterNode(key, next)) {
  635. // found it
  636. *out_prev = before;
  637. *out_next = next;
  638. return;
  639. }
  640. before = next;
  641. }
  642. }
  643. template <class Comparator>
  644. void InlineSkipList<Comparator>::RecomputeSpliceLevels(const DecodedKey& key,
  645. Splice* splice,
  646. int recompute_level) {
  647. assert(recompute_level > 0);
  648. assert(recompute_level <= splice->height_);
  649. for (int i = recompute_level - 1; i >= 0; --i) {
  650. FindSpliceForLevel<true>(key, splice->prev_[i + 1], splice->next_[i + 1], i,
  651. &splice->prev_[i], &splice->next_[i]);
  652. }
  653. }
  654. template <class Comparator>
  655. template <bool UseCAS>
  656. bool InlineSkipList<Comparator>::Insert(const char* key, Splice* splice,
  657. bool allow_partial_splice_fix) {
  658. Node* x = reinterpret_cast<Node*>(const_cast<char*>(key)) - 1;
  659. const DecodedKey key_decoded = compare_.decode_key(key);
  660. int height = x->UnstashHeight();
  661. assert(height >= 1 && height <= kMaxHeight_);
  662. int max_height = max_height_.load(std::memory_order_relaxed);
  663. while (height > max_height) {
  664. if (max_height_.compare_exchange_weak(max_height, height)) {
  665. // successfully updated it
  666. max_height = height;
  667. break;
  668. }
  669. // else retry, possibly exiting the loop because somebody else
  670. // increased it
  671. }
  672. assert(max_height <= kMaxPossibleHeight);
  673. int recompute_height = 0;
  674. if (splice->height_ < max_height) {
  675. // Either splice has never been used or max_height has grown since
  676. // last use. We could potentially fix it in the latter case, but
  677. // that is tricky.
  678. splice->prev_[max_height] = head_;
  679. splice->next_[max_height] = nullptr;
  680. splice->height_ = max_height;
  681. recompute_height = max_height;
  682. } else {
  683. // Splice is a valid proper-height splice that brackets some
  684. // key, but does it bracket this one? We need to validate it and
  685. // recompute a portion of the splice (levels 0..recompute_height-1)
  686. // that is a superset of all levels that don't bracket the new key.
  687. // Several choices are reasonable, because we have to balance the work
  688. // saved against the extra comparisons required to validate the Splice.
  689. //
  690. // One strategy is just to recompute all of orig_splice_height if the
  691. // bottom level isn't bracketing. This pessimistically assumes that
  692. // we will either get a perfect Splice hit (increasing sequential
  693. // inserts) or have no locality.
  694. //
  695. // Another strategy is to walk up the Splice's levels until we find
  696. // a level that brackets the key. This strategy lets the Splice
  697. // hint help for other cases: it turns insertion from O(log N) into
  698. // O(log D), where D is the number of nodes in between the key that
  699. // produced the Splice and the current insert (insertion is aided
  700. // whether the new key is before or after the splice). If you have
  701. // a way of using a prefix of the key to map directly to the closest
  702. // Splice out of O(sqrt(N)) Splices and we make it so that splices
  703. // can also be used as hints during read, then we end up with Oshman's
  704. // and Shavit's SkipTrie, which has O(log log N) lookup and insertion
  705. // (compare to O(log N) for skip list).
  706. //
  707. // We control the pessimistic strategy with allow_partial_splice_fix.
  708. // A good strategy is probably to be pessimistic for seq_splice_,
  709. // optimistic if the caller actually went to the work of providing
  710. // a Splice.
  711. while (recompute_height < max_height) {
  712. if (splice->prev_[recompute_height]->Next(recompute_height) !=
  713. splice->next_[recompute_height]) {
  714. // splice isn't tight at this level, there must have been some inserts
  715. // to this
  716. // location that didn't update the splice. We might only be a little
  717. // stale, but if
  718. // the splice is very stale it would be O(N) to fix it. We haven't used
  719. // up any of
  720. // our budget of comparisons, so always move up even if we are
  721. // pessimistic about
  722. // our chances of success.
  723. ++recompute_height;
  724. } else if (splice->prev_[recompute_height] != head_ &&
  725. !KeyIsAfterNode(key_decoded,
  726. splice->prev_[recompute_height])) {
  727. // key is from before splice
  728. if (allow_partial_splice_fix) {
  729. // skip all levels with the same node without more comparisons
  730. Node* bad = splice->prev_[recompute_height];
  731. while (splice->prev_[recompute_height] == bad) {
  732. ++recompute_height;
  733. }
  734. } else {
  735. // we're pessimistic, recompute everything
  736. recompute_height = max_height;
  737. }
  738. } else if (KeyIsAfterNode(key_decoded,
  739. splice->next_[recompute_height])) {
  740. // key is from after splice
  741. if (allow_partial_splice_fix) {
  742. Node* bad = splice->next_[recompute_height];
  743. while (splice->next_[recompute_height] == bad) {
  744. ++recompute_height;
  745. }
  746. } else {
  747. recompute_height = max_height;
  748. }
  749. } else {
  750. // this level brackets the key, we won!
  751. break;
  752. }
  753. }
  754. }
  755. assert(recompute_height <= max_height);
  756. if (recompute_height > 0) {
  757. RecomputeSpliceLevels(key_decoded, splice, recompute_height);
  758. }
  759. bool splice_is_valid = true;
  760. if (UseCAS) {
  761. for (int i = 0; i < height; ++i) {
  762. while (true) {
  763. // Checking for duplicate keys on the level 0 is sufficient
  764. if (UNLIKELY(i == 0 && splice->next_[i] != nullptr &&
  765. compare_(x->Key(), splice->next_[i]->Key()) >= 0)) {
  766. // duplicate key
  767. return false;
  768. }
  769. if (UNLIKELY(i == 0 && splice->prev_[i] != head_ &&
  770. compare_(splice->prev_[i]->Key(), x->Key()) >= 0)) {
  771. // duplicate key
  772. return false;
  773. }
  774. assert(splice->next_[i] == nullptr ||
  775. compare_(x->Key(), splice->next_[i]->Key()) < 0);
  776. assert(splice->prev_[i] == head_ ||
  777. compare_(splice->prev_[i]->Key(), x->Key()) < 0);
  778. x->NoBarrier_SetNext(i, splice->next_[i]);
  779. if (splice->prev_[i]->CASNext(i, splice->next_[i], x)) {
  780. // success
  781. break;
  782. }
  783. // CAS failed, we need to recompute prev and next. It is unlikely
  784. // to be helpful to try to use a different level as we redo the
  785. // search, because it should be unlikely that lots of nodes have
  786. // been inserted between prev[i] and next[i]. No point in using
  787. // next[i] as the after hint, because we know it is stale.
  788. FindSpliceForLevel<false>(key_decoded, splice->prev_[i], nullptr, i,
  789. &splice->prev_[i], &splice->next_[i]);
  790. // Since we've narrowed the bracket for level i, we might have
  791. // violated the Splice constraint between i and i-1. Make sure
  792. // we recompute the whole thing next time.
  793. if (i > 0) {
  794. splice_is_valid = false;
  795. }
  796. }
  797. }
  798. } else {
  799. for (int i = 0; i < height; ++i) {
  800. if (i >= recompute_height &&
  801. splice->prev_[i]->Next(i) != splice->next_[i]) {
  802. FindSpliceForLevel<false>(key_decoded, splice->prev_[i], nullptr, i,
  803. &splice->prev_[i], &splice->next_[i]);
  804. }
  805. // Checking for duplicate keys on the level 0 is sufficient
  806. if (UNLIKELY(i == 0 && splice->next_[i] != nullptr &&
  807. compare_(x->Key(), splice->next_[i]->Key()) >= 0)) {
  808. // duplicate key
  809. return false;
  810. }
  811. if (UNLIKELY(i == 0 && splice->prev_[i] != head_ &&
  812. compare_(splice->prev_[i]->Key(), x->Key()) >= 0)) {
  813. // duplicate key
  814. return false;
  815. }
  816. assert(splice->next_[i] == nullptr ||
  817. compare_(x->Key(), splice->next_[i]->Key()) < 0);
  818. assert(splice->prev_[i] == head_ ||
  819. compare_(splice->prev_[i]->Key(), x->Key()) < 0);
  820. assert(splice->prev_[i]->Next(i) == splice->next_[i]);
  821. x->NoBarrier_SetNext(i, splice->next_[i]);
  822. splice->prev_[i]->SetNext(i, x);
  823. }
  824. }
  825. if (splice_is_valid) {
  826. for (int i = 0; i < height; ++i) {
  827. splice->prev_[i] = x;
  828. }
  829. assert(splice->prev_[splice->height_] == head_);
  830. assert(splice->next_[splice->height_] == nullptr);
  831. for (int i = 0; i < splice->height_; ++i) {
  832. assert(splice->next_[i] == nullptr ||
  833. compare_(key, splice->next_[i]->Key()) < 0);
  834. assert(splice->prev_[i] == head_ ||
  835. compare_(splice->prev_[i]->Key(), key) <= 0);
  836. assert(splice->prev_[i + 1] == splice->prev_[i] ||
  837. splice->prev_[i + 1] == head_ ||
  838. compare_(splice->prev_[i + 1]->Key(), splice->prev_[i]->Key()) <
  839. 0);
  840. assert(splice->next_[i + 1] == splice->next_[i] ||
  841. splice->next_[i + 1] == nullptr ||
  842. compare_(splice->next_[i]->Key(), splice->next_[i + 1]->Key()) <
  843. 0);
  844. }
  845. } else {
  846. splice->height_ = 0;
  847. }
  848. return true;
  849. }
  850. template <class Comparator>
  851. bool InlineSkipList<Comparator>::Contains(const char* key) const {
  852. Node* x = FindGreaterOrEqual(key);
  853. if (x != nullptr && Equal(key, x->Key())) {
  854. return true;
  855. } else {
  856. return false;
  857. }
  858. }
  859. template <class Comparator>
  860. void InlineSkipList<Comparator>::TEST_Validate() const {
  861. // Interate over all levels at the same time, and verify nodes appear in
  862. // the right order, and nodes appear in upper level also appear in lower
  863. // levels.
  864. Node* nodes[kMaxPossibleHeight];
  865. int max_height = GetMaxHeight();
  866. assert(max_height > 0);
  867. for (int i = 0; i < max_height; i++) {
  868. nodes[i] = head_;
  869. }
  870. while (nodes[0] != nullptr) {
  871. Node* l0_next = nodes[0]->Next(0);
  872. if (l0_next == nullptr) {
  873. break;
  874. }
  875. assert(nodes[0] == head_ || compare_(nodes[0]->Key(), l0_next->Key()) < 0);
  876. nodes[0] = l0_next;
  877. int i = 1;
  878. while (i < max_height) {
  879. Node* next = nodes[i]->Next(i);
  880. if (next == nullptr) {
  881. break;
  882. }
  883. auto cmp = compare_(nodes[0]->Key(), next->Key());
  884. assert(cmp <= 0);
  885. if (cmp == 0) {
  886. assert(next == nodes[0]);
  887. nodes[i] = next;
  888. } else {
  889. break;
  890. }
  891. i++;
  892. }
  893. }
  894. for (int i = 1; i < max_height; i++) {
  895. assert(nodes[i] != nullptr && nodes[i]->Next(i) == nullptr);
  896. }
  897. }
  898. } // namespace ROCKSDB_NAMESPACE