Skip to content

Commit 9c687a7

Browse files
fix: avoid allocations
1 parent 17d90ff commit 9c687a7

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

iroh/src/magicsock/remote_map/remote_state.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -357,22 +357,6 @@ impl RemoteStateActor {
357357
}
358358
}
359359

360-
async fn send_datagram(
361-
&mut self,
362-
dst: transports::Addr,
363-
owned_transmit: OwnedTransmit,
364-
) -> n0_error::Result<()> {
365-
let transmit = transports::Transmit {
366-
ecn: owned_transmit.ecn,
367-
contents: owned_transmit.contents.as_ref(),
368-
segment_size: owned_transmit.segment_size,
369-
};
370-
std::future::poll_fn(|cx| Pin::new(&mut self.sender).poll_send(cx, &dst, None, &transmit))
371-
.await
372-
.with_context(|_| format!("failed to send datagram to {dst:?}"))?;
373-
Ok(())
374-
}
375-
376360
/// Handles [`RemoteStateMessage::SendDatagram`].
377361
async fn handle_msg_send_datagram(&mut self, transmit: OwnedTransmit) {
378362
// Sending datagrams might fail, e.g. because we don't have the right transports set
@@ -384,7 +368,7 @@ impl RemoteStateActor {
384368
if let Some(addr) = self.selected_path.get() {
385369
trace!(?addr, "sending datagram to selected path");
386370

387-
if let Err(err) = self.send_datagram(addr.clone(), transmit).await {
371+
if let Err(err) = send_datagram(&mut self.sender, addr.clone(), transmit).await {
388372
debug!(?addr, "failed to send datagram on selected_path: {err:#}");
389373
}
390374
} else {
@@ -395,19 +379,21 @@ impl RemoteStateActor {
395379
if self.paths.is_empty() {
396380
warn!("Cannot send datagrams: No paths to remote endpoint known");
397381
}
398-
let addrs: Vec<_> = self.paths.addrs().cloned().collect();
399-
for addr in addrs {
382+
383+
for addr in self.paths.addrs() {
400384
// We never want to send to our local addresses.
401385
// The local address set is updated in the main loop so we can use `peek` here.
402386
if let transports::Addr::Ip(sockaddr) = addr
403387
&& self
404388
.local_direct_addrs
405389
.peek()
406390
.iter()
407-
.any(|a| a.addr == sockaddr)
391+
.any(|a| a.addr == *sockaddr)
408392
{
409393
trace!(%sockaddr, "not sending datagram to our own address");
410-
} else if let Err(err) = self.send_datagram(addr.clone(), transmit.clone()).await {
394+
} else if let Err(err) =
395+
send_datagram(&mut self.sender, addr.clone(), transmit.clone()).await
396+
{
411397
debug!(?addr, "failed to send datagram: {err:#}");
412398
}
413399
}
@@ -980,6 +966,24 @@ impl RemoteStateActor {
980966
}
981967
}
982968

969+
fn send_datagram<'a>(
970+
sender: &'a mut TransportsSender,
971+
dst: transports::Addr,
972+
owned_transmit: OwnedTransmit,
973+
) -> impl Future<Output = n0_error::Result<()>> + 'a {
974+
std::future::poll_fn(move |cx| {
975+
let transmit = transports::Transmit {
976+
ecn: owned_transmit.ecn,
977+
contents: owned_transmit.contents.as_ref(),
978+
segment_size: owned_transmit.segment_size,
979+
};
980+
981+
Pin::new(&mut *sender)
982+
.poll_send(cx, &dst, None, &transmit)
983+
.map(|res| res.with_context(|_| format!("failed to send datagram to {dst:?}")))
984+
})
985+
}
986+
983987
/// Messages to send to the [`RemoteStateActor`].
984988
#[derive(derive_more::Debug)]
985989
pub(crate) enum RemoteStateMessage {

0 commit comments

Comments
 (0)