Compute height cells lazily to speed up DaoStateMonitorView

Avoid a bottleneck computing the cycle index & calling 'Res.get(..)' for
every block since genesis in the DAO state monitor view, when building
the DaoStateBlockListItem objects, by making the 'height' field lazy. To
do this, pass the cycle index into the constructor using an IntSupplier
and make the height a memoised 'Supplier<String>' with a custom getter.

Also add a unit test to check that the auto-generated equals & hashCode
methods still work as expected, as it isn't totally clear what Lombok
would do when a field type differs from its getter return type.
This commit is contained in:
Steven Barclay 2020-03-06 17:57:08 +08:00
parent 6ef6bf24d9
commit c261933928
No known key found for this signature in database
GPG key ID: 9FED6BF1176D500B
4 changed files with 95 additions and 13 deletions

View file

@ -23,6 +23,11 @@ import bisq.core.locale.Res;
import bisq.common.util.Utilities;
import com.google.common.base.Suppliers;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@ -31,17 +36,27 @@ import lombok.extern.slf4j.Slf4j;
@Getter
@EqualsAndHashCode
public abstract class StateBlockListItem<StH extends StateHash, StB extends StateBlock<StH>> {
protected final StateBlock<StH> stateBlock;
protected final String height;
protected final String hash;
protected final String prevHash;
protected final String numNetworkMessages;
protected final String numMisMatches;
protected final boolean isInSync;
private final StateBlock<StH> stateBlock;
private final Supplier<String> height;
private final String hash;
private final String prevHash;
private final String numNetworkMessages;
private final String numMisMatches;
private final boolean isInSync;
public String getHeight() {
return height.get();
}
protected StateBlockListItem(StB stateBlock, int cycleIndex) {
this(stateBlock, () -> cycleIndex);
}
protected StateBlockListItem(StB stateBlock, IntSupplier cycleIndexSupplier) {
this.stateBlock = stateBlock;
height = Res.get("dao.monitor.table.cycleBlockHeight", cycleIndex + 1, String.valueOf(stateBlock.getHeight()));
height = Suppliers.memoize(() ->
Res.get("dao.monitor.table.cycleBlockHeight", cycleIndexSupplier.getAsInt() + 1,
String.valueOf(stateBlock.getHeight())))::get;
hash = Utilities.bytesAsHexString(stateBlock.getHash());
prevHash = stateBlock.getPrevHash().length > 0 ? Utilities.bytesAsHexString(stateBlock.getPrevHash()) : "-";
numNetworkMessages = String.valueOf(stateBlock.getPeersMap().size());

View file

@ -22,6 +22,8 @@ import bisq.desktop.main.dao.monitor.StateBlockListItem;
import bisq.core.dao.monitoring.model.DaoStateBlock;
import bisq.core.dao.monitoring.model.DaoStateHash;
import java.util.function.IntSupplier;
import lombok.EqualsAndHashCode;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
@ -30,8 +32,7 @@ import lombok.extern.slf4j.Slf4j;
@Value
@EqualsAndHashCode(callSuper = true)
class DaoStateBlockListItem extends StateBlockListItem<DaoStateHash, DaoStateBlock> {
DaoStateBlockListItem(DaoStateBlock stateBlock, int cycleIndex) {
super(stateBlock, cycleIndex);
DaoStateBlockListItem(DaoStateBlock stateBlock, IntSupplier cycleIndexSupplier) {
super(stateBlock, cycleIndexSupplier);
}
}

View file

@ -45,6 +45,7 @@ import javafx.collections.ListChangeListener;
import java.io.File;
import java.util.Map;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;
@FxmlView
@ -126,8 +127,10 @@ public class DaoStateMonitorView extends StateMonitorView<DaoStateHash, DaoState
@Override
protected DaoStateBlockListItem getStateBlockListItem(DaoStateBlock daoStateBlock) {
int cycleIndex = periodService.getCycle(daoStateBlock.getHeight()).map(cycleService::getCycleIndex).orElse(0);
return new DaoStateBlockListItem(daoStateBlock, cycleIndex);
IntSupplier cycleIndexSupplier = () -> periodService.getCycle(daoStateBlock.getHeight())
.map(cycleService::getCycleIndex)
.orElse(0);
return new DaoStateBlockListItem(daoStateBlock, cycleIndexSupplier);
}
@Override

View file

@ -0,0 +1,63 @@
/*
* 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.dao.monitor.daostate;
import bisq.core.dao.monitoring.model.DaoStateBlock;
import bisq.core.dao.monitoring.model.DaoStateHash;
import bisq.core.locale.Res;
import java.util.Locale;
import java.util.function.IntSupplier;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class DaoStateBlockListItemTest {
@Before
public void setup() {
Locale.setDefault(new Locale("en", "US"));
Res.setBaseCurrencyCode("BTC");
Res.setBaseCurrencyName("Bitcoin");
}
@Test
public void testEqualsAndHashCode() {
var block = new DaoStateBlock(new DaoStateHash(0, new byte[0], new byte[0]));
var item1 = new DaoStateBlockListItem(block, newSupplier(1));
var item2 = new DaoStateBlockListItem(block, newSupplier(2));
var item3 = new DaoStateBlockListItem(block, newSupplier(1));
assertNotEquals(item1, item2);
assertNotEquals(item2, item3);
assertEquals(item1, item3);
assertEquals(item1.hashCode(), item3.hashCode());
}
private IntSupplier newSupplier(int i) {
//noinspection Convert2Lambda
return new IntSupplier() {
@Override
public int getAsInt() {
return i;
}
};
}
}