Skip to content

Commit d439451

Browse files
author
Lei Jin
committed
delay initialization of cuckoo table iterator
Summary: cuckoo table iterator creation is quite expensive since it needs to load all data and sort them. After compaction, RocksDB creates a new iterator of the new file to make sure it is in good state. That makes the DB creation quite slow. Delay the iterator db sort to the seek time to speed it up. Test Plan: db_bench Reviewers: igor, yhchiang, sdong Reviewed By: sdong Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D23775
1 parent 94997ea commit d439451

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

table/cuckoo_table_reader.cc

+11-5
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class CuckooTableIterator : public Iterator {
191191
Slice key() const override;
192192
Slice value() const override;
193193
Status status() const override { return status_; }
194-
void LoadKeysFromReader();
194+
void InitIfNeeded();
195195

196196
private:
197197
struct BucketComparator {
@@ -224,6 +224,7 @@ class CuckooTableIterator : public Iterator {
224224
const BucketComparator bucket_comparator_;
225225
void PrepareKVAtCurrIdx();
226226
CuckooTableReader* reader_;
227+
bool initialized_;
227228
Status status_;
228229
// Contains a map of keys to bucket_id sorted in key order.
229230
std::vector<uint32_t> sorted_bucket_ids_;
@@ -240,13 +241,17 @@ CuckooTableIterator::CuckooTableIterator(CuckooTableReader* reader)
240241
: bucket_comparator_(reader->file_data_, reader->ucomp_,
241242
reader->bucket_length_, reader->user_key_length_),
242243
reader_(reader),
244+
initialized_(false),
243245
curr_key_idx_(kInvalidIndex) {
244246
sorted_bucket_ids_.clear();
245247
curr_value_.clear();
246248
curr_key_.Clear();
247249
}
248250

249-
void CuckooTableIterator::LoadKeysFromReader() {
251+
void CuckooTableIterator::InitIfNeeded() {
252+
if (initialized_) {
253+
return;
254+
}
250255
sorted_bucket_ids_.reserve(reader_->GetTableProperties()->num_entries);
251256
uint64_t num_buckets = reader_->table_size_ + reader_->cuckoo_block_size_ - 1;
252257
assert(num_buckets < kInvalidIndex);
@@ -262,19 +267,23 @@ void CuckooTableIterator::LoadKeysFromReader() {
262267
std::sort(sorted_bucket_ids_.begin(), sorted_bucket_ids_.end(),
263268
bucket_comparator_);
264269
curr_key_idx_ = kInvalidIndex;
270+
initialized_ = true;
265271
}
266272

267273
void CuckooTableIterator::SeekToFirst() {
274+
InitIfNeeded();
268275
curr_key_idx_ = 0;
269276
PrepareKVAtCurrIdx();
270277
}
271278

272279
void CuckooTableIterator::SeekToLast() {
280+
InitIfNeeded();
273281
curr_key_idx_ = sorted_bucket_ids_.size() - 1;
274282
PrepareKVAtCurrIdx();
275283
}
276284

277285
void CuckooTableIterator::Seek(const Slice& target) {
286+
InitIfNeeded();
278287
const BucketComparator seek_comparator(
279288
reader_->file_data_, reader_->ucomp_,
280289
reader_->bucket_length_, reader_->user_key_length_,
@@ -362,9 +371,6 @@ Iterator* CuckooTableReader::NewIterator(
362371
auto iter_mem = arena->AllocateAligned(sizeof(CuckooTableIterator));
363372
iter = new (iter_mem) CuckooTableIterator(this);
364373
}
365-
if (iter->status().ok()) {
366-
iter->LoadKeysFromReader();
367-
}
368374
return iter;
369375
}
370376

0 commit comments

Comments
 (0)