Compare commits

...

1 Commits

Author SHA1 Message Date
Pratik Mankawde
f993e32e07 added iterative loop instead of recursion
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-17 18:05:56 +00:00
3 changed files with 29 additions and 18 deletions

View File

@@ -64,6 +64,11 @@ public:
std::shared_ptr<SLE const>
read(ReadView const& base, Keylet const& k) const;
/** Check only local items without delegating to base.
Returns std::nullopt if key not found locally. */
std::optional<std::shared_ptr<SLE const>>
readLocal(Keylet const& k) const;
std::shared_ptr<SLE>
peek(ReadView const& base, Keylet const& k);

View File

@@ -330,24 +330,12 @@ ApplyStateTable::read(ReadView const& base, Keylet const& k) const
return sle;
}
std::shared_ptr<SLE>
ApplyStateTable::peek(ReadView const& base, Keylet const& k)
std::optional<std::shared_ptr<SLE const>>
ApplyStateTable::readLocal(Keylet const& k) const
{
auto iter = items_.lower_bound(k.key);
if (iter == items_.end() || iter->first != k.key)
{
auto const sle = base.read(k);
if (!sle)
return nullptr;
// Make our own copy
using namespace std;
iter = items_.emplace_hint(
iter,
piecewise_construct,
forward_as_tuple(sle->key()),
forward_as_tuple(Action::cache, make_shared<SLE>(*sle)));
return iter->second.second;
}
auto const iter = items_.find(k.key);
if (iter == items_.end())
return std::nullopt;
auto const& item = iter->second;
auto const& sle = item.second;
switch (item.first)
@@ -361,6 +349,7 @@ ApplyStateTable::peek(ReadView const& base, Keylet const& k)
};
if (!k.check(*sle))
return nullptr;
return sle;
}

View File

@@ -48,7 +48,24 @@ ApplyViewBase::succ(key_type const& key, std::optional<key_type> const& last) co
std::shared_ptr<SLE const>
ApplyViewBase::read(Keylet const& k) const
{
return items_.read(*base_, k);
// Iteratively walk up the chain of ApplyViewBase layers
// instead of recursing through items_.read(*base_, k).
auto const* current = this;
while (current)
{
if (auto result = current->items_.readLocal(k))
return *result;
// Check if the base is another ApplyViewBase layer
auto const* next = dynamic_cast<ApplyViewBase const*>(current->base_);
if (!next)
return current->base_->read(k);
current = next;
}
// Unreachable: current starts as `this` (non-null) and the loop
// always returns before current could become null.
UNREACHABLE("xrpl::ApplyViewBase::read : unreachable");
return nullptr;
}
auto