Memoise block times to speed up display of BSQ issued graph

Avoid repeatedly calling DaoFacade.getBlockTime for Issuance objects
with the same chain height, as that method linearly scans the entire
linked list of DaoState blocks, making it quite slow. Instead, memoise
the mapping from chain height to block-time month, so that it is only
computed once per graph point instead of once for every BSQ issuance.
This commit is contained in:
Steven Barclay 2020-02-10 04:46:59 +00:00
parent 97b9f733ab
commit 459f0db661
No known key found for this signature in database
GPG key ID: 9FED6BF1176D500B

View file

@ -78,8 +78,10 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterators.AbstractSpliterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -601,6 +603,11 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
}
private void updateBSQIssuedMonthly() {
Function<Integer, LocalDate> blockTimeFn = memoize(height ->
Instant.ofEpochMilli(daoFacade.getBlockTime(height)).atZone(ZoneId.systemDefault())
.toLocalDate()
.with(ADJUSTERS.get(MONTH)));
Stream<Issuance> bsqByCompensation = daoStateService.getIssuanceSet(IssuanceType.COMPENSATION).stream()
.sorted(Comparator.comparing(Issuance::getChainHeight));
@ -608,10 +615,7 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
.sorted(Comparator.comparing(Issuance::getChainHeight));
Map<LocalDate, List<Issuance>> bsqAddedByVote = Stream.concat(bsqByCompensation, bsqByReimbursement)
.collect(Collectors.groupingBy(item -> Instant.ofEpochMilli(daoFacade.getBlockTime(item.getChainHeight()))
.atZone(ZoneId.systemDefault())
.toLocalDate()
.with(ADJUSTERS.get(MONTH))));
.collect(Collectors.groupingBy(blockTimeFn.compose(Issuance::getChainHeight)));
List<XYChart.Data<Number, Number>> updatedAddedBSQ = bsqAddedByVote.keySet().stream()
.map(date -> {
@ -701,4 +705,9 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
};
return StreamSupport.stream(spliterator, false);
}
private static <T, R> Function<T, R> memoize(Function<T, R> fn) {
Map<T, R> map = new ConcurrentHashMap<>();
return x -> map.computeIfAbsent(x, fn);
}
}