Skip to content

Commit

Permalink
Improve AccountID string conversion caching:
Browse files Browse the repository at this point in the history
Caching the base58check encoded version of an `AccountID` has
performance advantages, because because of the computationally
heavy cost associated with the conversion, which requires the
application of SHA-256 twice.

This commit makes the cache significantly more efficient in terms
of memory used: it eliminates the map, using a vector with a size
that is determined by the configured size of the node, and a hash
function to directly map any given `AccountID` to a specific slot
in the cache; the eviction policy is simple: in case of collision
the existing entry is removed and replaced with the new data.

Previously, use of the cache was optional and required additional
effort by the programmer. Now the cache is automatic and does not
require any additional work or information.

The new cache also utilizes a 64-way spinlock, to help reduce any
contention that the pressure on the cache would impose.
  • Loading branch information
nbougalis committed Aug 25, 2022
1 parent 5a15229 commit e2eed96
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 196 deletions.
4 changes: 1 addition & 3 deletions src/ripple/app/ledger/AcceptedLedger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ AcceptedLedger::AcceptedLedger(
transactions_.reserve(256);

auto insertAll = [&](auto const& txns) {
auto const& idcache = app.accountIDCache();

for (auto const& item : txns)
transactions_.emplace_back(std::make_unique<AcceptedLedgerTx>(
ledger, item.first, item.second, idcache));
ledger, item.first, item.second));
};

if (app.config().reporting())
Expand Down
5 changes: 2 additions & 3 deletions src/ripple/app/ledger/AcceptedLedgerTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace ripple {
AcceptedLedgerTx::AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const& txn,
std::shared_ptr<STObject const> const& met,
AccountIDCache const& accountCache)
std::shared_ptr<STObject const> const& met)
: mTxn(txn)
, mMeta(txn->getTransactionID(), ledger->seq(), *met)
, mAffected(mMeta.getAffectedAccounts())
Expand All @@ -52,7 +51,7 @@ AcceptedLedgerTx::AcceptedLedgerTx(
{
Json::Value& affected = (mJson[jss::affected] = Json::arrayValue);
for (auto const& account : mAffected)
affected.append(accountCache.toBase58(account));
affected.append(toBase58(account));
}

if (mTxn->getTxnType() == ttOFFER_CREATE)
Expand Down
3 changes: 1 addition & 2 deletions src/ripple/app/ledger/AcceptedLedgerTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class AcceptedLedgerTx : public CountedObject<AcceptedLedgerTx>
AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const&,
std::shared_ptr<STObject const> const&,
AccountIDCache const&);
std::shared_ptr<STObject const> const&);

std::shared_ptr<STTx const> const&
getTxn() const
Expand Down
11 changes: 2 additions & 9 deletions src/ripple/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ class ApplicationImp : public Application, public BasicApp
NodeStoreScheduler m_nodeStoreScheduler;
std::unique_ptr<SHAMapStore> m_shaMapStore;
PendingSaves pendingSaves_;
AccountIDCache accountIDCache_;
std::optional<OpenLedger> openLedger_;

NodeCache m_tempNodeCache;
Expand Down Expand Up @@ -336,8 +335,6 @@ class ApplicationImp : public Application, public BasicApp
m_nodeStoreScheduler,
logs_->journal("SHAMapStore")))

, accountIDCache_(128000)

, m_tempNodeCache(
"NodeCache",
16384,
Expand Down Expand Up @@ -494,6 +491,8 @@ class ApplicationImp : public Application, public BasicApp
config_->reporting() ? std::make_unique<ReportingETL>(*this)
: nullptr)
{
initAccountIdCache(config_->getValueFor(SizedItem::accountIdCacheSize));

add(m_resourceManager.get());

//
Expand Down Expand Up @@ -856,12 +855,6 @@ class ApplicationImp : public Application, public BasicApp
return pendingSaves_;
}

AccountIDCache const&
accountIDCache() const override
{
return accountIDCache_;
}

OpenLedger&
openLedger() override
{
Expand Down
3 changes: 0 additions & 3 deletions src/ripple/app/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class PathRequests;
class PendingSaves;
class PublicKey;
class SecretKey;
class AccountIDCache;
class STLedgerEntry;
class TimeKeeper;
class TransactionMaster;
Expand Down Expand Up @@ -251,8 +250,6 @@ class Application : public beast::PropertyStream::Source
getSHAMapStore() = 0;
virtual PendingSaves&
pendingSaves() = 0;
virtual AccountIDCache const&
accountIDCache() const = 0;
virtual OpenLedger&
openLedger() = 0;
virtual OpenLedger const&
Expand Down
19 changes: 12 additions & 7 deletions src/ripple/app/paths/PathRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,16 @@ PathRequest::findPaths(
continueCallback);
mContext[issue] = ps;

auto& sourceAccount = !isXRP(issue.account)
? issue.account
: isXRP(issue.currency) ? xrpAccount() : *raSrcAccount;
auto const& sourceAccount = [&] {
if (!isXRP(issue.account))
return issue.account;

if (isXRP(issue.currency))
return xrpAccount();

return *raSrcAccount;
}();

STAmount saMaxAmount = saSendMax.value_or(
STAmount({issue.currency, sourceAccount}, 1u, 0, true));

Expand Down Expand Up @@ -675,10 +682,8 @@ PathRequest::doUpdate(
destCurrencies.append(to_string(c));
}

newStatus[jss::source_account] =
app_.accountIDCache().toBase58(*raSrcAccount);
newStatus[jss::destination_account] =
app_.accountIDCache().toBase58(*raDstAccount);
newStatus[jss::source_account] = toBase58(*raSrcAccount);
newStatus[jss::destination_account] = toBase58(*raDstAccount);
newStatus[jss::destination_amount] = saDstAmount.getJson(JsonOptions::none);
newStatus[jss::full_reply] = !fast;

Expand Down
4 changes: 0 additions & 4 deletions src/ripple/app/rdb/backend/detail/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ getNewestAccountTxsB(
* account which match given criteria starting from given marker
* and calls callback for each found transaction.
* @param session Session with database.
* @param idCache Account ID cache.
* @param onUnsavedLedger Callback function to call on each found unsaved
* ledger within given range.
* @param onTransaction Callback function to call on each found transaction.
Expand All @@ -408,7 +407,6 @@ getNewestAccountTxsB(
std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
oldestAccountTxPage(
soci::session& session,
AccountIDCache const& idCache,
std::function<void(std::uint32_t)> const& onUnsavedLedger,
std::function<
void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const&
Expand All @@ -422,7 +420,6 @@ oldestAccountTxPage(
* account which match given criteria starting from given marker
* and calls callback for each found transaction.
* @param session Session with database.
* @param idCache Account ID cache.
* @param onUnsavedLedger Callback function to call on each found unsaved
* ledger within given range.
* @param onTransaction Callback function to call on each found transaction.
Expand All @@ -441,7 +438,6 @@ oldestAccountTxPage(
std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
newestAccountTxPage(
soci::session& session,
AccountIDCache const& idCache,
std::function<void(std::uint32_t)> const& onUnsavedLedger,
std::function<
void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const&
Expand Down
21 changes: 7 additions & 14 deletions src/ripple/app/rdb/backend/detail/impl/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ saveValidatedLedger(

sql += txnId;
sql += "','";
sql += app.accountIDCache().toBase58(account);
sql += toBase58(account);
sql += "',";
sql += ledgerSeq;
sql += ",";
Expand Down Expand Up @@ -760,8 +760,7 @@ transactionsSQL(
sql = boost::str(
boost::format("SELECT %s FROM AccountTransactions "
"WHERE Account = '%s' %s %s LIMIT %u, %u;") %
selection % app.accountIDCache().toBase58(options.account) %
maxClause % minClause %
selection % toBase58(options.account) % maxClause % minClause %
beast::lexicalCastThrow<std::string>(options.offset) %
beast::lexicalCastThrow<std::string>(numberOfResults));
else
Expand All @@ -774,9 +773,9 @@ transactionsSQL(
"ORDER BY AccountTransactions.LedgerSeq %s, "
"AccountTransactions.TxnSeq %s, AccountTransactions.TransID %s "
"LIMIT %u, %u;") %
selection % app.accountIDCache().toBase58(options.account) %
maxClause % minClause % (descending ? "DESC" : "ASC") %
selection % toBase58(options.account) % maxClause % minClause %
(descending ? "DESC" : "ASC") % (descending ? "DESC" : "ASC") %
(descending ? "DESC" : "ASC") %
beast::lexicalCastThrow<std::string>(options.offset) %
beast::lexicalCastThrow<std::string>(numberOfResults));
JLOG(j.trace()) << "txSQL query: " << sql;
Expand Down Expand Up @@ -1049,7 +1048,6 @@ getNewestAccountTxsB(
* account that matches the given criteria starting from the provided
* marker and invokes the callback parameter for each found transaction.
* @param session Session with the database.
* @param idCache Account ID cache.
* @param onUnsavedLedger Callback function to call on each found unsaved
* ledger within the given range.
* @param onTransaction Callback function to call on each found transaction.
Expand All @@ -1069,7 +1067,6 @@ getNewestAccountTxsB(
static std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
accountTxPage(
soci::session& session,
AccountIDCache const& idCache,
std::function<void(std::uint32_t)> const& onUnsavedLedger,
std::function<
void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const&
Expand Down Expand Up @@ -1135,8 +1132,8 @@ accountTxPage(
ORDER BY AccountTransactions.LedgerSeq %s,
AccountTransactions.TxnSeq %s
LIMIT %u;)")) %
idCache.toBase58(options.account) % options.minLedger %
options.maxLedger % order % order % queryLimit);
toBase58(options.account) % options.minLedger % options.maxLedger %
order % order % queryLimit);
}
else
{
Expand All @@ -1146,7 +1143,7 @@ accountTxPage(
const std::uint32_t maxLedger =
forward ? options.maxLedger : findLedger - 1;

auto b58acct = idCache.toBase58(options.account);
auto b58acct = toBase58(options.account);
sql = boost::str(
boost::format((
R"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
Expand Down Expand Up @@ -1250,7 +1247,6 @@ accountTxPage(
std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
oldestAccountTxPage(
soci::session& session,
AccountIDCache const& idCache,
std::function<void(std::uint32_t)> const& onUnsavedLedger,
std::function<
void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const&
Expand All @@ -1261,7 +1257,6 @@ oldestAccountTxPage(
{
return accountTxPage(
session,
idCache,
onUnsavedLedger,
onTransaction,
options,
Expand All @@ -1273,7 +1268,6 @@ oldestAccountTxPage(
std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
newestAccountTxPage(
soci::session& session,
AccountIDCache const& idCache,
std::function<void(std::uint32_t)> const& onUnsavedLedger,
std::function<
void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const&
Expand All @@ -1284,7 +1278,6 @@ newestAccountTxPage(
{
return accountTxPage(
session,
idCache,
onUnsavedLedger,
onTransaction,
options,
Expand Down
Loading

0 comments on commit e2eed96

Please sign in to comment.