From 3bb02e1531a2138bfc98329be204ea5f86599185 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sat, 18 Jul 2026 03:53:38 -0300 Subject: [PATCH] perf(liquidity): reuse active pool probe read --- apps/amm/src/NewPositionRuntime.cpp | 18 +++- apps/amm/src/NewPositionRuntime.h | 3 + apps/amm/tests/cpp/NewPositionRuntimeTest.cpp | 82 +++++++++++++++---- 3 files changed, 87 insertions(+), 16 deletions(-) diff --git a/apps/amm/src/NewPositionRuntime.cpp b/apps/amm/src/NewPositionRuntime.cpp index 56c3dfa..313e037 100644 --- a/apps/amm/src/NewPositionRuntime.cpp +++ b/apps/amm/src/NewPositionRuntime.cpp @@ -451,6 +451,7 @@ void NewPositionRuntime::buildQuoteInputAsync( const ActiveNetworkSnapshot& network, bool walletOpen, bool forceRefresh, + WalletAccountRead preloadedPoolRead, std::function shouldContinue, std::function callback) { @@ -475,6 +476,7 @@ void NewPositionRuntime::buildQuoteInputAsync( QPointer guard(this); m_sequencer->readAccounts({ configId }, forceRefresh, [guard, request, network, walletOpen, forceRefresh, + preloadedPoolRead = std::move(preloadedPoolRead), shouldContinue, callback = std::move(callback)](QVector configReads) mutable { if (!guard || !shouldContinue()) @@ -511,13 +513,21 @@ void NewPositionRuntime::buildQuoteInputAsync( pair.value(QStringLiteral("currentTickId")).toString(), pair.value(QStringLiteral("clockId")).toString(), }; - guard->m_sequencer->readAccounts(fixedIds, forceRefresh, + const bool reusePreloadedPool = preloadedPoolRead.ok() + && preloadedPoolRead.accountId == fixedIds.value(2); + QStringList readIds = fixedIds; + if (reusePreloadedPool) + readIds.removeAt(2); + guard->m_sequencer->readAccounts(readIds, forceRefresh, [guard, requestObject, network, walletOpen, config, pair, + preloadedPoolRead = std::move(preloadedPoolRead), reusePreloadedPool, shouldContinue, callback = std::move(callback)]( QVector fixedReads) mutable { if (!guard || !shouldContinue()) return; + if (reusePreloadedPool) + fixedReads.insert(2, std::move(preloadedPoolRead)); QStringList selectedIds; for (const QString& key : { QStringLiteral("holdingAId"), @@ -593,11 +603,13 @@ void NewPositionRuntime::quoteFromAccountsAsync( const ActiveNetworkSnapshot& network, bool walletOpen, bool forceRefresh, + WalletAccountRead preloadedPoolRead, std::function shouldContinue, ResultCallback callback) { QPointer guard(this); buildQuoteInputAsync(request, network, walletOpen, forceRefresh, + std::move(preloadedPoolRead), shouldContinue, [guard, shouldContinue, callback = std::move(callback)]( @@ -637,6 +649,7 @@ void NewPositionRuntime::quoteAsync(const QVariantMap& request, || !m_sequencer || !m_sequencer->isConfigured()) { quoteFromAccountsAsync( request, network, walletOpen, forceRefresh, + {}, std::move(shouldContinue), std::move(callback)); return; } @@ -672,6 +685,7 @@ void NewPositionRuntime::quoteAsync(const QVariantMap& request, } guard->quoteFromAccountsAsync( request, network, walletOpen, forceRefresh, + std::move(read), std::move(shouldContinue), std::move(callback)); }); } @@ -705,7 +719,7 @@ void NewPositionRuntime::submitAsync(const QVariantMap& request, return guard && guard->submitIsCurrent(submitGeneration, walletGeneration); }; - buildQuoteInputAsync(request, network, true, true, + buildQuoteInputAsync(request, network, true, true, {}, shouldContinue, [guard, quoteHash, submitGeneration, walletGeneration, shouldContinue]( diff --git a/apps/amm/src/NewPositionRuntime.h b/apps/amm/src/NewPositionRuntime.h index 75914c5..dfc1ecc 100644 --- a/apps/amm/src/NewPositionRuntime.h +++ b/apps/amm/src/NewPositionRuntime.h @@ -15,6 +15,7 @@ class AmmClient; class WalletProvider; class SequencerClient; struct WalletAccount; +struct WalletAccountRead; class NewPositionRuntime : public QObject { public: @@ -75,12 +76,14 @@ private: const ActiveNetworkSnapshot& network, bool walletOpen, bool forceRefresh, + WalletAccountRead preloadedPoolRead, std::function shouldContinue, std::function callback); void quoteFromAccountsAsync(const QVariantMap& request, const ActiveNetworkSnapshot& network, bool walletOpen, bool forceRefresh, + WalletAccountRead preloadedPoolRead, std::function shouldContinue, ResultCallback callback); void submitPlanAsync(QJsonObject input, diff --git a/apps/amm/tests/cpp/NewPositionRuntimeTest.cpp b/apps/amm/tests/cpp/NewPositionRuntimeTest.cpp index 269d9e6..9cae532 100644 --- a/apps/amm/tests/cpp/NewPositionRuntimeTest.cpp +++ b/apps/amm/tests/cpp/NewPositionRuntimeTest.cpp @@ -309,17 +309,35 @@ namespace { AmmClientResult pairIds(const QJsonObject&) const override { ++pairIdsCalls; + const QString tokenAId = useValidPairIds + ? QString(64, QLatin1Char('2')) : QStringLiteral("token-a"); + const QString tokenBId = useValidPairIds + ? QString(64, QLatin1Char('3')) : QStringLiteral("token-b"); + const QString poolId = useValidPairIds + ? pairPoolId : QStringLiteral("pool"); + const QString vaultAId = useValidPairIds + ? QString(64, QLatin1Char('4')) : QStringLiteral("vault-a"); + const QString vaultBId = useValidPairIds + ? QString(64, QLatin1Char('5')) : QStringLiteral("vault-b"); + const QString lpDefinitionId = useValidPairIds + ? QString(64, QLatin1Char('6')) : QStringLiteral("lp"); + const QString lpLockHoldingId = useValidPairIds + ? QString(64, QLatin1Char('7')) : QStringLiteral("lp-lock"); + const QString currentTickId = useValidPairIds + ? QString(64, QLatin1Char('8')) : QStringLiteral("tick"); + const QString clockId = useValidPairIds + ? QString(64, QLatin1Char('9')) : QStringLiteral("clock"); return success({ { QStringLiteral("status"), QStringLiteral("ok") }, - { QStringLiteral("tokenAId"), QStringLiteral("token-a") }, - { QStringLiteral("tokenBId"), QStringLiteral("token-b") }, - { QStringLiteral("poolId"), QStringLiteral("pool") }, - { QStringLiteral("vaultAId"), QStringLiteral("vault-a") }, - { QStringLiteral("vaultBId"), QStringLiteral("vault-b") }, - { QStringLiteral("lpDefinitionId"), QStringLiteral("lp") }, - { QStringLiteral("lpLockHoldingId"), QStringLiteral("lp-lock") }, - { QStringLiteral("currentTickId"), QStringLiteral("tick") }, - { QStringLiteral("clockId"), QStringLiteral("clock") }, + { QStringLiteral("tokenAId"), tokenAId }, + { QStringLiteral("tokenBId"), tokenBId }, + { QStringLiteral("poolId"), poolId }, + { QStringLiteral("vaultAId"), vaultAId }, + { QStringLiteral("vaultBId"), vaultBId }, + { QStringLiteral("lpDefinitionId"), lpDefinitionId }, + { QStringLiteral("lpLockHoldingId"), lpLockHoldingId }, + { QStringLiteral("currentTickId"), currentTickId }, + { QStringLiteral("clockId"), clockId }, }); } @@ -408,6 +426,8 @@ namespace { mutable QStringList freshLpAccountIds; mutable QStringList normalizedAccountIds; QString normalizedBalanceHex = QString(32, QLatin1Char('0')); + bool useValidPairIds = false; + QString pairPoolId; }; ActiveNetworkSnapshot readyNetwork() @@ -898,6 +918,11 @@ int main(int argc, char** argv) return 1; poolProbeClient.normalizedBalanceHex = QString(32, QLatin1Char('1')); + poolProbeClient.useValidPairIds = true; + poolProbeClient.pairPoolId = poolIdHex; + const int requestsBeforeActiveProbe = poolProbeServer.requestCount(); + const int poolReadsBeforeActiveProbe = + poolProbeClient.normalizedAccountIds.count(poolIdHex); poolProbeResult.clear(); poolProbeRuntime.quoteAsync( poolProbeRequest, readyNetwork(), true, true, true, @@ -907,12 +932,41 @@ int main(int argc, char** argv) return 1; if (!expect(poolProbeResult.value(QStringLiteral("poolStatus")).toString() == QStringLiteral("active_pool") - && poolProbeServer.requestCount() == 3 + && poolProbeServer.requestCount() == requestsBeforeActiveProbe + 10 + && poolProbeClient.normalizedAccountIds.count(poolIdHex) + == poolReadsBeforeActiveProbe + 1 && poolProbeClient.pairIdsCalls == 1 && poolProbeClient.quoteCalls == 1, - "nondefault pool should run one validating full quote")) + "active pool probe should reuse its fresh pool read")) return 1; + const QString mismatchedPoolId = QString(64, QLatin1Char('d')); + poolProbeClient.pairPoolId = mismatchedPoolId; + const int requestsBeforeMismatch = poolProbeServer.requestCount(); + const int sourcePoolReadsBeforeMismatch = + poolProbeClient.normalizedAccountIds.count(poolIdHex); + const int pairPoolReadsBeforeMismatch = + poolProbeClient.normalizedAccountIds.count(mismatchedPoolId); + poolProbeResult.clear(); + poolProbeRuntime.quoteAsync( + poolProbeRequest, readyNetwork(), true, true, true, + [&](QVariantMap result) { poolProbeResult = std::move(result); }); + if (!expect(waitForCondition([&]() { return !poolProbeResult.isEmpty(); }), + "mismatched pool probe should complete")) + return 1; + if (!expect(poolProbeResult.value(QStringLiteral("poolStatus")).toString() + == QStringLiteral("active_pool") + && poolProbeServer.requestCount() == requestsBeforeMismatch + 11 + && poolProbeClient.normalizedAccountIds.count(poolIdHex) + == sourcePoolReadsBeforeMismatch + 1 + && poolProbeClient.normalizedAccountIds.count(mismatchedPoolId) + == pairPoolReadsBeforeMismatch + 1 + && poolProbeClient.pairIdsCalls == 2 + && poolProbeClient.quoteCalls == 2, + "pool mismatch should retain the full forced quote")) + return 1; + + const int requestsBeforeFailedProbe = poolProbeServer.requestCount(); poolProbeServer.failNextRequest(); poolProbeResult.clear(); poolProbeRuntime.quoteAsync( @@ -923,9 +977,9 @@ int main(int argc, char** argv) return 1; if (!expect(poolProbeResult.value(QStringLiteral("code")).toString() == QStringLiteral("account_read_failed") - && poolProbeServer.requestCount() == 4 - && poolProbeClient.pairIdsCalls == 1 - && poolProbeClient.quoteCalls == 1, + && poolProbeServer.requestCount() == requestsBeforeFailedProbe + 1 + && poolProbeClient.pairIdsCalls == 2 + && poolProbeClient.quoteCalls == 2, "failed pool probe should wait for the next one-account retry")) return 1;