Skip to content

perf(recv): add try_recv fast path in packet receive loop#2426

Open
fanyang89 wants to merge 1 commit into
EasyTier:mainfrom
fanyang89:perf/recv-try-fast-path
Open

perf(recv): add try_recv fast path in packet receive loop#2426
fanyang89 wants to merge 1 commit into
EasyTier:mainfrom
fanyang89:perf/recv-try-fast-path

Conversation

@fanyang89

Copy link
Copy Markdown
Member

Summary

The peer packet receive hot path (start_peer_recv) calls recv_packet_from_chan().await for every incoming packet, which always parks the task and yields to the Tokio scheduler even when the channel already has items buffered.

This adds a try_recv() fast path: if a packet is immediately available, it is processed inline without scheduling overhead. Only when the channel is empty do we fall back to recv().await.

Changes

  • peers/mod.rsrecv_packet_from_chan(): try try_recv() first, fall back to recv().await on Empty.
  • peers/peer_manager.rsstart_peer_recv: inline the same try_recvrecv().await pattern in the hot loop, and remove the now-unused import.

Impact

~20 lines, 2 files. No behavior change — purely a fast-path optimization that avoids an unnecessary task park/wakeup cycle per packet when the channel is non-empty (the common case under load).

Copilot AI review requested due to automatic review settings July 11, 2026 12:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the peer packet receive hot path by adding a non-async try_recv() fast path before falling back to recv().await, aiming to reduce overhead when packets are already buffered in the channel.

Changes:

  • Add try_recv() fast path to recv_packet_from_chan() and fall back to recv().await only when empty.
  • Inline the same try_recv()recv().await logic in PeerManager::start_peer_recv() and remove the previous helper import.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
easytier/src/peers/peer_manager.rs Inlines a try_recv() fast path in the main receive loop and removes the helper import.
easytier/src/peers/mod.rs Updates recv_packet_from_chan() to use try_recv() first, then await recv() on empty.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread easytier/src/peers/peer_manager.rs Outdated
Add a try_recv() fast path to recv_packet_from_chan(): if a packet is
immediately available, return it without parking the task. Only when
the channel is empty do we fall back to recv().await.

This benefits all callers of recv_packet_from_chan() including
start_peer_recv, virtual_nic, foreign_network_manager, and instance.
@fanyang89
fanyang89 force-pushed the perf/recv-try-fast-path branch from 8093dc8 to 82b4013 Compare July 11, 2026 13:30
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.

2 participants