diff --git a/db/memtable.cc b/db/memtable.cc index ba4f0da824..60037c9ba4 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -1621,7 +1621,8 @@ Status MemTable::UpdateCallback(SequenceNumber seq, const Slice& key, return Status::NotFound(); } -size_t MemTable::CountSuccessiveMergeEntries(const LookupKey& key) { +size_t MemTable::CountSuccessiveMergeEntries(const LookupKey& key, + size_t limit) { Slice memkey = key.memtable_key(); // A total ordered iterator is costly for some memtablerep (prefix aware @@ -1633,7 +1634,7 @@ size_t MemTable::CountSuccessiveMergeEntries(const LookupKey& key) { size_t num_successive_merges = 0; - for (; iter->Valid(); iter->Next()) { + for (; iter->Valid() && num_successive_merges < limit; iter->Next()) { const char* entry = iter->key(); uint32_t key_length = 0; const char* iter_key_ptr = GetVarint32Ptr(entry, entry + 5, &key_length); diff --git a/db/memtable.h b/db/memtable.h index 730258f05c..11ab6ea41b 100644 --- a/db/memtable.h +++ b/db/memtable.h @@ -326,9 +326,10 @@ class MemTable { const ProtectionInfoKVOS64* kv_prot_info); // Returns the number of successive merge entries starting from the newest - // entry for the key up to the last non-merge entry or last entry for the - // key in the memtable. - size_t CountSuccessiveMergeEntries(const LookupKey& key); + // entry for the key. The count ends when the oldest entry in the memtable + // with which the newest entry would be merged is reached, or the count + // reaches `limit`. + size_t CountSuccessiveMergeEntries(const LookupKey& key, size_t limit); // Update counters and flush status after inserting a whole write batch // Used in concurrent memtable inserts. diff --git a/db/write_batch.cc b/db/write_batch.cc index 4adba1de84..e16cef7f52 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -2623,8 +2623,10 @@ class MemTableInserter : public WriteBatch::Handler { LookupKey lkey(key, sequence_); // Count the number of successive merges at the head - // of the key in the memtable - size_t num_merges = mem->CountSuccessiveMergeEntries(lkey); + // of the key in the memtable. Limit the count to the threshold for + // triggering merge to prevent unnecessary counting overhead. + size_t num_merges = mem->CountSuccessiveMergeEntries( + lkey, moptions->max_successive_merges /* limit */); if (num_merges >= moptions->max_successive_merges) { perform_merge = true; diff --git a/unreleased_history/bug_fixes/max_successive_merges_regression.md b/unreleased_history/bug_fixes/max_successive_merges_regression.md new file mode 100644 index 0000000000..d00028014b --- /dev/null +++ b/unreleased_history/bug_fixes/max_successive_merges_regression.md @@ -0,0 +1 @@ +* Fixed a regression when `ColumnFamilyOptions::max_successive_merges > 0` where the CPU overhead for deciding whether to merge could have increased unless the user had set the option `ColumnFamilyOptions::strict_max_successive_merges`