From a5fafd4f46248e8b89d5ec7c63d3f25aaa347fd1 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Fri, 14 Mar 2014 14:01:45 -0700 Subject: [PATCH] Correct the logic of MemTable::ShouldFlushNow(). Summary: Memtable will now be forced to flush if the one of the following conditions is met: 1. Already allocated more than write_buffer_size + 60% arena block size. (the overflowing condition) 2. Unable to safely allocate one more arena block without hitting the overflowing condition AND the unused allocated memory < 25% arena block size. Test Plan: make all check Reviewers: sdong, haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D16893 --- db/memtable.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/db/memtable.cc b/db/memtable.cc index f9fa66eec..b787ec24e 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -78,21 +78,23 @@ bool MemTable::ShouldFlushNow() const { auto allocated_memory = table_->ApproximateMemoryUsage() + arena_.MemoryAllocatedBytes(); - if (allocated_memory + kArenaBlockSize * kAllowOverAllocationRatio < - kWriteBufferSize) { + // if we can still allocate one more block without exceeding the + // over-allocation ratio, then we should not flush. + if (allocated_memory + kArenaBlockSize < + kWriteBufferSize + kArenaBlockSize * kAllowOverAllocationRatio) { return false; } // if user keeps adding entries that exceeds kWriteBufferSize, we need to - // flush - // earlier even though we still have much available memory left. - if (allocated_memory > kWriteBufferSize * (1 + kAllowOverAllocationRatio)) { + // flush earlier even though we still have much available memory left. + if (allocated_memory > + kWriteBufferSize + kArenaBlockSize * kAllowOverAllocationRatio) { return true; } // In this code path, Arena has already allocated its "last block", which // means the total allocatedmemory size is either: - // (1) "moderately" over allocated the memory (no more than `0.4 * arena + // (1) "moderately" over allocated the memory (no more than `0.6 * arena // block size`. Or, // (2) the allocated memory is less than write buffer size, but we'll stop // here since if we allocate a new arena block, we'll over allocate too much