mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-08 19:26:45 +00:00
Compare commits
26 Commits
bthomee/pr
...
a1q123456/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13039e1dfe | ||
|
|
6fab4d867f | ||
|
|
36ec0fb275 | ||
|
|
8c7e29a13c | ||
|
|
014d8caaa7 | ||
|
|
fe996913f5 | ||
|
|
b7a066caa1 | ||
|
|
79d19f392c | ||
|
|
b3e1937244 | ||
|
|
e7f3241af3 | ||
|
|
9c23371c46 | ||
|
|
f2edebd919 | ||
|
|
a7ee324153 | ||
|
|
39c66b4da0 | ||
|
|
0bcf43a26e | ||
|
|
9b42bd288a | ||
|
|
95969cb95e | ||
|
|
bf4dc342c6 | ||
|
|
a71cd5d271 | ||
|
|
45baf7339c | ||
|
|
2bc2930a28 | ||
|
|
e837171f7c | ||
|
|
6f05bd035c | ||
|
|
b98b42bbec | ||
|
|
6e6ea4311b | ||
|
|
f2271305e5 |
@@ -9,6 +9,7 @@
|
||||
#include <xrpl/beast/insight/Insight.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
@@ -17,6 +18,22 @@
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Replace-policy tags selecting how TaggedCache::canonicalizeImpl resolves a
|
||||
// collision when the key already exists (defined in TaggedCache.ipp):
|
||||
// - ReplaceCached: always replace the cached value with `data`. `data` is
|
||||
// never written back and may be const.
|
||||
// - ReplaceClient: keep the cached value and write it back into `data` (the
|
||||
// client's pointer), which must therefore be writable.
|
||||
// - ReplaceDynamically: call the supplied callback to decide per call; `data`
|
||||
// is written back when the cached value is kept, so it must be writable.
|
||||
struct ReplaceCached;
|
||||
struct ReplaceClient;
|
||||
struct ReplaceDynamically;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/** Map/cache combination.
|
||||
This class implements a cache and a map. The cache keeps objects alive
|
||||
in the map. The map allows multiple code paths that reference objects
|
||||
@@ -96,6 +113,32 @@ public:
|
||||
bool
|
||||
del(key_type const& key, bool valid);
|
||||
|
||||
private:
|
||||
// Selects the `data` parameter type of canonicalizeImpl from the replace
|
||||
// policy: const for detail::ReplaceCached (never written back), otherwise
|
||||
// writable.
|
||||
template <typename Policy>
|
||||
using CanonicalizeClientPointerType = std::conditional_t<
|
||||
std::is_same_v<detail::ReplaceCached, Policy>,
|
||||
SharedPointerType const&,
|
||||
SharedPointerType&>;
|
||||
|
||||
/** Shared implementation of the canonicalize family.
|
||||
|
||||
`policy` selects how a collision is resolved when `key` already exists:
|
||||
detail::ReplaceCached, detail::ReplaceClient or
|
||||
detail::ReplaceDynamically. For ReplaceDynamically `replaceCallback` is
|
||||
invoked with the existing strong pointer and returns whether to replace
|
||||
the cached value with `data`; for the tag policies it is unused.
|
||||
*/
|
||||
template <class Policy, class Callback = std::nullptr_t>
|
||||
bool
|
||||
canonicalizeImpl(
|
||||
key_type const& key,
|
||||
CanonicalizeClientPointerType<Policy> data,
|
||||
Policy policy,
|
||||
Callback&& replaceCallback = nullptr);
|
||||
|
||||
public:
|
||||
/** Replace aliased objects with originals.
|
||||
|
||||
@@ -104,19 +147,49 @@ public:
|
||||
This routine eliminates the duplicate and performs a replacement
|
||||
on the callers shared pointer if needed.
|
||||
|
||||
`replaceCallback` is a callable taking the existing strong pointer and
|
||||
returning whether to replace the cached value with `data` (true) or to
|
||||
keep the cached value and write it back into `data` (false). Because the
|
||||
write-back case mutates `data`, `data` must be writable.
|
||||
|
||||
@param key The key corresponding to the object
|
||||
@param data A shared pointer to the data corresponding to the object.
|
||||
@param replace Function that decides if cache should be replaced
|
||||
@param replaceCallback A callable (existing strong pointer -> bool).
|
||||
|
||||
@return `true` If the key already existed.
|
||||
*/
|
||||
template <class R>
|
||||
template <class Callback>
|
||||
bool
|
||||
canonicalize(key_type const& key, SharedPointerType& data, R&& replaceCallback);
|
||||
canonicalize(key_type const& key, SharedPointerType& data, Callback&& replaceCallback);
|
||||
|
||||
/** Insert/update the canonical entry for `key`, always replacing the
|
||||
cached value with `data`.
|
||||
|
||||
If an entry already exists for `key`, the cached value is unconditionally
|
||||
replaced with `data`; otherwise `data` is inserted. `data` is never
|
||||
written back, so it may be const.
|
||||
|
||||
@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.
|
||||
*/
|
||||
bool
|
||||
canonicalizeReplaceCache(key_type const& key, SharedPointerType const& data);
|
||||
|
||||
/** Insert the canonical entry for `key`, keeping any existing cached value.
|
||||
|
||||
If an entry already exists for `key`, the cached value is kept and
|
||||
written back into `data` so the caller ends up with the canonical
|
||||
object; otherwise `data` is inserted. Because `data` may be overwritten
|
||||
it must be writable.
|
||||
|
||||
@param key The key corresponding to the object.
|
||||
@param data A shared pointer to the data corresponding to the object;
|
||||
updated to the canonical value when one already exists.
|
||||
|
||||
@return `true` If the key already existed.
|
||||
*/
|
||||
bool
|
||||
canonicalizeReplaceClient(key_type const& key, SharedPointerType& data);
|
||||
|
||||
|
||||
@@ -5,6 +5,30 @@
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Replace-policy tags selecting how TaggedCache::canonicalizeImpl resolves a
|
||||
// collision when the key already exists:
|
||||
// - ReplaceCached: always replace the cached value with `data`. `data` is
|
||||
// never written back and may be const.
|
||||
// - ReplaceClient: keep the cached value and write it back into `data` (the
|
||||
// client's pointer), which must therefore be writable.
|
||||
// - ReplaceDynamically: call the supplied callback to decide per call; `data`
|
||||
// is written back when the cached value is kept, so it must be writable.
|
||||
struct ReplaceCached
|
||||
{
|
||||
};
|
||||
|
||||
struct ReplaceClient
|
||||
{
|
||||
};
|
||||
|
||||
struct ReplaceDynamically
|
||||
{
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class T,
|
||||
@@ -300,13 +324,29 @@ template <
|
||||
class Hash,
|
||||
class KeyEqual,
|
||||
class Mutex>
|
||||
template <class R>
|
||||
template <class Policy, class Callback>
|
||||
inline bool
|
||||
TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash, KeyEqual, Mutex>::
|
||||
canonicalize(key_type const& key, SharedPointerType& data, R&& replaceCallback)
|
||||
canonicalizeImpl(
|
||||
key_type const& key,
|
||||
CanonicalizeClientPointerType<Policy> data,
|
||||
[[maybe_unused]] Policy policy,
|
||||
[[maybe_unused]] Callback&& replaceCallback)
|
||||
{
|
||||
// Return canonical value, store if needed, refresh in cache
|
||||
// Return values: true=we had the data already
|
||||
|
||||
// `Policy` is one of:
|
||||
// - detail::ReplaceCached: always replace the cached value with `data`;
|
||||
// `data` is never written back and may be const.
|
||||
// - detail::ReplaceClient: keep the cached value and write it back into
|
||||
// `data` (the client's pointer), which must therefore be writable.
|
||||
// - detail::ReplaceDynamically: call `replaceCallback` to decide at run
|
||||
// time; `data` must be writable.
|
||||
// For the latter two the write-back below requires a mutable `data`, so
|
||||
// passing a const argument is a compile error.
|
||||
constexpr bool replaceCached = std::is_same_v<Policy, detail::ReplaceCached>;
|
||||
|
||||
std::scoped_lock const lock(mutex_);
|
||||
|
||||
auto cit = cache_.find(key);
|
||||
@@ -324,13 +364,14 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
|
||||
Entry& entry = cit->second;
|
||||
entry.touch(clock_.now());
|
||||
|
||||
auto shouldReplace = [&] {
|
||||
if constexpr (std::is_invocable_r_v<bool, R>)
|
||||
auto shouldReplaceCached = [&] {
|
||||
if constexpr (replaceCached)
|
||||
{
|
||||
// The reason for this extra complexity is for intrusive
|
||||
// strong/weak combo getting a strong is relatively expensive
|
||||
// and not needed for many cases.
|
||||
return replaceCallback();
|
||||
return true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Policy, detail::ReplaceClient>)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -340,11 +381,11 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
|
||||
|
||||
if (entry.isCached())
|
||||
{
|
||||
if (shouldReplace())
|
||||
if (shouldReplaceCached())
|
||||
{
|
||||
entry.ptr = data;
|
||||
}
|
||||
else
|
||||
else if constexpr (!replaceCached)
|
||||
{
|
||||
data = entry.ptr.getStrong();
|
||||
}
|
||||
@@ -356,11 +397,11 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
|
||||
|
||||
if (cachedData)
|
||||
{
|
||||
if (shouldReplace())
|
||||
if (shouldReplaceCached())
|
||||
{
|
||||
entry.ptr = data;
|
||||
}
|
||||
else
|
||||
else if constexpr (!replaceCached)
|
||||
{
|
||||
entry.ptr.convertToStrong();
|
||||
data = cachedData;
|
||||
@@ -376,6 +417,24 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
|
||||
return false;
|
||||
}
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class T,
|
||||
bool IsKeyCache,
|
||||
class SharedWeakUnionPointer,
|
||||
class SharedPointerType,
|
||||
class Hash,
|
||||
class KeyEqual,
|
||||
class Mutex>
|
||||
template <class Callback>
|
||||
inline bool
|
||||
TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash, KeyEqual, Mutex>::
|
||||
canonicalize(key_type const& key, SharedPointerType& data, Callback&& replaceCallback)
|
||||
{
|
||||
return canonicalizeImpl(
|
||||
key, data, detail::ReplaceDynamically{}, std::forward<Callback>(replaceCallback));
|
||||
}
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class T,
|
||||
@@ -389,7 +448,7 @@ inline bool
|
||||
TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash, KeyEqual, Mutex>::
|
||||
canonicalizeReplaceCache(key_type const& key, SharedPointerType const& data)
|
||||
{
|
||||
return canonicalize(key, const_cast<SharedPointerType&>(data), []() { return true; });
|
||||
return canonicalizeImpl(key, data, detail::ReplaceCached{});
|
||||
}
|
||||
|
||||
template <
|
||||
@@ -405,7 +464,7 @@ inline bool
|
||||
TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash, KeyEqual, Mutex>::
|
||||
canonicalizeReplaceClient(key_type const& key, SharedPointerType& data)
|
||||
{
|
||||
return canonicalize(key, data, []() { return false; });
|
||||
return canonicalizeImpl(key, data, detail::ReplaceClient{});
|
||||
}
|
||||
|
||||
template <
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <test/unit_test/SuiteJournal.h>
|
||||
|
||||
#include <xrpl/basics/IntrusivePointer.h>
|
||||
#include <xrpl/basics/IntrusiveRefCounts.h>
|
||||
#include <xrpl/basics/TaggedCache.h>
|
||||
#include <xrpl/basics/TaggedCache.ipp> // IWYU pragma: keep
|
||||
#include <xrpl/basics/chrono.h>
|
||||
@@ -8,6 +10,7 @@
|
||||
#include <xrpl/protocol/Protocol.h>
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
@@ -133,6 +136,113 @@ public:
|
||||
BEAST_EXPECT(c.getCacheSize() == 0);
|
||||
BEAST_EXPECT(c.getTrackSize() == 0);
|
||||
}
|
||||
{
|
||||
BEAST_EXPECT(!c.insert(5, "five"));
|
||||
BEAST_EXPECT(c.getCacheSize() == 1);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
|
||||
{
|
||||
auto const p1 = c.fetch(5);
|
||||
BEAST_EXPECT(p1 != nullptr);
|
||||
BEAST_EXPECT(c.getCacheSize() == 1);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
|
||||
// Advance the clock a lot
|
||||
++clock;
|
||||
c.sweep();
|
||||
BEAST_EXPECT(c.getCacheSize() == 0);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
|
||||
auto p2 = std::make_shared<std::string>("five_2");
|
||||
BEAST_EXPECT(c.canonicalizeReplaceCache(5, p2));
|
||||
BEAST_EXPECT(c.getCacheSize() == 1);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// Make sure the caller's original pointer is unchanged
|
||||
BEAST_EXPECT(p1.get() != p2.get());
|
||||
BEAST_EXPECT(*p2 == "five_2");
|
||||
|
||||
auto const p3 = c.fetch(5);
|
||||
BEAST_EXPECT(p3 != nullptr);
|
||||
BEAST_EXPECT(p3.get() == p2.get());
|
||||
BEAST_EXPECT(p3.get() != p1.get());
|
||||
}
|
||||
|
||||
++clock;
|
||||
c.sweep();
|
||||
BEAST_EXPECT(c.getCacheSize() == 0);
|
||||
BEAST_EXPECT(c.size() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
testcase("intrptr");
|
||||
|
||||
struct MyRefCountObject : IntrusiveRefCounts
|
||||
{
|
||||
std::string data;
|
||||
|
||||
// Needed to support weak intrusive pointers
|
||||
virtual void
|
||||
partialDestructor() {};
|
||||
|
||||
MyRefCountObject() = default;
|
||||
explicit MyRefCountObject(std::string data) : data(std::move(data))
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
operator==(std::string const& other) const
|
||||
{
|
||||
return data == other;
|
||||
}
|
||||
};
|
||||
|
||||
using IntrPtrCache = TaggedCache<
|
||||
Key,
|
||||
MyRefCountObject,
|
||||
/*IsKeyCache*/ false,
|
||||
intr_ptr::SharedWeakUnionPtr<MyRefCountObject>,
|
||||
intr_ptr::SharedPtr<MyRefCountObject>>;
|
||||
|
||||
IntrPtrCache intrPtrCache("IntrPtrTest", 1, 1s, clock, journal);
|
||||
|
||||
intrPtrCache.canonicalizeReplaceCache(1, intr_ptr::makeShared<MyRefCountObject>("one"));
|
||||
BEAST_EXPECT(intrPtrCache.getCacheSize() == 1);
|
||||
BEAST_EXPECT(intrPtrCache.size() == 1);
|
||||
|
||||
{
|
||||
{
|
||||
intrPtrCache.canonicalizeReplaceCache(
|
||||
1, intr_ptr::makeShared<MyRefCountObject>("one_replaced"));
|
||||
|
||||
auto p = intrPtrCache.fetch(1);
|
||||
BEAST_EXPECT(*p == "one_replaced");
|
||||
|
||||
// Advance the clock a lot
|
||||
++clock;
|
||||
intrPtrCache.sweep();
|
||||
BEAST_EXPECT(intrPtrCache.getCacheSize() == 0);
|
||||
BEAST_EXPECT(intrPtrCache.size() == 1);
|
||||
|
||||
intrPtrCache.canonicalizeReplaceCache(
|
||||
1, intr_ptr::makeShared<MyRefCountObject>("one_replaced_2"));
|
||||
|
||||
auto p2 = intrPtrCache.fetch(1);
|
||||
BEAST_EXPECT(*p2 == "one_replaced_2");
|
||||
|
||||
intrPtrCache.del(1, true);
|
||||
}
|
||||
|
||||
intrPtrCache.canonicalizeReplaceCache(
|
||||
1, intr_ptr::makeShared<MyRefCountObject>("one_replaced_3"));
|
||||
auto p3 = intrPtrCache.fetch(1);
|
||||
BEAST_EXPECT(*p3 == "one_replaced_3");
|
||||
}
|
||||
|
||||
++clock;
|
||||
intrPtrCache.sweep();
|
||||
BEAST_EXPECT(intrPtrCache.getCacheSize() == 0);
|
||||
BEAST_EXPECT(intrPtrCache.size() == 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user