Skip to content

Commit

Permalink
Make ipns publishing 2X faster (45s down to 15s)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Dec 11, 2023
1 parent 71be5c4 commit f30a124
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/main/java/org/peergos/protocol/dht/Kademlia.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private int compareKeys(RoutingEntry a, RoutingEntry b, Id keyId) {
return a.addresses.peerId.toBase58().compareTo(b.addresses.peerId.toBase58());
}

private final ExecutorService ioExec = Executors.newFixedThreadPool(16);

public List<PeerAddresses> findClosestPeers(Multihash peerIdkey, int maxCount, Host us) {
byte[] key = peerIdkey.toBytes();
Id keyId = Id.create(Hash.sha256(key), 256);
Expand All @@ -154,19 +156,20 @@ public List<PeerAddresses> findClosestPeers(Multihash peerIdkey, int maxCount, H
Set<Multihash> queried = Collections.synchronizedSet(new HashSet<>());
int queryParallelism = 3;
while (true) {
List<CompletableFuture<List<PeerAddresses>>> futures = toQuery.stream()
List<RoutingEntry> thisRound = toQuery.stream()
.limit(queryParallelism)
.parallel()
.collect(Collectors.toList());
List<Future<List<PeerAddresses>>> futures = thisRound.stream()
.map(r -> {
toQuery.remove(r);
queried.add(r.addresses.peerId);
return getCloserPeers(peerIdkey, r.addresses, us);
return ioExec.submit(() -> getCloserPeers(peerIdkey, r.addresses, us).join());
})
.collect(Collectors.toList());
boolean foundCloser = false;
for (CompletableFuture<List<PeerAddresses>> future : futures) {
for (Future<List<PeerAddresses>> future : futures) {
try {
List<PeerAddresses> result = future.join();
List<PeerAddresses> result = future.get();
for (PeerAddresses peer : result) {
if (!queried.contains(peer.peerId)) {
// exit early if we are looking for the specific node
Expand Down Expand Up @@ -316,19 +319,19 @@ public CompletableFuture<Void> publishValue(PrivKey priv,
LocalDateTime expiry,
long ttlNanos,
Host us) {
int publishes = 0;
for (int i=0; i < 5 && publishes < 20; i++) {
Set<Multihash> publishes = Collections.synchronizedSet(new HashSet<>());
for (int i=0; i < 5 && publishes.size() < 20; i++) {
List<PeerAddresses> closestPeers = findClosestPeers(publisher, 25, us);
publishes += closestPeers.stream().parallel().mapToInt(peer -> {
closestPeers.forEach(peer -> ioExec.submit(() -> {
try {
boolean success = dialPeer(peer, us).join()
dialPeer(peer, us).join()
.putValue(publishValue, expiry, sequence,
ttlNanos, publisher, priv).join();
if (success)
return 1;
ttlNanos, publisher, priv).thenAccept(success -> {
if (success)
publishes.add(peer.peerId);
});
} catch (Exception e) {}
return 0;
}).sum();
}));
}
return CompletableFuture.completedFuture(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/KademliaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void ipnsBenchmark() throws Exception {

List<PrivKey> signers = new ArrayList<>();
long publishTotal = 0, resolveTotal = 0;
int iterations = 10;
int iterations = 5;
for (int i = 0; i < iterations; i++) {
// publish mapping from node 1
PrivKey signer = Ed25519Kt.generateEd25519KeyPair().getFirst();
Expand Down

0 comments on commit f30a124

Please sign in to comment.