fix: Allocate TaggedCache::getKeys() memory outside of lock

- Uses a loop in case the size grows while the lock is free. Guarantees
  the result vector will not need to allocate under lock.
This commit is contained in:
Ed Hennis
2026-06-17 13:06:40 -04:00
parent 7b9d55326d
commit 9b3dd7002d

View File

@@ -2,6 +2,7 @@
#include <xrpl/basics/IntrusivePointer.ipp>
#include <xrpl/basics/TaggedCache.h>
#include <xrpl/basics/scope.h>
namespace xrpl {
@@ -536,8 +537,15 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
std::vector<key_type> v;
{
std::scoped_lock const lock(mutex_);
v.reserve(cache_.size());
std::unique_lock lock(mutex_);
for (int size = cache_.size(); v.capacity() < size; size = cache_.size())
{
ScopeUnlock const unlock(lock);
v.reserve(size);
}
XRPL_ASSERT(lock.owns_lock(), "xrpl::TaggedCache::getKeys(): owns lock");
XRPL_ASSERT(
v.capacity() >= cache_.size(), "xrpl::TaggedCache::getKeys(): sufficient capacity");
for (auto const& _ : cache_)
v.push_back(_.first);
}