From 65706e7c14f6e7a7443f569b529b9b75b79c439d Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Mon, 1 Nov 2021 22:52:39 +0100 Subject: [PATCH] Add async handling with CompletableFuture to fillList The creation of TradeStatistics3ListItem is rather fast but the applying to the list is due sorting pretty slow (300 ms) as its > 100k items. We do the applying on the callback thread. Seems JavaFx permits that. So we can keep the UI thread unblocked. Remove modelReadyListener Renamed model.selectedTradeStatistics to model.tradeStatisticsByCurrency --- .../main/market/trades/TradesChartsView.java | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsView.java b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsView.java index 20af97e32c..2c51f35f03 100644 --- a/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsView.java +++ b/desktop/src/main/java/bisq/desktop/main/market/trades/TradesChartsView.java @@ -103,6 +103,7 @@ import java.text.DecimalFormat; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -172,7 +173,6 @@ public class TradesChartsView extends ActivatableViewAndModel priceColumnLabelListener; private ListChangeListener> itemsChangeListener; private ListChangeListener tradeStatisticsByCurrencyListener; - private ChangeListener modelReadyListener; @SuppressWarnings("FieldCanBeLocal") private MonadicBinding currencySelectionBinding; @@ -243,10 +243,8 @@ public class TradesChartsView extends ActivatableViewAndModel { - nrOfTradeStatisticsLabel.setText(Res.get("market.trades.nrOfTrades", model.selectedTradeStatistics.size())); - if (model.modelReady.get()) { - fillList(); - } + nrOfTradeStatisticsLabel.setText(Res.get("market.trades.nrOfTrades", model.tradeStatisticsByCurrency.size())); + fillList(); }; parentHeightListener = (observable, oldValue, newValue) -> layout(); @@ -285,19 +283,6 @@ public class TradesChartsView extends ActivatableViewAndModel { - if (newValue) { - long ts = System.currentTimeMillis(); - - fillList(); - tableView.setItems(sortedList); - layout(); - - log.error("{}", System.currentTimeMillis() - ts); - UserThread.execute(() -> model.modelReady.removeListener(modelReadyListener)); - } - }; } @Override @@ -336,7 +321,7 @@ public class TradesChartsView extends ActivatableViewAndModel exportToCsv()); UserThread.runAfter(this::updateChartData, 100, TimeUnit.MILLISECONDS); @@ -374,9 +359,10 @@ public class TradesChartsView extends ActivatableViewAndModel tradeStatistics3ListItems = model.selectedTradeStatistics.stream() - .map(tradeStatistics -> new TradeStatistics3ListItem(tradeStatistics, - coinFormatter, - model.showAllTradeCurrenciesProperty.get())) - .collect(Collectors.toList()); - listItems.setAll(tradeStatistics3ListItems); + long ts = System.currentTimeMillis(); + CompletableFuture.supplyAsync(() -> { + return model.tradeStatisticsByCurrency.stream() + .map(tradeStatistics -> new TradeStatistics3ListItem(tradeStatistics, + coinFormatter, + model.showAllTradeCurrenciesProperty.get())) + .collect(Collectors.toList()); + }).whenComplete((items, throwable) -> { + log.error("Creating listItems took {} ms", System.currentTimeMillis() - ts); + long ts2 = System.currentTimeMillis(); + listItems.setAll(items); + // Is slow because of sorting of > 100k items. But at least it seems it works on the thread from the + // CompletableFuture callback, so we do not block the UI thread;. + log.error("Applying sorted list took {} ms", + System.currentTimeMillis() - ts2); + }); } private void exportToCsv() {