From 9d66d6d13e25b1b80b37da7da7d59fb407e0a6cf Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Mon, 18 Oct 2021 08:34:45 -0700 Subject: [PATCH] Two performance improvements in BlockBuilder (#9039) Summary: Primarily, this change reserves space in the std::string for building the next block once a block is finished, using `block_size` as reservation size. Note: also tried reusing same std::string in the common "unbuffered" path but that showed no benefit or regression. Secondarily, this slightly reduces the work in resetting `restarts_`. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9039 Test Plan: TEST_TMPDIR=/dev/shm/rocksdb1 ./db_bench -benchmarks=fillseq -memtablerep=vector -allow_concurrent_memtable_write=false -num=50000000 Compiled with DEBUG_LEVEL=0 Test vs. control runs simulaneous for better accuracy, units = ops/sec Run 1, Primary change only: 292697 vs. 280267 (+4.4%) Run 2, Primary change only: 288763 vs. 279621 (+3.3%) Run 1, Secondary change only: 260065 vs. 254232 (+2.3%) Run 2, Secondary change only: 275925 vs. 272248 (+1.4%) Run 1, Both changes: 284890 vs. 270372 (+5.3%) Run 2, Both changes: 263511 vs. 258188 (+2.0%) Reviewed By: zhichao-cao Differential Revision: D31701253 Pulled By: pdillinger fbshipit-source-id: 7e40810afbb98e6b6446955e77bda59e69b19ffd --- table/block_based/block_based_table_builder.cc | 1 + table/block_based/block_builder.cc | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/table/block_based/block_based_table_builder.cc b/table/block_based/block_based_table_builder.cc index 55f4d826d..c94004815 100644 --- a/table/block_based/block_based_table_builder.cc +++ b/table/block_based/block_based_table_builder.cc @@ -1023,6 +1023,7 @@ void BlockBasedTableBuilder::WriteBlock(BlockBuilder* block, BlockType block_type) { block->Finish(); std::string raw_block_contents; + raw_block_contents.reserve(rep_->table_options.block_size); block->SwapAndReset(raw_block_contents); if (rep_->state == Rep::State::kBuffered) { assert(block_type == BlockType::kData); diff --git a/table/block_based/block_builder.cc b/table/block_based/block_builder.cc index 4964ba3ae..219504d50 100644 --- a/table/block_based/block_builder.cc +++ b/table/block_based/block_builder.cc @@ -50,7 +50,7 @@ BlockBuilder::BlockBuilder( : block_restart_interval_(block_restart_interval), use_delta_encoding_(use_delta_encoding), use_value_delta_encoding_(use_value_delta_encoding), - restarts_(), + restarts_(1, 0), // First restart point is at offset 0 counter_(0), finished_(false) { switch (index_type) { @@ -64,14 +64,13 @@ BlockBuilder::BlockBuilder( assert(0); } assert(block_restart_interval_ >= 1); - restarts_.push_back(0); // First restart point is at offset 0 estimate_ = sizeof(uint32_t) + sizeof(uint32_t); } void BlockBuilder::Reset() { buffer_.clear(); - restarts_.clear(); - restarts_.push_back(0); // First restart point is at offset 0 + restarts_.resize(1); // First restart point is at offset 0 + assert(restarts_[0] == 0); estimate_ = sizeof(uint32_t) + sizeof(uint32_t); counter_ = 0; finished_ = false;