Skip to content

Commit 1a737eb

Browse files
remove recursion from the ApplyStateTable::read and add flat call ApplyStateTable::readLocal
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
1 parent d6e9986 commit 1a737eb

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

include/xrpl/ledger/detail/ApplyStateTable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class ApplyStateTable
6464
std::shared_ptr<SLE const>
6565
read(ReadView const& base, Keylet const& k) const;
6666

67+
/** Check only local items without delegating to base.
68+
Returns std::nullopt if key not found locally. */
69+
std::optional<std::shared_ptr<SLE const>>
70+
readLocal(Keylet const& k) const;
71+
6772
std::shared_ptr<SLE>
6873
peek(ReadView const& base, Keylet const& k);
6974

src/libxrpl/ledger/ApplyStateTable.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ ApplyStateTable::read(ReadView const& base, Keylet const& k) const
330330
return sle;
331331
}
332332

333+
std::optional<std::shared_ptr<SLE const>>
334+
ApplyStateTable::readLocal(Keylet const& k) const
335+
{
336+
auto const iter = items_.find(k.key);
337+
if (iter == items_.end())
338+
return std::nullopt;
339+
auto const& item = iter->second;
340+
auto const& sle = item.second;
341+
switch (item.first)
342+
{
343+
case Action::erase:
344+
return nullptr;
345+
case Action::cache:
346+
case Action::insert:
347+
case Action::modify:
348+
break;
349+
};
350+
if (!k.check(*sle))
351+
return nullptr;
352+
return sle;
353+
}
354+
333355
std::shared_ptr<SLE>
334356
ApplyStateTable::peek(ReadView const& base, Keylet const& k)
335357
{

src/libxrpl/ledger/ApplyViewBase.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,20 @@ ApplyViewBase::succ(key_type const& key, std::optional<key_type> const& last) co
4848
std::shared_ptr<SLE const>
4949
ApplyViewBase::read(Keylet const& k) const
5050
{
51-
return items_.read(*base_, k);
51+
// Iteratively walk up the chain of ApplyViewBase layers
52+
// instead of recursing through items_.read(*base_, k).
53+
auto const* current = this;
54+
while (current)
55+
{
56+
if (auto result = current->items_.readLocal(k))
57+
return *result;
58+
59+
// Check if the base is another ApplyViewBase layer
60+
auto const* next = dynamic_cast<ApplyViewBase const*>(current->base_);
61+
if (!next)
62+
return current->base_->read(k);
63+
current = next;
64+
}
5265
}
5366

5467
auto

0 commit comments

Comments
 (0)