Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further speed up transactions view load #6579

32 changes: 25 additions & 7 deletions core/src/main/java/bisq/core/util/FormattingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import bisq.core.monetary.Price;

import bisq.common.util.MathUtils;
import bisq.common.util.Tuple3;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Monetary;
Expand All @@ -33,12 +34,15 @@
public class FormattingUtils {
public static final String BTC_FORMATTER_KEY = "BTC";

public final static String RANGE_SEPARATOR = " - ";
public static final String RANGE_SEPARATOR = " - ";

private static final MonetaryFormat fiatPriceFormat = new MonetaryFormat().shift(0).minDecimals(4).repeatOptionalDecimals(0, 0);
private static final MonetaryFormat altcoinFormat = new MonetaryFormat().shift(0).minDecimals(8).repeatOptionalDecimals(0, 0);
private static final DecimalFormat decimalFormat = new DecimalFormat("#.#");

private static final ThreadLocal<Tuple3<Locale, DateFormat, DateFormat>> cachedUtcDateTimeFormatters = new ThreadLocal<>();
private static final ThreadLocal<Tuple3<Locale, DateFormat, DateFormat>> cachedLocalDateTimeFormatters = new ThreadLocal<>();

public static String formatCoinWithCode(long value, MonetaryFormat coinFormat) {
return formatCoinWithCode(Coin.valueOf(value), coinFormat);
}
Expand Down Expand Up @@ -183,12 +187,25 @@ public static String formatRoundedDoubleWithPrecision(double value, int precisio

public static String formatDateTime(Date date, boolean useLocaleAndLocalTimezone) {
Locale locale = useLocaleAndLocalTimezone ? GlobalSettings.getLocale() : Locale.US;
DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
DateFormat timeInstance = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
if (!useLocaleAndLocalTimezone) {
dateInstance.setTimeZone(TimeZone.getTimeZone("UTC"));
timeInstance.setTimeZone(TimeZone.getTimeZone("UTC"));

var formatterTuple = (useLocaleAndLocalTimezone ?
cachedLocalDateTimeFormatters : cachedUtcDateTimeFormatters).get();
if (formatterTuple == null || !formatterTuple.first.equals(locale)) {
formatterTuple = new Tuple3<>(locale,
DateFormat.getDateInstance(DateFormat.DEFAULT, locale),
DateFormat.getTimeInstance(DateFormat.DEFAULT, locale));

if (useLocaleAndLocalTimezone) {
cachedLocalDateTimeFormatters.set(formatterTuple);
} else {
formatterTuple.second.setTimeZone(TimeZone.getTimeZone("UTC"));
formatterTuple.third.setTimeZone(TimeZone.getTimeZone("UTC"));
cachedUtcDateTimeFormatters.set(formatterTuple);
}
}
DateFormat dateInstance = formatterTuple.second;
DateFormat timeInstance = formatterTuple.third;

return formatDateTime(date, dateInstance, timeInstance);
}

Expand Down Expand Up @@ -288,7 +305,8 @@ else if (bytes < mb)
}

@NotNull
public static String fillUpPlacesWithEmptyStrings(String formattedNumber, @SuppressWarnings("unused") int maxNumberOfDigits) {
public static String fillUpPlacesWithEmptyStrings(String formattedNumber,
@SuppressWarnings("unused") int maxNumberOfDigits) {
//FIXME: temporary deactivate adding spaces in front of numbers as we don't use a monospace font right now.
/*int numberOfPlacesToFill = maxNumberOfDigits - formattedNumber.length();
for (int i = 0; i < numberOfPlacesToFill; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.main.funds.transactions;

import org.bitcoinj.core.Transaction;

import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static bisq.desktop.main.funds.transactions.TransactionAwareTradable.TX_FILTER_SIZE;

public class RelatedTransactionFilterSlices {
private final List<TransactionAwareTradable> tradables;
private final BitSet[] filterSlices;

public RelatedTransactionFilterSlices(Collection<? extends TransactionAwareTradable> tradables) {
this.tradables = List.copyOf(tradables);

filterSlices = new BitSet[TX_FILTER_SIZE];
Arrays.setAll(filterSlices, i -> new BitSet(this.tradables.size()));

IntStream.range(0, this.tradables.size())
.forEach(j -> this.tradables.get(j).getRelatedTransactionFilter()
.forEach(i -> filterSlices[i].set(j)));
}

public Stream<TransactionAwareTradable> getAllRelatedTradables(Transaction tx) {
int i = TransactionAwareTradable.bucketIndex(tx);
return filterSlices[i].stream()
.mapToObj(tradables::get)
.filter(t -> t.isRelatedToTransaction(tx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.bitcoinj.core.Transaction;

import java.util.stream.IntStream;

class TransactionAwareOpenOffer implements TransactionAwareTradable {
private final OpenOffer delegate;

Expand All @@ -34,12 +36,18 @@ public boolean isRelatedToTransaction(Transaction transaction) {
Offer offer = delegate.getOffer();
String paymentTxId = offer.getOfferFeePaymentTxId();

String txId = transaction.getTxId().toString();

return paymentTxId != null && paymentTxId.equals(txId);
return paymentTxId != null && paymentTxId.equals(transaction.getTxId().toString());
}

public Tradable asTradable() {
return delegate;
}

@Override
public IntStream getRelatedTransactionFilter() {
Offer offer = delegate.getOffer();
String paymentTxId = offer.getOfferFeePaymentTxId();
return IntStream.of(TransactionAwareTradable.bucketIndex(paymentTxId))
.filter(i -> i >= 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,35 @@

import bisq.core.trade.model.Tradable;

import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.Transaction;

import java.util.stream.IntStream;

import javax.annotation.Nullable;

interface TransactionAwareTradable {
int TX_FILTER_SIZE = 64;
int DELAYED_PAYOUT_TX_BUCKET_INDEX = TX_FILTER_SIZE - 1;

boolean isRelatedToTransaction(Transaction transaction);

Tradable asTradable();

/** Returns a list of bucket indices of all transactions which might be related to this Tradable. */
IntStream getRelatedTransactionFilter();

static int bucketIndex(Transaction tx) {
return tx.getLockTime() == 0 ? bucketIndex(tx.getTxId()) : DELAYED_PAYOUT_TX_BUCKET_INDEX;
}

static int bucketIndex(Sha256Hash hash) {
int i = hash.getBytes()[31] & 255;
return i % TX_FILTER_SIZE != DELAYED_PAYOUT_TX_BUCKET_INDEX ?
i % TX_FILTER_SIZE : i / TX_FILTER_SIZE;
}

static int bucketIndex(@Nullable String txId) {
return txId != null ? bucketIndex(Sha256Hash.wrap(txId)) : -1;
}
}
Loading