Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion benches/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,11 @@ impl Iterator for Ipv4NetworkAddrs {
)))
}

#[inline]
fn count(self) -> usize {
self.len()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.front > self.back {
Expand Down Expand Up @@ -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<usize>) {
if self.front > self.back {
Expand Down
Loading