Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removes redundant vector allocations before calling sendmmsg::batch_send #4129

Merged

Conversation

behzadnouri
Copy link

@behzadnouri behzadnouri commented Dec 15, 2024

Problem

streamer::sendmmsg::batch_send only requires an ExactSizeIterator:
https://github.com/anza-xyz/agave/blob/82347779f/streamer/src/sendmmsg.rs#L169-L175

Collecting an iterator into a vector before calling batch_send is unnecessary and only adds overhead.
In particular multi_target_send used in retransmitting shreds can be use without doing an additional vector allocation:
https://github.com/anza-xyz/agave/blob/82347779f/streamer/src/sendmmsg.rs#L197

Summary of Changes

Removed redundant vector allocations before calling streamer::sendmmsg::batch_send.

@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch 2 times, most recently from 365214b to 43d93a0 Compare December 15, 2024 19:33
@behzadnouri behzadnouri marked this pull request as draft December 15, 2024 20:46
@behzadnouri behzadnouri added noCI Suppress CI on this Pull Request and removed noCI Suppress CI on this Pull Request labels Dec 15, 2024
@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch 2 times, most recently from 19acb5b to bbb62dc Compare December 16, 2024 21:31
@behzadnouri behzadnouri marked this pull request as ready for review December 16, 2024 22:46
@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch 3 times, most recently from d73c5db to 0142799 Compare December 18, 2024 18:03
@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch from 0142799 to 8ed7047 Compare January 9, 2025 18:19
alexpyattaev
alexpyattaev previously approved these changes Jan 17, 2025
Copy link

@alexpyattaev alexpyattaev left a comment

Choose a reason for hiding this comment

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

lgtm!

@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch 2 times, most recently from 6cedd30 to 1e221d9 Compare February 8, 2025 16:05
@@ -1249,14 +1249,13 @@ impl ServeRepair {
}
}
if !pending_pongs.is_empty() {
match batch_send(repair_socket, &pending_pongs) {
let num_pkts = pending_pongs.len();
let pending_pongs = pending_pongs.iter().map(|(bytes, addr)| (bytes, addr));

Choose a reason for hiding this comment

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

this looks redundant... why map without any actual transform?

Copy link
Author

Choose a reason for hiding this comment

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

The closure is converting &(S, T) to (&S, &T).
The code will not build without it.

Choose a reason for hiding this comment

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

Oh I see. rust sometimes works in mysterious ways!

@@ -362,7 +362,7 @@ fn recv_send(
let data = pkt.data(..)?;
socket_addr_space.check(&addr).then_some((data, addr))
});
batch_send(sock, &packets.collect::<Vec<_>>())?;
batch_send(sock, packets.collect::<Vec<_>>())?;

Choose a reason for hiding this comment

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

why are we still doing collect here if we can pass iterators now?

Copy link
Author

Choose a reason for hiding this comment

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

We need an ExactSizeIterator. A filter_map is not an ExactSizeIterator because the number of items filtered out are not known.

Copy link

@alexpyattaev alexpyattaev left a comment

Choose a reason for hiding this comment

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

Thank you, this is probably a good direction given how many packets we send. Left a few small questions in the code.

streamer::sendmmsg::batch_send only requires an ExactSizeIterator:
https://github.com/anza-xyz/agave/blob/566bb9565/streamer/src/sendmmsg.rs#L203-L204
https://github.com/anza-xyz/agave/blob/566bb9565/streamer/src/sendmmsg.rs#L166-L175

Collecting an iterator into a vector before calling batch_send is
unnecessary and only adds overhead.
In particular multi_target_send used in retransmitting shreds can be use
without doing an additional vector allocation:
https://github.com/anza-xyz/agave/blob/566bb9565/streamer/src/sendmmsg.rs#L219
@behzadnouri behzadnouri force-pushed the multi-target-send-rm-vec-collect branch from 1e221d9 to 774f0d8 Compare February 10, 2025 01:47
Copy link

@alexpyattaev alexpyattaev left a comment

Choose a reason for hiding this comment

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

good to go

@behzadnouri behzadnouri merged commit c857274 into anza-xyz:master Feb 10, 2025
47 checks passed
@behzadnouri behzadnouri deleted the multi-target-send-rm-vec-collect branch February 10, 2025 17:19
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