mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 23:18:17 +01:00
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:
parent
97b9f733ab
commit
459f0db661
1 changed files with 13 additions and 4 deletions
|
@ -78,8 +78,10 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Spliterators.AbstractSpliterator;
|
import java.util.Spliterators.AbstractSpliterator;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -601,6 +603,11 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateBSQIssuedMonthly() {
|
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()
|
Stream<Issuance> bsqByCompensation = daoStateService.getIssuanceSet(IssuanceType.COMPENSATION).stream()
|
||||||
.sorted(Comparator.comparing(Issuance::getChainHeight));
|
.sorted(Comparator.comparing(Issuance::getChainHeight));
|
||||||
|
|
||||||
|
@ -608,10 +615,7 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
|
||||||
.sorted(Comparator.comparing(Issuance::getChainHeight));
|
.sorted(Comparator.comparing(Issuance::getChainHeight));
|
||||||
|
|
||||||
Map<LocalDate, List<Issuance>> bsqAddedByVote = Stream.concat(bsqByCompensation, bsqByReimbursement)
|
Map<LocalDate, List<Issuance>> bsqAddedByVote = Stream.concat(bsqByCompensation, bsqByReimbursement)
|
||||||
.collect(Collectors.groupingBy(item -> Instant.ofEpochMilli(daoFacade.getBlockTime(item.getChainHeight()))
|
.collect(Collectors.groupingBy(blockTimeFn.compose(Issuance::getChainHeight)));
|
||||||
.atZone(ZoneId.systemDefault())
|
|
||||||
.toLocalDate()
|
|
||||||
.with(ADJUSTERS.get(MONTH))));
|
|
||||||
|
|
||||||
List<XYChart.Data<Number, Number>> updatedAddedBSQ = bsqAddedByVote.keySet().stream()
|
List<XYChart.Data<Number, Number>> updatedAddedBSQ = bsqAddedByVote.keySet().stream()
|
||||||
.map(date -> {
|
.map(date -> {
|
||||||
|
@ -701,4 +705,9 @@ public class SupplyView extends ActivatableView<GridPane, Void> implements DaoSt
|
||||||
};
|
};
|
||||||
return StreamSupport.stream(spliterator, false);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue