From 88e0527724bc3be91cbfe5a090835ff588a46646 Mon Sep 17 00:00:00 2001 From: Islam AbdelRahman Date: Thu, 19 Nov 2015 22:26:37 -0800 Subject: [PATCH] Reduce moving memory in LDB::ScanCommand Summary: Based on https://github.com/facebook/rocksdb/issues/843 It looks that when the data is hot we spend significant amount of time moving data out of RocksDB blocks. This patch reduce moving memory when possible Original performance ``` $ time ./ldb --db=/home/tec/local/ellina_test/testdb scan > /dev/null real 0m16.736s user 0m11.993s sys 0m4.725s ``` Performance after reducing memcpy ``` $ time ./ldb --db=/home/tec/local/ellina_test/testdb scan > /dev/null real 0m11.590s user 0m6.983s sys 0m4.595s ``` Test Plan: dump the output of the scan into 2 files and verifying the are exactly the same make check Reviewers: sdong, yhchiang, anthony, rven, igor Reviewed By: igor Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D51093 --- tools/ldb_cmd.cc | 29 +++++++++++++++++++++++------ tools/ldb_tool.cc | 11 +---------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tools/ldb_cmd.cc b/tools/ldb_cmd.cc index 93c82fb43..b9adef02a 100644 --- a/tools/ldb_cmd.cc +++ b/tools/ldb_cmd.cc @@ -1762,7 +1762,6 @@ void ScanCommand::DoCommand() { for ( ; it->Valid() && (!end_key_specified_ || it->key().ToString() < end_key_); it->Next()) { - string key = ldb_options_.key_formatter->Format(it->key()); if (is_db_ttl_) { TtlIterator* it_ttl = dynamic_cast(it); assert(it_ttl); @@ -1774,11 +1773,29 @@ void ScanCommand::DoCommand() { fprintf(stdout, "%s ", ReadableTime(rawtime).c_str()); } } - string value = it->value().ToString(); - fprintf(stdout, "%s : %s\n", - (is_key_hex_ ? "0x" + it->key().ToString(true) : key).c_str(), - (is_value_hex_ ? StringToHex(value) : value).c_str() - ); + + Slice key_slice = it->key(); + Slice val_slice = it->value(); + + std::string formatted_key; + if (is_key_hex_) { + formatted_key = "0x" + key_slice.ToString(true /* hex */); + key_slice = formatted_key; + } else if (ldb_options_.key_formatter) { + formatted_key = ldb_options_.key_formatter->Format(key_slice); + key_slice = formatted_key; + } + + std::string formatted_value; + if (is_value_hex_) { + formatted_value = "0x" + val_slice.ToString(true /* hex */); + val_slice = formatted_value; + } + + fprintf(stdout, "%.*s : %.*s\n", + static_cast(key_slice.size()), key_slice.data(), + static_cast(val_slice.size()), val_slice.data()); + num_keys_scanned++; if (max_keys_scanned_ >= 0 && num_keys_scanned >= max_keys_scanned_) { break; diff --git a/tools/ldb_tool.cc b/tools/ldb_tool.cc index 366202c2d..c1b23ebac 100644 --- a/tools/ldb_tool.cc +++ b/tools/ldb_tool.cc @@ -9,16 +9,7 @@ namespace rocksdb { -class DefaultSliceFormatter : public SliceFormatter { - public: - virtual std::string Format(const Slice& s) const override { - return s.ToString(); - } -}; - -LDBOptions::LDBOptions() - : key_formatter(new DefaultSliceFormatter()) { -} +LDBOptions::LDBOptions() {} class LDBCommandRunner { public: