Skip to content

Commit

Permalink
Optimize account_lines and account_offers (RIPD-587)
Browse files Browse the repository at this point in the history
Conflicts:
	src/ripple/app/ledger/Ledger.h
  • Loading branch information
miguelportilla authored and nbougalis committed Oct 2, 2014
1 parent 0f71b4a commit f14d75e
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 177 deletions.
101 changes: 96 additions & 5 deletions src/ripple/app/ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,89 @@ void Ledger::visitAccountItems (

}

bool Ledger::visitAccountItems (
Account const& accountID,
uint256 const& startAfter,
std::uint64_t const hint,
unsigned int limit,
std::function <bool (SLE::ref)> func) const
{
// Visit each item in this account's owner directory
uint256 const rootIndex (Ledger::getOwnerDirIndex (accountID));
uint256 currentIndex (rootIndex);

// If startAfter is not zero try jumping to that page using the hint
if (startAfter.isNonZero ())
{
uint256 const hintIndex (getDirNodeIndex (rootIndex, hint));
SLE::pointer hintDir (getSLEi (hintIndex));
if (hintDir != nullptr)
{
for (auto const& node : hintDir->getFieldV256 (sfIndexes))
{
if (node == startAfter)
{
// We found the hint, we can start here
currentIndex = hintIndex;
break;
}
}
}

bool found (false);
for (;;)
{
SLE::pointer ownerDir (getSLEi (currentIndex));

if (! ownerDir || ownerDir->getType () != ltDIR_NODE)
return found;

for (auto const& node : ownerDir->getFieldV256 (sfIndexes))
{
if (!found)
{
if (node == startAfter)
found = true;
}
else if (func (getSLEi (node)) && limit-- <= 1)
{
return found;
}
}

std::uint64_t const uNodeNext (ownerDir->getFieldU64 (sfIndexNext));

if (uNodeNext == 0)
return found;

currentIndex = Ledger::getDirNodeIndex (rootIndex, uNodeNext);
}
}
else
{
for (;;)
{
SLE::pointer ownerDir (getSLEi (currentIndex));

if (! ownerDir || ownerDir->getType () != ltDIR_NODE)
return true;

for (auto const& node : ownerDir->getFieldV256 (sfIndexes))
{
if (func (getSLEi (node)) && limit-- <= 1)
return true;
}

std::uint64_t const uNodeNext (ownerDir->getFieldU64 (sfIndexNext));

if (uNodeNext == 0)
return true;

currentIndex = Ledger::getDirNodeIndex (rootIndex, uNodeNext);
}
}
}

static void visitHelper (
std::function<void (SLE::ref)>& function, SHAMapItem::ref item)
{
Expand Down Expand Up @@ -1785,12 +1868,20 @@ uint256 Ledger::getRippleStateIndex (
{
Serializer s (62);

bool const bAltB = a < b;
s.add16 (spaceRipple); // 2

if (a < b)
{
s.add160 (a); // 20
s.add160 (b); // 20
}
else
{
s.add160 (b); // 20
s.add160 (a); // 20
}

s.add16 (spaceRipple); // 2
s.add160 (bAltB ? a : b); // 20
s.add160 (bAltB ? b : a); // 20
s.add160 (currency); // 20
s.add160 (currency); // 20

return s.getSHA512Half ();
}
Expand Down
9 changes: 8 additions & 1 deletion src/ripple/app/ledger/Ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,15 @@ class Ledger
SLE::pointer getAccountRoot (Account const& accountID) const;
SLE::pointer getAccountRoot (const RippleAddress & naAccountID) const;
void updateSkipList ();

void visitAccountItems (
Account const& acctID, std::function<void (SLE::ref)>) const;
Account const& accountID, std::function<void (SLE::ref)>) const;
bool visitAccountItems (
Account const& accountID,
uint256 const& startAfter, // Entry to start after
std::uint64_t const hint, // Hint which page to start at
unsigned int limit,
std::function <bool (SLE::ref)>) const;
void visitStateItems (std::function<void (SLE::ref)>) const;

// database functions (low-level)
Expand Down
Loading

0 comments on commit f14d75e

Please sign in to comment.