Skip to content

Commit

Permalink
[RocksDB] Fix skiplist sequential insertion optimization
Browse files Browse the repository at this point in the history
Summary: The original optimization missed updating links other than the lowest level.

Test Plan: make check; perf_context_test

Reviewers: dhruba

Reviewed By: dhruba

CC: leveldb, adsharma

Differential Revision: https://reviews.facebook.net/D13119
  • Loading branch information
haoboxu committed Sep 26, 2013
1 parent e0aa19a commit 08740b1
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions db/skiplist.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class SkipList {

// Used for optimizing sequential insert patterns
Node* prev_[kMaxHeight];
int prev_height_;

inline int GetMaxHeight() const {
return static_cast<int>(
Expand Down Expand Up @@ -267,6 +268,10 @@ typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindGreaterOr
Node* x = prev[0];
Node* next = x->Next(0);
if ((x == head_) || KeyIsAfterNode(key, x)) {
// Adjust all relevant insertion points to the previous entry
for (int i = 1; i < prev_height_; i++) {
prev[i] = x;
}
return next;
}
}
Expand All @@ -275,6 +280,9 @@ typename SkipList<Key,Comparator>::Node* SkipList<Key,Comparator>::FindGreaterOr
int level = GetMaxHeight() - 1;
while (true) {
Node* next = x->Next(level);
// Make sure the lists are sorted.
// If x points to head_ or next points nullptr, it is trivially satisfied.
assert((x == head_) || (next == nullptr) || KeyIsAfterNode(next->key, x));
if (KeyIsAfterNode(key, next)) {
// Keep searching in this list
x = next;
Expand Down Expand Up @@ -337,6 +345,7 @@ SkipList<Key,Comparator>::SkipList(Comparator cmp, Arena* arena)
arena_(arena),
head_(NewNode(0 /* any key will do */, kMaxHeight)),
max_height_(reinterpret_cast<void*>(1)),
prev_height_(1),
rnd_(0xdeadbeef) {
for (int i = 0; i < kMaxHeight; i++) {
head_->SetNext(i, nullptr);
Expand Down Expand Up @@ -378,6 +387,7 @@ void SkipList<Key,Comparator>::Insert(const Key& key) {
prev_[i]->SetNext(i, x);
}
prev_[0] = x;
prev_height_ = height;
}

template<typename Key, class Comparator>
Expand Down

0 comments on commit 08740b1

Please sign in to comment.