From ef0fc1b46189c51da8799ee79574fd37ced93752 Mon Sep 17 00:00:00 2001 From: Manuel Ung Date: Wed, 10 Apr 2019 15:54:53 -0700 Subject: [PATCH] Reduce copies of LockInfo (#5172) Summary: The LockInfo struct is not easy to copy because it contains std::vector. Reduce copies by using move constructor and `unordered_map::emplace`. Pull Request resolved: https://github.com/facebook/rocksdb/pull/5172 Differential Revision: D14882053 Pulled By: lth fbshipit-source-id: 93999ec6ab1a5841fb5115abb764b6c1831a6de1 --- utilities/transactions/transaction_base.cc | 2 +- utilities/transactions/transaction_lock_mgr.cc | 12 ++++++------ utilities/transactions/transaction_lock_mgr.h | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/utilities/transactions/transaction_base.cc b/utilities/transactions/transaction_base.cc index 212c82242..ac186a528 100644 --- a/utilities/transactions/transaction_base.cc +++ b/utilities/transactions/transaction_base.cc @@ -587,7 +587,7 @@ void TransactionBaseImpl::TrackKey(TransactionKeyMap* key_map, uint32_t cfh_id, auto& cf_key_map = (*key_map)[cfh_id]; auto iter = cf_key_map.find(key); if (iter == cf_key_map.end()) { - auto result = cf_key_map.insert({key, TransactionKeyMapInfo(seq)}); + auto result = cf_key_map.emplace(key, TransactionKeyMapInfo(seq)); iter = result.first; } else if (seq < iter->second.seq) { // Now tracking this key with an earlier sequence number diff --git a/utilities/transactions/transaction_lock_mgr.cc b/utilities/transactions/transaction_lock_mgr.cc index 9074a1494..43f62bafb 100644 --- a/utilities/transactions/transaction_lock_mgr.cc +++ b/utilities/transactions/transaction_lock_mgr.cc @@ -311,14 +311,14 @@ Status TransactionLockMgr::TryLock(PessimisticTransaction* txn, int64_t timeout = txn->GetLockTimeout(); return AcquireWithTimeout(txn, lock_map, stripe, column_family_id, key, env, - timeout, lock_info); + timeout, std::move(lock_info)); } // Helper function for TryLock(). Status TransactionLockMgr::AcquireWithTimeout( PessimisticTransaction* txn, LockMap* lock_map, LockMapStripe* stripe, uint32_t column_family_id, const std::string& key, Env* env, - int64_t timeout, const LockInfo& lock_info) { + int64_t timeout, LockInfo&& lock_info) { Status result; uint64_t end_time = 0; @@ -342,7 +342,7 @@ Status TransactionLockMgr::AcquireWithTimeout( // Acquire lock if we are able to uint64_t expire_time_hint = 0; autovector wait_ids; - result = AcquireLocked(lock_map, stripe, key, env, lock_info, + result = AcquireLocked(lock_map, stripe, key, env, std::move(lock_info), &expire_time_hint, &wait_ids); if (!result.ok() && timeout != 0) { @@ -407,7 +407,7 @@ Status TransactionLockMgr::AcquireWithTimeout( } if (result.ok() || result.IsTimedOut()) { - result = AcquireLocked(lock_map, stripe, key, env, lock_info, + result = AcquireLocked(lock_map, stripe, key, env, std::move(lock_info), &expire_time_hint, &wait_ids); } } while (!result.ok() && !timed_out); @@ -526,7 +526,7 @@ bool TransactionLockMgr::IncrementWaiters( Status TransactionLockMgr::AcquireLocked(LockMap* lock_map, LockMapStripe* stripe, const std::string& key, Env* env, - const LockInfo& txn_lock_info, + LockInfo&& txn_lock_info, uint64_t* expire_time, autovector* txn_ids) { assert(txn_lock_info.txn_ids.size() == 1); @@ -578,7 +578,7 @@ Status TransactionLockMgr::AcquireLocked(LockMap* lock_map, result = Status::Busy(Status::SubCode::kLockLimit); } else { // acquire lock - stripe->keys.insert({key, txn_lock_info}); + stripe->keys.emplace(key, std::move(txn_lock_info)); // Maintain lock count if there is a limit on the number of locks if (max_num_locks_) { diff --git a/utilities/transactions/transaction_lock_mgr.h b/utilities/transactions/transaction_lock_mgr.h index cb650d9fc..b18271237 100644 --- a/utilities/transactions/transaction_lock_mgr.h +++ b/utilities/transactions/transaction_lock_mgr.h @@ -130,11 +130,11 @@ class TransactionLockMgr { Status AcquireWithTimeout(PessimisticTransaction* txn, LockMap* lock_map, LockMapStripe* stripe, uint32_t column_family_id, const std::string& key, Env* env, int64_t timeout, - const LockInfo& lock_info); + LockInfo&& lock_info); Status AcquireLocked(LockMap* lock_map, LockMapStripe* stripe, const std::string& key, Env* env, - const LockInfo& lock_info, uint64_t* wait_time, + LockInfo&& lock_info, uint64_t* wait_time, autovector* txn_ids); void UnLockKey(const PessimisticTransaction* txn, const std::string& key,