Skip to content

Commit 925b5b3

Browse files
perf: various improvements
1 parent 2f924d9 commit 925b5b3

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ unused-async = "warn"
4343

4444

4545
[patch.crates-io]
46+
n0-watcher = { git = "https://github.com/n0-computer/n0-watcher", branch = "feat-get-ref" }
47+
4648
iroh-quinn = { git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
4749
iroh-quinn-proto = { git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
4850
iroh-quinn-udp = { git = "https://github.com/n0-computer/quinn", branch = "main-iroh" }
@@ -52,3 +54,10 @@ netwatch = { git = "https://github.com/n0-computer/net-tools", branch = "main" }
5254
# iroh-quinn = { path = "../iroh-quinn/quinn" }
5355
# iroh-quinn-proto = { path = "../iroh-quinn/quinn-proto" }
5456
# iroh-quinn-udp = { path = "../iroh-quinn/quinn-udp" }
57+
58+
59+
[patch."https://github.com/n0-computer/quinn"]
60+
61+
# iroh-quinn = { path = "../iroh-quinn/quinn" }
62+
# iroh-quinn-proto = { path = "../iroh-quinn/quinn-proto" }
63+
# iroh-quinn-udp = { path = "../iroh-quinn/quinn-udp" }

iroh/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ rustls = { version = "0.23.33", default-features = false, features = ["ring"] }
5050
serde = { version = "1.0.219", features = ["derive", "rc"] }
5151
smallvec = "1.11.1"
5252
strum = { version = "0.27", features = ["derive"] }
53+
tinyvec = { version = "1.10.0", features = ["alloc"] }
5354
tokio = { version = "1.44.1", features = [
5455
"io-util",
5556
"macros",

iroh/src/magicsock.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ pub(crate) mod endpoint_map;
7575
pub(crate) mod mapped_addrs;
7676
pub(crate) mod transports;
7777

78-
use mapped_addrs::{EndpointIdMappedAddr, MappedAddr};
78+
use self::{
79+
mapped_addrs::{EndpointIdMappedAddr, MappedAddr},
80+
transports::Addr,
81+
};
7982

8083
pub use self::{endpoint_map::PathInfo, metrics::Metrics};
8184

@@ -489,18 +492,18 @@ impl MagicSock {
489492

490493
#[cfg_attr(windows, allow(dead_code))]
491494
fn normalized_local_addr(&self) -> io::Result<SocketAddr> {
492-
let addrs = self.local_addrs_watch.clone().get();
495+
let addrs = self.local_addrs_watch.get_ref();
493496

494497
let mut ipv4_addr = None;
495498
for addr in addrs {
496-
let Some(addr) = addr.into_socket_addr() else {
497-
continue;
498-
};
499-
if addr.is_ipv6() {
500-
return Ok(addr);
501-
}
502-
if addr.is_ipv4() && ipv4_addr.is_none() {
503-
ipv4_addr.replace(addr);
499+
match addr {
500+
Addr::Ip(addr @ SocketAddr::V6(_)) => {
501+
return Ok(*addr);
502+
}
503+
Addr::Ip(addr @ SocketAddr::V4(_)) if ipv4_addr.is_none() => {
504+
ipv4_addr.replace(*addr);
505+
}
506+
_ => {}
504507
}
505508
}
506509
match ipv4_addr {
@@ -550,11 +553,12 @@ impl MagicSock {
550553

551554
let mut quic_packets_total = 0;
552555

553-
for ((quinn_meta, buf), source_addr) in metas
554-
.iter_mut()
555-
.zip(bufs.iter_mut())
556-
.zip(source_addrs.iter())
557-
{
556+
// zip is slow :(
557+
for i in 0..metas.len() {
558+
let quinn_meta = &mut metas[i];
559+
let buf = &mut bufs[i];
560+
let source_addr = &source_addrs[i];
561+
558562
let mut buf_contains_quic_datagrams = false;
559563
let mut quic_datagram_count = 0;
560564
if quinn_meta.len > quinn_meta.stride {
@@ -578,11 +582,9 @@ impl MagicSock {
578582
// relies on quinn::EndpointConfig::grease_quic_bit being set to `false`,
579583
// which we do in Endpoint::bind.
580584
if let Some((sender, sealed_box)) = disco::source_and_box(datagram) {
581-
trace!(src = ?source_addr, len = datagram.len(), "UDP recv: DISCO packet");
582585
self.handle_disco_message(sender, sealed_box, source_addr);
583586
datagram[0] = 0u8;
584587
} else {
585-
trace!(src = ?source_addr, len = datagram.len(), "UDP recv: QUIC packet");
586588
match source_addr {
587589
transports::Addr::Ip(SocketAddr::V4(..)) => {
588590
self.metrics

iroh/src/magicsock/transports.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
io::{self, IoSliceMut},
44
net::{IpAddr, Ipv6Addr, SocketAddr, SocketAddrV6},
55
pin::Pin,
6-
sync::{Arc, atomic::AtomicUsize},
6+
sync::Arc,
77
task::{Context, Poll},
88
};
99

@@ -34,7 +34,8 @@ pub(crate) struct Transports {
3434
ip: Vec<IpTransport>,
3535
relay: Vec<RelayTransport>,
3636

37-
poll_recv_counter: AtomicUsize,
37+
poll_recv_counter: usize,
38+
source_addrs: tinyvec::TinyVec<[Addr; 4]>, // cache for source addrs
3839
}
3940

4041
#[cfg(not(wasm_browser))]
@@ -69,6 +70,7 @@ impl Transports {
6970
ip,
7071
relay,
7172
poll_recv_counter: Default::default(),
73+
source_addrs: Default::default(),
7274
}
7375
}
7476

@@ -84,11 +86,11 @@ impl Transports {
8486
return Poll::Pending;
8587
}
8688

87-
let mut source_addrs = vec![Addr::default(); metas.len()];
88-
match self.inner_poll_recv(cx, bufs, metas, &mut source_addrs)? {
89+
self.source_addrs.resize_with(metas.len(), Addr::default);
90+
match self.inner_poll_recv(cx, bufs, metas)? {
8991
Poll::Pending | Poll::Ready(0) => Poll::Pending,
9092
Poll::Ready(n) => {
91-
msock.process_datagrams(&mut bufs[..n], &mut metas[..n], &source_addrs[..n]);
93+
msock.process_datagrams(&mut bufs[..n], &mut metas[..n], &self.source_addrs[..n]);
9294
Poll::Ready(Ok(n))
9395
}
9496
}
@@ -100,13 +102,12 @@ impl Transports {
100102
cx: &mut Context,
101103
bufs: &mut [IoSliceMut<'_>],
102104
metas: &mut [quinn_udp::RecvMeta],
103-
source_addrs: &mut [Addr],
104105
) -> Poll<io::Result<usize>> {
105106
debug_assert_eq!(bufs.len(), metas.len(), "non matching bufs & metas");
106107

107108
macro_rules! poll_transport {
108109
($socket:expr) => {
109-
match $socket.poll_recv(cx, bufs, metas, source_addrs)? {
110+
match $socket.poll_recv(cx, bufs, metas, &mut self.source_addrs)? {
110111
Poll::Pending | Poll::Ready(0) => {}
111112
Poll::Ready(n) => {
112113
return Poll::Ready(Ok(n));
@@ -117,9 +118,7 @@ impl Transports {
117118

118119
// To improve fairness, every other call reverses the ordering of polling.
119120

120-
let counter = self
121-
.poll_recv_counter
122-
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
121+
let counter = self.poll_recv_counter.wrapping_add(1);
123122

124123
if counter % 2 == 0 {
125124
#[cfg(not(wasm_browser))]

0 commit comments

Comments
 (0)