Skip to content

Commit 96561a8

Browse files
committed
Auto merge of rust-lang#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
2 parents c7beecf + 69637c9 commit 96561a8

File tree

4 files changed

+131
-21
lines changed

4 files changed

+131
-21
lines changed

library/core/benches/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod char;
1616
mod fmt;
1717
mod hash;
1818
mod iter;
19+
mod net;
1920
mod num;
2021
mod ops;
2122
mod pattern;
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
2+
use core::str::FromStr;
3+
4+
use test::{black_box, Bencher};
5+
6+
const IPV4_STR: &str = "192.168.0.1";
7+
const IPV4_STR_PORT: &str = "192.168.0.1:8080";
8+
9+
const IPV6_STR_FULL: &str = "2001:db8:0:0:0:0:c0a8:1";
10+
const IPV6_STR_COMPRESS: &str = "2001:db8::c0a8:1";
11+
const IPV6_STR_V4: &str = "2001:db8::192.168.0.1";
12+
const IPV6_STR_PORT: &str = "[2001:db8::c0a8:1]:8080";
13+
const IPV6_STR_PORT_SCOPE_ID: &str = "[2001:db8::c0a8:1%1337]:8080";
14+
15+
#[bench]
16+
fn bench_parse_ipv4(b: &mut Bencher) {
17+
b.iter(|| Ipv4Addr::from_str(black_box(IPV4_STR)));
18+
}
19+
20+
#[bench]
21+
fn bench_parse_ipv6_full(b: &mut Bencher) {
22+
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_FULL)));
23+
}
24+
25+
#[bench]
26+
fn bench_parse_ipv6_compress(b: &mut Bencher) {
27+
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_COMPRESS)));
28+
}
29+
30+
#[bench]
31+
fn bench_parse_ipv6_v4(b: &mut Bencher) {
32+
b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_V4)));
33+
}
34+
35+
#[bench]
36+
fn bench_parse_ipaddr_v4(b: &mut Bencher) {
37+
b.iter(|| IpAddr::from_str(black_box(IPV4_STR)));
38+
}
39+
40+
#[bench]
41+
fn bench_parse_ipaddr_v6_full(b: &mut Bencher) {
42+
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_FULL)));
43+
}
44+
45+
#[bench]
46+
fn bench_parse_ipaddr_v6_compress(b: &mut Bencher) {
47+
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_COMPRESS)));
48+
}
49+
50+
#[bench]
51+
fn bench_parse_ipaddr_v6_v4(b: &mut Bencher) {
52+
b.iter(|| IpAddr::from_str(black_box(IPV6_STR_V4)));
53+
}
54+
55+
#[bench]
56+
fn bench_parse_socket_v4(b: &mut Bencher) {
57+
b.iter(|| SocketAddrV4::from_str(black_box(IPV4_STR_PORT)));
58+
}
59+
60+
#[bench]
61+
fn bench_parse_socket_v6(b: &mut Bencher) {
62+
b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT)));
63+
}
64+
65+
#[bench]
66+
fn bench_parse_socket_v6_scope_id(b: &mut Bencher) {
67+
b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT_SCOPE_ID)));
68+
}
69+
70+
#[bench]
71+
fn bench_parse_socketaddr_v4(b: &mut Bencher) {
72+
b.iter(|| SocketAddr::from_str(black_box(IPV4_STR_PORT)));
73+
}
74+
75+
#[bench]
76+
fn bench_parse_socketaddr_v6(b: &mut Bencher) {
77+
b.iter(|| SocketAddr::from_str(black_box(IPV6_STR_PORT)));
78+
}

library/core/benches/net/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod addr_parser;

library/core/src/net/parser.rs

+51-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module is "publicly exported" through the `FromStr` implementations
44
//! below.
55
6-
use crate::convert::TryInto;
6+
use crate::convert::{TryFrom, TryInto};
77
use crate::error::Error;
88
use crate::fmt;
99
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
@@ -104,36 +104,66 @@ impl<'a> Parser<'a> {
104104
// Read a number off the front of the input in the given radix, stopping
105105
// at the first non-digit character or eof. Fails if the number has more
106106
// digits than max_digits or if there is no number.
107-
fn read_number<T: ReadNumberHelper>(
107+
//
108+
// INVARIANT: `max_digits` must be less than the number of digits that `u32`
109+
// can represent.
110+
fn read_number<T: ReadNumberHelper + TryFrom<u32>>(
108111
&mut self,
109112
radix: u32,
110113
max_digits: Option<usize>,
111114
allow_zero_prefix: bool,
112115
) -> Option<T> {
113-
self.read_atomically(move |p| {
114-
let mut result = T::ZERO;
115-
let mut digit_count = 0;
116-
let has_leading_zero = p.peek_char() == Some('0');
117-
118-
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
119-
result = result.checked_mul(radix)?;
120-
result = result.checked_add(digit)?;
121-
digit_count += 1;
122-
if let Some(max_digits) = max_digits {
116+
// If max_digits.is_some(), then we are parsing a `u8` or `u16` and
117+
// don't need to use checked arithmetic since it fits within a `u32`.
118+
if let Some(max_digits) = max_digits {
119+
// u32::MAX = 4_294_967_295u32, which is 10 digits long.
120+
// `max_digits` must be less than 10 to not overflow a `u32`.
121+
debug_assert!(max_digits < 10);
122+
123+
self.read_atomically(move |p| {
124+
let mut result = 0_u32;
125+
let mut digit_count = 0;
126+
let has_leading_zero = p.peek_char() == Some('0');
127+
128+
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
129+
result *= radix;
130+
result += digit;
131+
digit_count += 1;
132+
123133
if digit_count > max_digits {
124134
return None;
125135
}
126136
}
127-
}
128137

129-
if digit_count == 0 {
130-
None
131-
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
132-
None
133-
} else {
134-
Some(result)
135-
}
136-
})
138+
if digit_count == 0 {
139+
None
140+
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
141+
None
142+
} else {
143+
result.try_into().ok()
144+
}
145+
})
146+
} else {
147+
self.read_atomically(move |p| {
148+
let mut result = T::ZERO;
149+
let mut digit_count = 0;
150+
let has_leading_zero = p.peek_char() == Some('0');
151+
152+
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
153+
result = result.checked_mul(radix)?;
154+
result = result.checked_add(digit)?;
155+
digit_count += 1;
156+
}
157+
158+
if digit_count == 0 {
159+
None
160+
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
161+
None
162+
} else {
163+
Some(result)
164+
}
165+
})
166+
}
137167
}
138168

139169
/// Read an IPv4 address.

0 commit comments

Comments
 (0)