From 3726f8bf31b3eab8bab39dce139656fd705ae9a0 Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Mon, 18 Jul 2022 11:31:36 -0400 Subject: [PATCH] Release TaggedCache object memory outside the lock --- src/ripple/basics/TaggedCache.h | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ripple/basics/TaggedCache.h b/src/ripple/basics/TaggedCache.h index d9a1b542e..6765ff16b 100644 --- a/src/ripple/basics/TaggedCache.h +++ b/src/ripple/basics/TaggedCache.h @@ -196,14 +196,17 @@ public: return true; } + using SweptPointersVector = std::pair< + std::vector>, + std::vector>>; + void sweep() { // Keep references to all the stuff we sweep // For performance, each worker thread should exit before the swept data // is destroyed but still within the main cache lock. - std::vector>> allStuffToSweep( - m_cache.partitions()); + std::vector allStuffToSweep(m_cache.partitions()); clock_type::time_point const now(m_clock.now()); clock_type::time_point when_expire; @@ -652,9 +655,9 @@ private: clock_type::time_point const& when_expire, [[maybe_unused]] clock_type::time_point const& now, typename KeyValueCacheType::map_type& partition, - std::vector>& stuffToSweep, + SweptPointersVector& stuffToSweep, std::atomic& allRemovals, - std::lock_guard const& lock) + std::lock_guard const&) { return std::thread([&, this]() { int cacheRemovals = 0; @@ -662,7 +665,8 @@ private: // Keep references to all the stuff we sweep // so that we can destroy them outside the lock. - stuffToSweep.reserve(partition.size()); + stuffToSweep.first.reserve(partition.size()); + stuffToSweep.second.reserve(partition.size()); { auto cit = partition.begin(); while (cit != partition.end()) @@ -672,6 +676,8 @@ private: // weak if (cit->second.isExpired()) { + stuffToSweep.second.push_back( + std::move(cit->second.weak_ptr)); ++mapRemovals; cit = partition.erase(cit); } @@ -686,7 +692,8 @@ private: ++cacheRemovals; if (cit->second.ptr.use_count() == 1) { - stuffToSweep.push_back(cit->second.ptr); + stuffToSweep.first.push_back( + std::move(cit->second.ptr)); ++mapRemovals; cit = partition.erase(cit); } @@ -722,9 +729,9 @@ private: clock_type::time_point const& when_expire, clock_type::time_point const& now, typename KeyOnlyCacheType::map_type& partition, - std::vector>& stuffToSweep, + SweptPointersVector&, std::atomic& allRemovals, - std::lock_guard const& lock) + std::lock_guard const&) { return std::thread([&, this]() { int cacheRemovals = 0; @@ -732,7 +739,6 @@ private: // Keep references to all the stuff we sweep // so that we can destroy them outside the lock. - stuffToSweep.reserve(partition.size()); { auto cit = partition.begin(); while (cit != partition.end())