From 95a411d8530c5de173238ff34400f57df6bb8b1f Mon Sep 17 00:00:00 2001 From: Siying Dong Date: Tue, 10 Dec 2013 20:03:27 -0800 Subject: [PATCH] When flushing mem tables, create iterators out of mutex Summary: creating new iterators of mem tables can be expensive. Move them out of mutex. DBImpl::WriteLevel0Table()'s mems seems to be a local vector and is only used by flushing. memtables to flush are also immutable, so it should be safe to do so. Test Plan: make all check Reviewers: haobo, dhruba, kailiu Reviewed By: dhruba CC: igor, leveldb Differential Revision: https://reviews.facebook.net/D14577 --- db/db_impl.cc | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index d29449c4e..895bb20cc 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1054,27 +1054,26 @@ Status DBImpl::WriteLevel0Table(std::vector &mems, VersionEdit* edit, *filenumber = meta.number; pending_outputs_.insert(meta.number); - std::vector list; - for (MemTable* m : mems) { - Log(options_.info_log, - "Flushing memtable with log file: %lu\n", - (unsigned long)m->GetLogNumber()); - list.push_back(m->NewIterator()); - } - Iterator* iter = NewMergingIterator(env_, &internal_comparator_, &list[0], - list.size()); const SequenceNumber newest_snapshot = snapshots_.GetNewest(); const SequenceNumber earliest_seqno_in_memtable = mems[0]->GetFirstSequenceNumber(); - Log(options_.info_log, - "Level-0 flush table #%lu: started", - (unsigned long)meta.number); - Version* base = versions_->current(); base->Ref(); // it is likely that we do not need this reference Status s; { mutex_.Unlock(); + std::vector list; + for (MemTable* m : mems) { + Log(options_.info_log, + "Flushing memtable with log file: %lu\n", + (unsigned long)m->GetLogNumber()); + list.push_back(m->NewIterator()); + } + Iterator* iter = NewMergingIterator(env_, &internal_comparator_, &list[0], + list.size()); + Log(options_.info_log, + "Level-0 flush table #%lu: started", + (unsigned long)meta.number); // We skip compression if universal compression is used and the size // threshold is set for compression. bool enable_compression = (options_.compaction_style @@ -1085,15 +1084,15 @@ Status DBImpl::WriteLevel0Table(std::vector &mems, VersionEdit* edit, user_comparator(), newest_snapshot, earliest_seqno_in_memtable, enable_compression); LogFlush(options_.info_log); + delete iter; + Log(options_.info_log, "Level-0 flush table #%lu: %lu bytes %s", + (unsigned long) meta.number, + (unsigned long) meta.file_size, + s.ToString().c_str()); mutex_.Lock(); } base->Unref(); - Log(options_.info_log, "Level-0 flush table #%lu: %lu bytes %s", - (unsigned long) meta.number, - (unsigned long) meta.file_size, - s.ToString().c_str()); - delete iter; // re-acquire the most current version base = versions_->current();