From caf946dfe009c3500aef90cf61f1f4b6dc64c70a Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Thu, 21 Nov 2019 10:13:20 -0800 Subject: [PATCH] Remove redundant HashSet lookups in filter functions The appendOnlyDataStoreService and map already have unique keys that are based on the hash of the payload. This would catch instances where: PersistableNetworkPayload - None: The key is based on ByteArray(payload.getHash()) which is the same as this check. ProtectedStorageEntry - Cases where multiple PSEs contain payloads that have equivalent hashCode(), but different data.toProtoMessage().toByteArray(). I don't think it is a good idea to keep 2 "unique" methods on payloads. This is likely left over from a time when Payload hashCode() needed to be different than the hash of the payload. --- .../java/bisq/network/p2p/storage/P2PDataStorage.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java index db09ef9449..091d56788d 100644 --- a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java +++ b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java @@ -263,8 +263,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers int maxEntries, AtomicBoolean outPersistableNetworkPayloadOutputTruncated, Capabilities peerCapabilities) { - Set tempLookupSet = new HashSet<>(); - Set excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys()); AtomicInteger maxSize = new AtomicInteger(maxEntries); Set result = this.appendOnlyDataStoreService.getMap().entrySet().stream() @@ -272,10 +270,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers .filter(e -> maxSize.decrementAndGet() >= 0) .map(Map.Entry::getValue) .filter(persistableNetworkPayload -> shouldTransmitPayloadToPeer(peerCapabilities, persistableNetworkPayload)) - .filter(payload -> { - boolean notContained = tempLookupSet.add(new P2PDataStorage.ByteArray(payload.getHash())); - return notContained; - }) .collect(Collectors.toSet()); if (maxSize.get() <= 0) @@ -289,7 +283,6 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers AtomicBoolean outProtectedStorageEntryOutputTruncated, Capabilities peerCapabilities) { Set filteredDataSet = new HashSet<>(); - Set lookupSet = new HashSet<>(); AtomicInteger maxSize = new AtomicInteger(maxEntries); Set excludedKeysAsByteArray = P2PDataStorage.ByteArray.convertBytesSetToByteArraySet(getDataRequest.getExcludedKeys()); @@ -315,9 +308,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers doAdd = true; } if (doAdd) { - boolean notContained = lookupSet.add(protectedStoragePayload.hashCode()); - if (notContained) - filteredDataSet.add(protectedStorageEntry); + filteredDataSet.add(protectedStorageEntry); } } return filteredDataSet;