From da3b2840cb20639ef9f468d9b96f1285cbd049f8 Mon Sep 17 00:00:00 2001 From: jsteemann Date: Mon, 7 Oct 2019 12:21:39 -0700 Subject: [PATCH] save a few redundant container lookups (#5875) Summary: This PR eliminates repeated lookups in associative or ordered containers when a single lookup suffices. Pull Request resolved: https://github.com/facebook/rocksdb/pull/5875 Differential Revision: D17753172 Pulled By: anand1976 fbshipit-source-id: 796b02b760082521d8c42a1cb65a76bf0e6c1b8e --- db/version_builder.cc | 10 ++++------ utilities/transactions/pessimistic_transaction.cc | 7 ++++--- utilities/transactions/transaction_base.cc | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/db/version_builder.cc b/db/version_builder.cc index 8a8aefcb8..53e25a446 100644 --- a/db/version_builder.cc +++ b/db/version_builder.cc @@ -303,10 +303,7 @@ class VersionBuilder::Rep { levels_[level].added_files.erase(exising); } } else { - auto exising = invalid_levels_[level].find(number); - if (exising != invalid_levels_[level].end()) { - invalid_levels_[level].erase(exising); - } else { + if (invalid_levels_[level].erase(number) == 0) { // Deleting an non-existing file on invalid level. has_invalid_levels_ = true; } @@ -326,8 +323,9 @@ class VersionBuilder::Rep { levels_[level].added_files[f->fd.GetNumber()] = f; } else { uint64_t number = new_file.second.fd.GetNumber(); - if (invalid_levels_[level].count(number) == 0) { - invalid_levels_[level].insert(number); + auto& lvls = invalid_levels_[level]; + if (lvls.count(number) == 0) { + lvls.insert(number); } else { // Creating an already existing file on invalid level. has_invalid_levels_ = true; diff --git a/utilities/transactions/pessimistic_transaction.cc b/utilities/transactions/pessimistic_transaction.cc index 1c0e2f063..c709d34b1 100644 --- a/utilities/transactions/pessimistic_transaction.cc +++ b/utilities/transactions/pessimistic_transaction.cc @@ -473,10 +473,11 @@ Status PessimisticTransaction::LockBatch(WriteBatch* batch, void RecordKey(uint32_t column_family_id, const Slice& key) { std::string key_str = key.ToString(); - auto iter = (keys_)[column_family_id].find(key_str); - if (iter == (keys_)[column_family_id].end()) { + auto& cfh_keys = keys_[column_family_id]; + auto iter = cfh_keys.find(key_str); + if (iter == cfh_keys.end()) { // key not yet seen, store it. - (keys_)[column_family_id].insert({std::move(key_str)}); + cfh_keys.insert({std::move(key_str)}); } } diff --git a/utilities/transactions/transaction_base.cc b/utilities/transactions/transaction_base.cc index 637a38b9a..c10c1795f 100644 --- a/utilities/transactions/transaction_base.cc +++ b/utilities/transactions/transaction_base.cc @@ -170,7 +170,7 @@ Status TransactionBaseImpl::RollbackToSavePoint() { } if (tracked_keys_iter->second.num_reads == 0 && tracked_keys_iter->second.num_writes == 0) { - tracked_keys_[column_family_id].erase(tracked_keys_iter); + cf_tracked_keys.erase(tracked_keys_iter); } } }