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
This commit is contained in:
Peter Dillinger 2021-10-18 08:34:45 -07:00 committed by Facebook GitHub Bot
parent 3ffb3baa0b
commit 9d66d6d13e
2 changed files with 4 additions and 4 deletions

View File

@ -1023,6 +1023,7 @@ void BlockBasedTableBuilder::WriteBlock(BlockBuilder* block,
BlockType block_type) { BlockType block_type) {
block->Finish(); block->Finish();
std::string raw_block_contents; std::string raw_block_contents;
raw_block_contents.reserve(rep_->table_options.block_size);
block->SwapAndReset(raw_block_contents); block->SwapAndReset(raw_block_contents);
if (rep_->state == Rep::State::kBuffered) { if (rep_->state == Rep::State::kBuffered) {
assert(block_type == BlockType::kData); assert(block_type == BlockType::kData);

View File

@ -50,7 +50,7 @@ BlockBuilder::BlockBuilder(
: block_restart_interval_(block_restart_interval), : block_restart_interval_(block_restart_interval),
use_delta_encoding_(use_delta_encoding), use_delta_encoding_(use_delta_encoding),
use_value_delta_encoding_(use_value_delta_encoding), use_value_delta_encoding_(use_value_delta_encoding),
restarts_(), restarts_(1, 0), // First restart point is at offset 0
counter_(0), counter_(0),
finished_(false) { finished_(false) {
switch (index_type) { switch (index_type) {
@ -64,14 +64,13 @@ BlockBuilder::BlockBuilder(
assert(0); assert(0);
} }
assert(block_restart_interval_ >= 1); assert(block_restart_interval_ >= 1);
restarts_.push_back(0); // First restart point is at offset 0
estimate_ = sizeof(uint32_t) + sizeof(uint32_t); estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
} }
void BlockBuilder::Reset() { void BlockBuilder::Reset() {
buffer_.clear(); buffer_.clear();
restarts_.clear(); restarts_.resize(1); // First restart point is at offset 0
restarts_.push_back(0); // First restart point is at offset 0 assert(restarts_[0] == 0);
estimate_ = sizeof(uint32_t) + sizeof(uint32_t); estimate_ = sizeof(uint32_t) + sizeof(uint32_t);
counter_ = 0; counter_ = 0;
finished_ = false; finished_ = false;