Skip to content

fix(kad-dht): reprovide CIDs in Kademlia key order to reduce new dials#3426

Open
paschal533 wants to merge 1 commit intolibp2p:mainfrom
paschal533:fix/kad-dht-reprovide-kademlia-key-order
Open

fix(kad-dht): reprovide CIDs in Kademlia key order to reduce new dials#3426
paschal533 wants to merge 1 commit intolibp2p:mainfrom
paschal533:fix/kad-dht-reprovide-kademlia-key-order

Conversation

@paschal533
Copy link
Copy Markdown
Contributor

@paschal533 paschal533 commented Mar 31, 2026

Summary

During a reprovide run, the reprovider iterates all stored CIDs and calls provide() for each one. Each provide() opens connections to the K closest peers for that CID. Because CIDs are processed in datastore iteration order (essentially random), each CID's K closest peers are likely different, so every CID requires a fresh set of dials.

This ports the SweepingProvider optimisation from go-libp2p: collect all CIDs that need reproviding, sort them by their Kademlia key, then queue them in that order.

XOR-adjacent CIDs share the same K closest peers. By processing them consecutively, the connections opened for one CID are still live when the next CID is queued, so they get reused instead of requiring new dials. Over a full reprovide run with many CIDs this significantly reduces the total number of new connections opened.

Changes

  • src/reprovider.ts: collect CIDs into an array before queueing, sort by Kademlia key, then queue in sorted order
  • test/reprovider.spec.ts: inserts 5 CIDs in reverse Kademlia key order and verifies contentRouting.provide is called in ascending Kademlia key order

Test plan

  • should reprovide in Kademlia key order inserts CIDs in reverse order, verifies provide calls arrive in correct ascending Kademlia key order
  • All 150 existing tests continue to pass
  • Lint and TypeScript build clean

Collect all CIDs that need reproviding before queueing them, sort by
their Kademlia key, then queue in that order. XOR-adjacent CIDs share
the same K closest peers, so connections opened for one CID are reused
for the next, reducing the number of new dials across a reprovide run.

Ports the SweepingProvider optimisation from go-libp2p:
libp2p/go-libp2p#2774
@paschal533 paschal533 requested a review from a team as a code owner March 31, 2026 10:05
@paschal533 paschal533 marked this pull request as draft March 31, 2026 10:06
@paschal533 paschal533 marked this pull request as ready for review March 31, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant