Skip to content

Commit 94decc7

Browse files
authored
perf: Move mutex to the partition level (#5486)
This change introduces two key optimizations: * Mutex scope reduction: Limits the lock to individual partitions within `TaggedCache`, reducing contention. * Decoupling: Removes the tight coupling between `LedgerHistory` and `TaggedCache`, improving modularity and testability. Lock contention analysis based on eBPF showed significant improvements as a result of this change.
1 parent 9918916 commit 94decc7

File tree

8 files changed

+135
-143
lines changed

8 files changed

+135
-143
lines changed

include/xrpl/basics/SHAMapHash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#define RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
2222

2323
#include <xrpl/basics/base_uint.h>
24-
#include <xrpl/basics/partitioned_unordered_map.h>
2524

2625
#include <ostream>
2726

include/xrpl/basics/TaggedCache.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ class TaggedCache
9090
int
9191
getCacheSize() const;
9292

93-
int
94-
getTrackSize() const;
95-
9693
float
9794
getHitRate();
9895

@@ -170,9 +167,6 @@ class TaggedCache
170167
bool
171168
retrieve(key_type const& key, T& data);
172169

173-
mutex_type&
174-
peekMutex();
175-
176170
std::vector<key_type>
177171
getKeys() const;
178172

@@ -193,11 +187,14 @@ class TaggedCache
193187

194188
private:
195189
SharedPointerType
196-
initialFetch(key_type const& key, std::lock_guard<mutex_type> const& l);
190+
initialFetch(key_type const& key);
197191

198192
void
199193
collect_metrics();
200194

195+
Mutex&
196+
lockPartition(key_type const& key) const;
197+
201198
private:
202199
struct Stats
203200
{
@@ -300,8 +297,8 @@ class TaggedCache
300297
[[maybe_unused]] clock_type::time_point const& now,
301298
typename KeyValueCacheType::map_type& partition,
302299
SweptPointersVector& stuffToSweep,
303-
std::atomic<int>& allRemovals,
304-
std::lock_guard<std::recursive_mutex> const&);
300+
std::atomic<int>& allRemoval,
301+
Mutex& partitionLock);
305302

306303
[[nodiscard]] std::thread
307304
sweepHelper(
@@ -310,14 +307,12 @@ class TaggedCache
310307
typename KeyOnlyCacheType::map_type& partition,
311308
SweptPointersVector&,
312309
std::atomic<int>& allRemovals,
313-
std::lock_guard<std::recursive_mutex> const&);
310+
Mutex& partitionLock);
314311

315312
beast::Journal m_journal;
316313
clock_type& m_clock;
317314
Stats m_stats;
318315

319-
mutex_type mutable m_mutex;
320-
321316
// Used for logging
322317
std::string m_name;
323318

@@ -328,10 +323,11 @@ class TaggedCache
328323
clock_type::duration const m_target_age;
329324

330325
// Number of items cached
331-
int m_cache_count;
326+
std::atomic<int> m_cache_count;
332327
cache_type m_cache; // Hold strong reference to recent objects
333-
std::uint64_t m_hits;
334-
std::uint64_t m_misses;
328+
std::atomic<std::uint64_t> m_hits;
329+
std::atomic<std::uint64_t> m_misses;
330+
mutable std::vector<mutex_type> partitionLocks_;
335331
};
336332

337333
} // namespace ripple

0 commit comments

Comments
 (0)