From 9b51af5a17f3cfd754575894e090dd867fb47740 Mon Sep 17 00:00:00 2001 From: Siying Dong Date: Thu, 12 Dec 2013 10:54:03 -0800 Subject: [PATCH] [RocksDB Performance Branch] DBImpl.NewInternalIterator() to reduce works inside mutex Summary: To reduce mutex contention caused by DBImpl.NewInternalIterator(), in this function, move all the iteration creation works out of mutex, only leaving object ref and get. Test Plan: make all check will run db_stress for a while too to make sure no problem. Reviewers: haobo, dhruba, kailiu Reviewed By: haobo CC: igor, leveldb Differential Revision: https://reviews.facebook.net/D14589 Conflicts: db/db_impl.cc --- db/db_impl.cc | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 908ede5b4..07ac5c9d0 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -2657,38 +2657,40 @@ static void CleanupIteratorState(void* arg1, void* arg2) { Iterator* DBImpl::NewInternalIterator(const ReadOptions& options, SequenceNumber* latest_snapshot) { IterState* cleanup = new IterState; - mutex_.Lock(); - *latest_snapshot = versions_->LastSequence(); + MemTable* mutable_mem; + std::vector immutables; + Version* version; // Collect together all needed child iterators for mem - std::vector list; + mutex_.Lock(); + *latest_snapshot = versions_->LastSequence(); mem_->Ref(); - list.push_back(mem_->NewIterator(options)); - - cleanup->mem.push_back(mem_); - + mutable_mem = mem_; // Collect together all needed child iterators for imm_ - std::vector immutables; imm_.GetMemTables(&immutables); for (unsigned int i = 0; i < immutables.size(); i++) { - MemTable* m = immutables[i]; - m->Ref(); + immutables[i]->Ref(); + } + // Collect iterators for files in L0 - Ln + versions_->current()->Ref(); + version = versions_->current(); + mutex_.Unlock(); + + std::vector list; + list.push_back(mutable_mem->NewIterator(options)); + cleanup->mem.push_back(mutable_mem); + for (MemTable* m : immutables) { list.push_back(m->NewIterator(options)); cleanup->mem.push_back(m); } - - // Collect iterators for files in L0 - Ln - versions_->current()->AddIterators(options, storage_options_, &list); + version->AddIterators(options, storage_options_, &list); Iterator* internal_iter = NewMergingIterator(&internal_comparator_, &list[0], list.size()); - versions_->current()->Ref(); - + cleanup->version = version; cleanup->mu = &mutex_; cleanup->db = this; - cleanup->version = versions_->current(); internal_iter->RegisterCleanup(CleanupIteratorState, cleanup, nullptr); - mutex_.Unlock(); return internal_iter; }