perf(history): don't rebuild the O(N) tx display cache on every block for a confirmation-only change

Performance audit: tx_revision bumps on every new block because each tx's confirmation count rises, so
the History tab's groupByTxid + merge + sort + deep-copy display cache fully rebuilt once per ~36s block
on the UI thread, even when nothing but the counts changed.

Add WalletState::tx_structure_revision, bumped alongside tx_revision EXCEPT when setTransactionsIfChanged
sees a pure confirmation-count delta (new sameTransactionLayout: every row equal ignoring the confirmation
COUNT, but the pending/confirmations==0 state — which drives the pending-first sort and shield merge — must
still match). Every other writer bumps both (conservative default). The History tab's groups cache and
display cache now key on tx_structure_revision; on a confirmation-only delta it patches dtx.confirmations
in place (rows, order and indices into state.transactions are unchanged) instead of rebuilding, and re-stamps
the filter snapshot so "export current filter" stays valid. Displayed confirmations still update every block.

No fund path touched; display/caching only. Test: testTxStructureRevision.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-10-19 14:44:13 -05:00
co-authored by Claude Opus 4.8
parent 6d58802127
commit df98f98669
3 changed files with 112 additions and 8 deletions
+20 -2
View File
@@ -86,6 +86,15 @@ inline bool sameTransactionInfo(const TransactionInfo& a, const TransactionInfo&
a.txid == b.txid && a.type == b.type && a.address == b.address &&
a.from_address == b.from_address && a.memo == b.memo;
}
// Same row except the confirmation COUNT: the pending (confirmations==0) state must still match because it drives the
// History tab's pending-first sort and shield-leg merge. A delta for which every row passes this changes nothing the
// tab's merged/sorted display list is built from, so it can patch confirmations in place instead of rebuilding.
inline bool sameTransactionLayout(const TransactionInfo& a, const TransactionInfo& b) {
return a.timestamp == b.timestamp && (a.confirmations == 0) == (b.confirmations == 0) &&
std::memcmp(&a.amount, &b.amount, sizeof(double)) == 0 &&
a.txid == b.txid && a.type == b.type && a.address == b.address &&
a.from_address == b.from_address && a.memo == b.memo;
}
inline bool sameTransactionList(const std::vector<TransactionInfo>& a, const std::vector<TransactionInfo>& b) {
if (a.size() != b.size()) return false;
for (std::size_t i = 0; i < a.size(); ++i)
@@ -370,12 +379,21 @@ struct WalletState {
// touchTransactions() afterwards. UI caches (History tab, ...) key on this, NOT on last_tx_update (a
// wall-clock refresh stamp that lite never sets) nor App::tx_dirty_generation_ (a refresh-needed flag).
std::uint64_t tx_revision = 1;
void touchTransactions() { ++tx_revision; }
// Bumps with tx_revision EXCEPT for a pure confirmation-count change (see sameTransactionLayout): the History tab's
// O(N) merge+sort cache keys on this and just patches confirmations when only tx_revision moved. Every writer that
// is not setTransactionsIfChanged goes through touchTransactions()/replaceTransactions() and bumps both (safe default).
std::uint64_t tx_structure_revision = 1;
void touchTransactions() { ++tx_revision; ++tx_structure_revision; }
void replaceTransactions(std::vector<TransactionInfo>&& next) { transactions = std::move(next); touchTransactions(); }
// Replace only when the content differs; returns true (and bumps) if it did. Keeps the old list when identical.
bool setTransactionsIfChanged(std::vector<TransactionInfo>&& next) {
if (sameTransactionList(transactions, next)) return false;
replaceTransactions(std::move(next));
bool layoutSame = transactions.size() == next.size();
for (std::size_t i = 0; layoutSame && i < next.size(); ++i)
layoutSame = sameTransactionLayout(transactions[i], next[i]);
transactions = std::move(next);
++tx_revision;
if (!layoutSame) ++tx_structure_revision;
return true;
}
int64_t last_peer_update = 0;