From 9c23371c46f0d4e86ac4325cc5b4828b80ee9267 Mon Sep 17 00:00:00 2001 From: JCW Date: Wed, 1 Apr 2026 21:04:20 +0100 Subject: [PATCH] Address PR comments --- include/xrpl/basics/TaggedCache.h | 14 +------------- include/xrpl/basics/TaggedCache.ipp | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/include/xrpl/basics/TaggedCache.h b/include/xrpl/basics/TaggedCache.h index f00b9889bb..4c22829317 100644 --- a/include/xrpl/basics/TaggedCache.h +++ b/include/xrpl/basics/TaggedCache.h @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace xrpl { @@ -97,19 +98,6 @@ public: del(key_type const& key, bool valid); public: - /** Replace aliased objects with originals. - - Due to concurrency it is possible for two separate objects with - the same content and referring to the same unique "thing" to exist. - These routines eliminate the duplicate and perform a replacement - as needed. - - @param key The key corresponding to the object - @param data A shared pointer to the data corresponding to the object. - - @return `true` If the key already existed. - */ - /** Replace the cache entry with the caller's data. */ bool canonicalize_replace_cache(key_type const& key, SharedPointerType const& data); diff --git a/include/xrpl/basics/TaggedCache.ipp b/include/xrpl/basics/TaggedCache.ipp index 3bd81d6dc3..de4a9db6a3 100644 --- a/include/xrpl/basics/TaggedCache.ipp +++ b/include/xrpl/basics/TaggedCache.ipp @@ -318,21 +318,25 @@ inline std::pair< TaggedCache:: touchOrInsert(key_type const& key, SharedPointerType const& data) { - auto [it, inserted] = m_cache.emplace( - std::piecewise_construct, - std::forward_as_tuple(key), - std::forward_as_tuple(m_clock.now(), data)); + auto cit = m_cache.find(key); + bool inserted = false; - if (inserted) + if (cit == m_cache.end()) { + inserted = true; + auto emplaceResult = m_cache.emplace( + std::piecewise_construct, + std::forward_as_tuple(key), + std::forward_as_tuple(m_clock.now(), data)); + ++m_cache_count; - } - else - { - it->second.touch(m_clock.now()); + cit = emplaceResult.first; + return {cit, inserted}; } - return {it, inserted}; + cit->second.touch(m_clock.now()); + + return {cit, inserted}; } template <