diff --git a/benches/net.rs b/benches/net.rs index 7be5f19..09a306a 100644 --- a/benches/net.rs +++ b/benches/net.rs @@ -75,13 +75,38 @@ fn bench_net_addrs(c: &mut Criterion) { fn bench_net_addrs_count(c: &mut Criterion) { let mut group = c.benchmark_group("netip"); - group.throughput(Throughput::Elements(256)); group.bench_function("Ipv4Network::addrs count /24", |b| { let net = Ipv4Network::parse("77.88.55.0/24").unwrap(); b.iter(|| { core::hint::black_box(net.addrs().count()); }); }); + + group.bench_function("Ipv6Network::addrs count /120", |b| { + let net = Ipv6Network::parse("2a02:6b8:c00::1234:0:0/120").unwrap(); + b.iter(|| { + core::hint::black_box(net.addrs().count()); + }); + }); + + group.bench_function("Ipv6Network::addrs count /112", |b| { + let net = Ipv6Network::parse("2a02:6b8:c00::1234:0:0/112").unwrap(); + b.iter(|| { + core::hint::black_box(net.addrs().count()); + }); + }); + + group.bench_function("Ipv6Network::addrs count non-contiguous 8 host bits", |b| { + let net = Ipv6Network::new( + Ipv6Addr::new(0x2a02, 0x6b8, 0xc00, 0, 0, 0x1234, 0, 0), + Ipv6Addr::new(0xffff, 0xffff, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff), + ); + b.iter(|| { + core::hint::black_box(net.addrs().count()); + }); + }); + + group.finish(); } fn bench_intersection(c: &mut Criterion) { diff --git a/src/net.rs b/src/net.rs index 77e5445..53549e5 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1359,6 +1359,11 @@ impl Iterator for Ipv4NetworkAddrs { ))) } + #[inline] + fn count(self) -> usize { + self.len() + } + #[inline] fn size_hint(&self) -> (usize, Option) { if self.front > self.back { @@ -2404,6 +2409,12 @@ impl Iterator for Ipv6NetworkAddrs { ))) } + #[inline] + fn count(self) -> usize { + let (n, ..) = self.size_hint(); + n + } + #[inline] fn size_hint(&self) -> (usize, Option) { if self.front > self.back {