Skip to content

Commit d6c9467

Browse files
committed
Optimize Table::allows_code_point
1 parent 2846702 commit d6c9467

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

bench/benches/bench.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ criterion_group!(
1313
bench_parse_uri,
1414
bench_parse_iri,
1515
bench_build,
16-
bench_normalize,
16+
bench_normalize_uri,
17+
bench_normalize_iri,
1718
bench_resolve,
1819
bench_top100,
1920
);
2021
criterion_main!(benches);
2122

2223
const PARSE_URI_CASE: &str = "https://user@example.com/search?q=%E6%B5%8B%E8%AF%95#fragment";
2324
const PARSE_IRI_CASE: &str = "https://用户@测试.com/search?q=我们测试解析IRI#fragment";
24-
const NORMALIZE_CASE: &str = "eXAMPLE://a/./b/../b/%63/%7bfoo%7d";
25+
const NORMALIZE_URI_CASE: &str = "eXAMPLE://a/./b/../b/%63/%7bfoo%7d";
26+
const NORMALIZE_IRI_CASE: &str = "https://%E7%94%A8%E6%88%B7@%E6%B5%8B%E8%AF%95.com/search?q=%E6%88%91%E4%BB%AC%E6%B5%8B%E8%AF%95%E8%A7%A3%E6%9E%90IRI#fragment";
2527
const RESOLVE_CASE_BASE: &str = "http://example.com/foo/bar/baz/quz";
2628
const RESOLVE_CASE_REF: &str = "../../../qux/./quux/../corge";
2729

@@ -89,11 +91,23 @@ fn bench_build(c: &mut Criterion) {
8991
group.finish();
9092
}
9193

92-
fn bench_normalize(c: &mut Criterion) {
93-
let r_fluent = Uri::parse(NORMALIZE_CASE).unwrap();
94-
let r_iri = UriStr::new(NORMALIZE_CASE).unwrap();
94+
fn bench_normalize_uri(c: &mut Criterion) {
95+
let r_fluent = Uri::parse(NORMALIZE_URI_CASE).unwrap();
96+
let r_iri = UriStr::new(NORMALIZE_URI_CASE).unwrap();
9597

96-
let mut group = c.benchmark_group("normalize");
98+
let mut group = c.benchmark_group("normalize-uri");
99+
group.bench_function("fluent-uri", |b| b.iter(|| r_fluent.normalize()));
100+
group.bench_function("iri-string", |b| {
101+
b.iter(|| r_iri.normalize().to_dedicated_string())
102+
});
103+
group.finish();
104+
}
105+
106+
fn bench_normalize_iri(c: &mut Criterion) {
107+
let r_fluent = Iri::parse(NORMALIZE_IRI_CASE).unwrap();
108+
let r_iri = IriStr::new(NORMALIZE_IRI_CASE).unwrap();
109+
110+
let mut group = c.benchmark_group("normalize-iri");
97111
group.bench_function("fluent-uri", |b| b.iter(|| r_fluent.normalize()));
98112
group.bench_function("iri-string", |b| {
99113
b.iter(|| r_iri.normalize().to_dedicated_string())

bench/result.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ parse-uri/fluent-uri time: [54.553 ns 54.789 ns 55.067 ns]
44
parse-uri/iref time: [109.68 ns 110.15 ns 110.64 ns]
55
parse-uri/iri-string time: [137.27 ns 138.20 ns 139.19 ns]
66

7-
parse-iri/fluent-uri time: [73.795 ns 74.449 ns 75.264 ns]
8-
parse-iri/iref time: [130.43 ns 131.37 ns 132.46 ns]
9-
parse-iri/iri-string time: [156.71 ns 158.12 ns 159.70 ns]
10-
parse-iri/oxiri time: [116.23 ns 117.85 ns 119.68 ns]
7+
parse-iri/fluent-uri time: [65.470 ns 65.827 ns 66.212 ns]
8+
parse-iri/iref time: [126.40 ns 127.42 ns 128.59 ns]
9+
parse-iri/iri-string time: [149.28 ns 150.21 ns 151.20 ns]
10+
parse-iri/oxiri time: [107.25 ns 107.73 ns 108.27 ns]
1111

1212
build/fluent-uri time: [199.80 ns 201.46 ns 203.59 ns]
1313
build/iri-string time: [378.34 ns 381.28 ns 384.42 ns]
@@ -23,11 +23,11 @@ parse-top100/iri-string time: [19.908 ms 20.010 ms 20.118 ms]
2323
parse-top100/iref time: [22.702 ms 22.790 ms 22.885 ms]
2424

2525
parse-iri-top100/fluent-uri
26-
time: [9.8868 ms 10.029 ms 10.221 ms]
26+
time: [9.6033 ms 9.6497 ms 9.7035 ms]
2727
parse-iri-top100/iri-string
28-
time: [19.857 ms 19.970 ms 20.093 ms]
29-
parse-iri-top100/iref time: [32.623 ms 32.762 ms 32.912 ms]
30-
parse-iri-top100/oxiri time: [28.590 ms 28.871 ms 29.196 ms]
28+
time: [19.762 ms 20.074 ms 20.457 ms]
29+
parse-iri-top100/iref time: [31.735 ms 31.863 ms 32.005 ms]
30+
parse-iri-top100/oxiri time: [27.149 ms 27.287 ms 27.438 ms]
3131

3232
parse-normalize-top100/fluent-uri
3333
time: [24.766 ms 24.897 ms 25.039 ms]

src/pct_enc/table.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ impl Table {
112112
self.0 & (MASK_UCSCHAR | MASK_IPRIVATE) != 0
113113
}
114114

115+
#[inline]
115116
pub(crate) const fn allows_code_point(self, x: u32) -> bool {
116117
if x < 128 {
117-
let table = if x < 64 {
118-
self.0 & MASK_UNENCODED_ASCII
119-
} else {
120-
self.1
121-
};
122-
table & 1u64.wrapping_shl(x) != 0
123-
} else {
124-
(self.0 & MASK_UCSCHAR != 0 && is_ucschar(x))
125-
|| (self.0 & MASK_IPRIVATE != 0 && is_iprivate(x))
118+
return self.allows_ascii(x as u8);
119+
}
120+
if self.0 & MASK_UCSCHAR != 0 && is_ucschar(x) {
121+
return true;
122+
}
123+
if self.0 & MASK_IPRIVATE != 0 && is_iprivate(x) {
124+
return true;
126125
}
126+
false
127127
}
128128

129129
/// Checks whether the given unencoded character is allowed by the table.

0 commit comments

Comments
 (0)