Cache enum.values() in ChartCalculations & TradeStatistics3

Cache enum arrays 'TickUnit.values()' & 'PaymentMethodWrapper.values()'
as the JVM makes defensive copies of them every time they are returned,
and they are both being used in tight loops. In particular, profiling
suggests this will make 'TradeStatistics3.isValid' about twice as fast.
This commit is contained in:
Steven Barclay 2023-05-07 20:51:09 +01:00
parent 835593f2c9
commit 914d75682c
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B
2 changed files with 10 additions and 7 deletions

View File

@ -200,7 +200,9 @@ public final class TradeStatistics3 implements ProcessOncePersistableNetworkPayl
TIKKIE,
TRANSFERWISE_USD,
ACH_TRANSFER,
DOMESTIC_WIRE_TRANSFER
DOMESTIC_WIRE_TRANSFER;
private static final PaymentMethodMapper[] values = values(); // cache for perf gain
}
@Getter
@ -413,7 +415,7 @@ public final class TradeStatistics3 implements ProcessOncePersistableNetworkPayl
return paymentMethod;
}
try {
return PaymentMethodMapper.values()[Integer.parseInt(paymentMethod)].name();
return PaymentMethodMapper.values[Integer.parseInt(paymentMethod)].name();
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
return paymentMethod;
}

View File

@ -73,20 +73,21 @@ public class ChartCalculations {
dateMapsPerTickUnit.put(tick, new HashMap<>());
}
TradesChartsViewModel.TickUnit[] tickUnits = TradesChartsViewModel.TickUnit.values();
tradeStatisticsSet.stream()
.filter(e -> e.getCurrency().equals("USD"))
.forEach(tradeStatistics -> {
for (TradesChartsViewModel.TickUnit tick : TradesChartsViewModel.TickUnit.values()) {
long time = roundToTick(tradeStatistics.getLocalDateTime(), tick).getTime();
Map<Long, List<TradeStatistics3>> map = dateMapsPerTickUnit.get(tick);
for (TradesChartsViewModel.TickUnit tickUnit : tickUnits) {
long time = roundToTick(tradeStatistics.getLocalDateTime(), tickUnit).getTime();
Map<Long, List<TradeStatistics3>> map = dateMapsPerTickUnit.get(tickUnit);
map.computeIfAbsent(time, t -> new ArrayList<>()).add(tradeStatistics);
}
});
dateMapsPerTickUnit.forEach((tick, map) -> {
dateMapsPerTickUnit.forEach((tickUnit, map) -> {
HashMap<Long, Long> priceMap = new HashMap<>();
map.forEach((date, tradeStatisticsList) -> priceMap.put(date, getAveragePrice(tradeStatisticsList)));
usdAveragePriceMapsPerTickUnit.put(tick, priceMap);
usdAveragePriceMapsPerTickUnit.put(tickUnit, priceMap);
});
return usdAveragePriceMapsPerTickUnit;
});