Skip to content

Commit 7743cca

Browse files
committed
Small performance fix to Storage ConcatOperator::Merge
Don't resize, since in unoptimized builds this leads to clearing memory only to overwrite it again. Instead, use reserve/assign/append. Also in this commit: Made the `merges` counter be size_t.
1 parent c869a89 commit 7743cca

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/Storage.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ namespace {
541541
public:
542542
~ConcatOperator() override;
543543

544-
mutable std::atomic<unsigned> merges = 0;
544+
mutable std::atomic_size_t merges = 0;
545545

546546
// Gives the client a way to express the read -> modify -> write semantics
547547
// key: (IN) The key that's associated with this merge operation.
@@ -564,18 +564,19 @@ namespace {
564564

565565
ConcatOperator::~ConcatOperator() {} // weak vtable warning prevention
566566

567-
bool ConcatOperator::Merge(const rocksdb::Slice& key, const rocksdb::Slice* existing_value,
568-
const rocksdb::Slice& value, std::string* new_value, rocksdb::Logger* logger) const
567+
bool ConcatOperator::Merge(const rocksdb::Slice &key [[maybe_unused]], const rocksdb::Slice *existing_value,
568+
const rocksdb::Slice &value, std::string *new_value, rocksdb::Logger *) const
569569
{
570-
(void)key; (void)logger;
571570
++merges;
572-
new_value->resize( (existing_value ? existing_value->size() : size_t{0u}) + value.size() );
573-
char *cur = new_value->data();
574-
if (existing_value) {
575-
std::memcpy(cur, existing_value->data(), existing_value->size());
576-
cur += existing_value->size();
571+
if (!existing_value) {
572+
new_value->assign(value.data(), value.size());
573+
} else {
574+
new_value->clear();
575+
const size_t evsz{existing_value->size()}, vsz{value.size()};
576+
new_value->reserve(evsz + vsz);
577+
new_value->append(existing_value->data(), evsz);
578+
new_value->append(value.data(), vsz);
577579
}
578-
std::memcpy(cur, value.data(), value.size());
579580
return true;
580581
}
581582

@@ -624,7 +625,7 @@ namespace {
624625
return iter;
625626
}
626627

627-
unsigned mergeCount() const { return concatOp->merges.load(); }
628+
size_t mergeCount() const { return concatOp->merges.load(); }
628629

629630
QString dbName() const { return QString::fromStdString(db->GetName()); }
630631

@@ -2142,8 +2143,8 @@ auto Storage::stats() const -> Stats
21422143
// TODO ... more stuff here, perhaps
21432144
QVariantMap ret;
21442145
auto & c = p->db.concatOperator, & c2 = p->db.concatOperatorTxHash2TxNum;
2145-
ret["merge calls"] = c ? c->merges.load() : QVariant();
2146-
ret["merge calls (txhash2txnum)"] = c2 ? c2->merges.load() : QVariant();
2146+
ret["merge calls"] = c ? static_cast<quint64>(c->merges.load()) : QVariant();
2147+
ret["merge calls (txhash2txnum)"] = c2 ? static_cast<quint64>(c2->merges.load()) : QVariant();
21472148
QVariantMap caches;
21482149
{
21492150
QVariantMap m;

0 commit comments

Comments
 (0)