-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #121428 - okaneco:ipaddr_parse, r=cuviper
net: Don't use checked arithmetic when parsing numbers with known max digits Add a branch to `Parser::read_number` that determines whether checked or regular arithmetic is used. - If `max_digits.is_some()`, then we know we are parsing a `u8` or `u16` because `read_number` is only called with `Some(3)` or `Some(4)`. Both types fit within a `u32` without risk of overflow. Thus, we can use plain arithmetic to avoid extra instructions from `checked_mul` and `checked_add`. Add benches for `IpAddr`, `Ipv4Addr`, `Ipv6Addr`, `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6` parsing
- Loading branch information
Showing
4 changed files
with
131 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ mod char; | |
mod fmt; | ||
mod hash; | ||
mod iter; | ||
mod net; | ||
mod num; | ||
mod ops; | ||
mod pattern; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; | ||
use core::str::FromStr; | ||
|
||
use test::{black_box, Bencher}; | ||
|
||
const IPV4_STR: &str = "192.168.0.1"; | ||
const IPV4_STR_PORT: &str = "192.168.0.1:8080"; | ||
|
||
const IPV6_STR_FULL: &str = "2001:db8:0:0:0:0:c0a8:1"; | ||
const IPV6_STR_COMPRESS: &str = "2001:db8::c0a8:1"; | ||
const IPV6_STR_V4: &str = "2001:db8::192.168.0.1"; | ||
const IPV6_STR_PORT: &str = "[2001:db8::c0a8:1]:8080"; | ||
const IPV6_STR_PORT_SCOPE_ID: &str = "[2001:db8::c0a8:1%1337]:8080"; | ||
|
||
#[bench] | ||
fn bench_parse_ipv4(b: &mut Bencher) { | ||
b.iter(|| Ipv4Addr::from_str(black_box(IPV4_STR))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipv6_full(b: &mut Bencher) { | ||
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_FULL))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipv6_compress(b: &mut Bencher) { | ||
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_COMPRESS))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipv6_v4(b: &mut Bencher) { | ||
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_V4))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipaddr_v4(b: &mut Bencher) { | ||
b.iter(|| IpAddr::from_str(black_box(IPV4_STR))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipaddr_v6_full(b: &mut Bencher) { | ||
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_FULL))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipaddr_v6_compress(b: &mut Bencher) { | ||
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_COMPRESS))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_ipaddr_v6_v4(b: &mut Bencher) { | ||
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_V4))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_socket_v4(b: &mut Bencher) { | ||
b.iter(|| SocketAddrV4::from_str(black_box(IPV4_STR_PORT))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_socket_v6(b: &mut Bencher) { | ||
b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_socket_v6_scope_id(b: &mut Bencher) { | ||
b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT_SCOPE_ID))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_socketaddr_v4(b: &mut Bencher) { | ||
b.iter(|| SocketAddr::from_str(black_box(IPV4_STR_PORT))); | ||
} | ||
|
||
#[bench] | ||
fn bench_parse_socketaddr_v6(b: &mut Bencher) { | ||
b.iter(|| SocketAddr::from_str(black_box(IPV6_STR_PORT))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod addr_parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters