Skip to content

Commit 0304e3d

Browse files
committed
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 Conflicts: db/db_impl.cc
1 parent e8d40c3 commit 0304e3d

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

db/db_impl.cc

+17-18
Original file line numberDiff line numberDiff line change
@@ -1053,27 +1053,26 @@ Status DBImpl::WriteLevel0Table(std::vector<MemTable*> &mems, VersionEdit* edit,
10531053
*filenumber = meta.number;
10541054
pending_outputs_.insert(meta.number);
10551055

1056-
std::vector<Iterator*> list;
1057-
for (MemTable* m : mems) {
1058-
Log(options_.info_log,
1059-
"Flushing memtable with log file: %lu\n",
1060-
(unsigned long)m->GetLogNumber());
1061-
list.push_back(m->NewIterator());
1062-
}
1063-
Iterator* iter = NewMergingIterator(&internal_comparator_, &list[0],
1064-
list.size());
10651056
const SequenceNumber newest_snapshot = snapshots_.GetNewest();
10661057
const SequenceNumber earliest_seqno_in_memtable =
10671058
mems[0]->GetFirstSequenceNumber();
1068-
Log(options_.info_log,
1069-
"Level-0 flush table #%lu: started",
1070-
(unsigned long)meta.number);
1071-
10721059
Version* base = versions_->current();
10731060
base->Ref(); // it is likely that we do not need this reference
10741061
Status s;
10751062
{
10761063
mutex_.Unlock();
1064+
std::vector<Iterator*> list;
1065+
for (MemTable* m : mems) {
1066+
Log(options_.info_log,
1067+
"Flushing memtable with log file: %lu\n",
1068+
(unsigned long)m->GetLogNumber());
1069+
list.push_back(m->NewIterator());
1070+
}
1071+
Iterator* iter = NewMergingIterator(&internal_comparator_, &list[0],
1072+
list.size());
1073+
Log(options_.info_log,
1074+
"Level-0 flush table #%lu: started",
1075+
(unsigned long)meta.number);
10771076
// We skip compression if universal compression is used and the size
10781077
// threshold is set for compression.
10791078
bool enable_compression = (options_.compaction_style
@@ -1084,15 +1083,15 @@ Status DBImpl::WriteLevel0Table(std::vector<MemTable*> &mems, VersionEdit* edit,
10841083
user_comparator(), newest_snapshot,
10851084
earliest_seqno_in_memtable, enable_compression);
10861085
LogFlush(options_.info_log);
1086+
delete iter;
1087+
Log(options_.info_log, "Level-0 flush table #%lu: %lu bytes %s",
1088+
(unsigned long) meta.number,
1089+
(unsigned long) meta.file_size,
1090+
s.ToString().c_str());
10871091
mutex_.Lock();
10881092
}
10891093
base->Unref();
10901094

1091-
Log(options_.info_log, "Level-0 flush table #%lu: %lu bytes %s",
1092-
(unsigned long) meta.number,
1093-
(unsigned long) meta.file_size,
1094-
s.ToString().c_str());
1095-
delete iter;
10961095

10971096
// re-acquire the most current version
10981097
base = versions_->current();

0 commit comments

Comments
 (0)