From e18b1e833dccee08489d90231aa652d72824b61d Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Wed, 8 Mar 2023 14:23:15 +0800 Subject: [PATCH] Avoid repeated scanning for swap trades via BsqTxListItem ctor Precompute and pass a map of txIds to BsqSwapTrade instances to the BsqTxListItem constructor in 'BsqTxView.updateList()', in place of the tradable repository, so that the tradables don't need to be repeatedly scanned to find the optional matching BSQ swap trade for each BSQ tx. This fixes a quadratic time bug and significantly speeds up the BSQ tx view load for users with many past trades. --- .../main/dao/wallet/tx/BsqTxListItem.java | 11 +++----- .../desktop/main/dao/wallet/tx/BsqTxView.java | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxListItem.java b/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxListItem.java index 7f52b5b829..4a5bdca042 100644 --- a/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxListItem.java +++ b/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxListItem.java @@ -18,7 +18,6 @@ package bisq.desktop.main.dao.wallet.tx; import bisq.desktop.components.TxConfidenceListItem; -import bisq.desktop.main.funds.transactions.TradableRepository; import bisq.desktop.util.DisplayUtils; import bisq.core.btc.wallet.BsqWalletService; @@ -36,6 +35,7 @@ import org.bitcoinj.core.Transaction; import org.bitcoinj.core.TransactionOutput; import java.util.Date; +import java.util.Map; import java.util.Optional; import lombok.EqualsAndHashCode; @@ -66,7 +66,7 @@ class BsqTxListItem extends TxConfidenceListItem { DaoFacade daoFacade, Date date, BsqFormatter bsqFormatter, - TradableRepository tradableRepository) { + Map swapTradeByTxIdMap) { super(transaction, bsqWalletService); this.daoFacade = daoFacade; @@ -133,12 +133,7 @@ class BsqTxListItem extends TxConfidenceListItem { else address = ""; - - optionalBsqTrade = tradableRepository.getAll().stream() - .filter(tradable -> tradable instanceof BsqSwapTrade) - .map(tradable -> (BsqSwapTrade) tradable) - .filter(tradable -> txId.equals(tradable.getTxId())) - .findFirst(); + optionalBsqTrade = Optional.ofNullable(swapTradeByTxIdMap.get(txId)); } BsqTxListItem() { diff --git a/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxView.java b/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxView.java index 506a9b10d9..582cfc49e5 100644 --- a/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxView.java +++ b/desktop/src/main/java/bisq/desktop/main/dao/wallet/tx/BsqTxView.java @@ -87,6 +87,7 @@ import javafx.util.Callback; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -351,17 +352,21 @@ public class BsqTxView extends ActivatableView implements BsqBal observableList.forEach(BsqTxListItem::cleanup); List walletTransactions = bsqWalletService.getClonedWalletTransactions(); + Map swapTradeByTxIdMap = tradableRepository.getAll().stream() + .filter(tradable -> tradable instanceof BsqSwapTrade) + .map(t -> (BsqSwapTrade) t) + .collect(Collectors.toMap(BsqSwapTrade::getTxId, t -> t)); + List items = walletTransactions.stream() - .map(transaction -> { - return new BsqTxListItem(transaction, - bsqWalletService, - btcWalletService, - daoFacade, - // Use tx.getIncludedInBestChainAt() when available, otherwise use tx.getUpdateTime() - transaction.getIncludedInBestChainAt() != null ? transaction.getIncludedInBestChainAt() : transaction.getUpdateTime(), - bsqFormatter, - tradableRepository); - }) + .map(transaction -> new BsqTxListItem(transaction, + bsqWalletService, + btcWalletService, + daoFacade, + // Use tx.getIncludedInBestChainAt() when available, otherwise use tx.getUpdateTime() + transaction.getIncludedInBestChainAt() != null ? transaction.getIncludedInBestChainAt() : transaction.getUpdateTime(), + bsqFormatter, + swapTradeByTxIdMap) + ) .collect(Collectors.toList()); observableList.setAll(items); }