diff --git a/include/xrpl/basics/TaggedCache.h b/include/xrpl/basics/TaggedCache.h index 0037ce2b1e..2b0830df06 100644 --- a/include/xrpl/basics/TaggedCache.h +++ b/include/xrpl/basics/TaggedCache.h @@ -127,7 +127,8 @@ public: @param key The key corresponding to the object @param data A shared pointer to the data corresponding to the object. - @param replaceCallback Function that decides if cache should be replaced + @param shouldReplaceCheckCb Function that decides if cache should be + replaced @return First item: `true` If the key already existed; Second item: The canonicalized item. @@ -137,7 +138,7 @@ public: canonicalize( key_type const& key, SharedPointerType const& data, - R&& replaceCallback); + R&& shouldReplaceCheckCb); bool canonicalize_replace_cache( diff --git a/include/xrpl/basics/TaggedCache.ipp b/include/xrpl/basics/TaggedCache.ipp index fd8ee39b21..d1870fe17c 100644 --- a/include/xrpl/basics/TaggedCache.ipp +++ b/include/xrpl/basics/TaggedCache.ipp @@ -435,27 +435,31 @@ TaggedCache< Entry& entry = cit->second; entry.touch(m_clock.now()); - auto replaceEntryIfNecessary = [&] { - bool shouldReplace = false; + auto shouldReplace = [&] { if constexpr (std::is_invocable_r_v) { // The reason for this extra complexity is for intrusive // strong/weak combo getting a strong is relatively expensive // and not needed for many cases. - shouldReplace = replaceCallback(); + return replaceCallback(); } else { - shouldReplace = replaceCallback(entry.ptr.getStrong()); + return replaceCallback(entry.ptr.getStrong()); } - - if (shouldReplace) - entry.ptr = data; }; if (entry.isCached()) { - replaceEntryIfNecessary(); + if (shouldReplace()) + { + entry.ptr = data; + } + else + { + data = entry.ptr.getStrong(); + } + return std::make_pair(true, entry.ptr.getStrong()); } @@ -463,8 +467,16 @@ TaggedCache< if (cachedData) { - replaceEntryIfNecessary(); - entry.ptr.convertToStrong(); + if (shouldReplace()) + { + entry.ptr = data; + } + else + { + entry.ptr.convertToStrong(); + data = cachedData; + } + ++m_cache_count; return std::make_pair(true, entry.ptr.getStrong()); }