Skip to content

Commit

Permalink
Optimize GetPooledTransactionsFromPeerTask with HashSet (#6071)
Browse files Browse the repository at this point in the history
Switch from using a List to a HashSet for transaction hashes in GetPooledTransactionsFromPeerTask to improve performance.

Signed-off-by: Suyash Nayan <[email protected]>
Signed-off-by: Sally MacFarlane <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
  • Loading branch information
7suyash7 and macfarla authored Oct 25, 2023
1 parent 909649f commit a2dbb82
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,20 +40,20 @@ public class GetPooledTransactionsFromPeerTask extends AbstractPeerRequestTask<L
private static final Logger LOG =
LoggerFactory.getLogger(GetPooledTransactionsFromPeerTask.class);

private final List<Hash> hashes;
private final Set<Hash> hashes;

private GetPooledTransactionsFromPeerTask(
final EthContext ethContext, final List<Hash> hashes, final MetricsSystem metricsSystem) {
super(ethContext, EthPV65.GET_POOLED_TRANSACTIONS, metricsSystem);
this.hashes = List.copyOf(hashes);
this.hashes = new HashSet<>(hashes);
}

public static GetPooledTransactionsFromPeerTask forHashes(
final EthContext ethContext, final List<Hash> hashes, final MetricsSystem metricsSystem) {
return new GetPooledTransactionsFromPeerTask(ethContext, hashes, metricsSystem);
}

public List<Hash> getTransactionHashes() {
public Set<Hash> getTransactionHashes() {
return hashes;
}

Expand All @@ -60,7 +62,7 @@ protected PendingPeerRequest sendRequest() {
return sendRequestToPeer(
peer -> {
LOG.debug("Requesting {} transaction pool entries from peer {}.", hashes.size(), peer);
return peer.getPooledTransactions(hashes);
return peer.getPooledTransactions(new ArrayList<>(hashes));
},
0);
}
Expand Down

0 comments on commit a2dbb82

Please sign in to comment.