Skip to content

perf(p2p): randomize contact selection for RLPx connection initiation#6503

Closed
azteca1998 wants to merge 1 commit intofix/kademlia-snapsync-peer-pruningfrom
perf/kademlia-initiation-improvements
Closed

perf(p2p): randomize contact selection for RLPx connection initiation#6503
azteca1998 wants to merge 1 commit intofix/kademlia-snapsync-peer-pruningfrom
perf/kademlia-initiation-improvements

Conversation

@azteca1998
Copy link
Copy Markdown
Contributor

Summary

  • Randomize contact selection in get_contact_to_initiate to avoid sequential bucket traversal bias

Builds on #6497 (peer pruning fix).

Problem

After #6458 introduced the Kademlia k-bucket routing table, snap sync times regressed (~50-75% slower across hoodi, sepolia, mainnet). Part of the slowdown comes from do_get_contact_to_initiate iterating buckets sequentially from 0→255. Due to XOR distance distribution, ~87% of contacts cluster in buckets 253-255, while buckets 0-200 are nearly empty. Sequential traversal wastes connection attempts on sparse low-numbered buckets before reaching the dense high-numbered ones where most peers live.

Changes

Replace the sequential for bucket in &self.buckets loop with random selection from all eligible contacts across all buckets (main + replacements). This follows the same pattern already used by get_contact_for_lookup and get_contact_for_enr_lookup.

Test plan

  • All 33 p2p unit tests pass
  • Run daily snapsync test and compare sync times against baseline

Replace sequential bucket traversal (0→255) in get_contact_to_initiate
with random selection across all eligible contacts. The sequential
approach was biased toward sparse low-numbered buckets, wasting
connection attempts before reaching the dense high-numbered buckets
(253-255) where ~87% of contacts cluster due to XOR distance
distribution.
@github-actions github-actions Bot added the performance Block execution throughput and performance in general label Apr 20, 2026
@github-actions
Copy link
Copy Markdown

Lines of code report

Total lines added: 29
Total lines removed: 0
Total lines changed: 29

Detailed view
+------------------------------------------------+-------+------+
| File                                           | Lines | Diff |
+------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/peer_handler.rs   | 555   | +4   |
+------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/peer_table.rs     | 1253  | +24  |
+------------------------------------------------+-------+------+
| ethrex/crates/networking/p2p/sync/snap_sync.rs | 1020  | +1   |
+------------------------------------------------+-------+------+

@azteca1998 azteca1998 changed the base branch from main to fix/kademlia-snapsync-peer-pruning April 20, 2026 12:03
azteca1998 added a commit that referenced this pull request Apr 20, 2026
Merge PR #6503 (randomized contact selection) into PR #6504 (flat
connection pool). The connection pool approach already includes
randomization, so we keep its version of do_get_contact_to_initiate.
@azteca1998
Copy link
Copy Markdown
Contributor Author

Superseded by #6511 (Kademlia v2) — randomized selection replaced by O(1) random index probe.

@azteca1998 azteca1998 closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Block execution throughput and performance in general

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant