Skip to content

Commit 7c46b96

Browse files
committed
Benchmark hash functions on their own
Quickphf is the quickest of the three hash functions in this benchmark, then hashify, then phf. This is inconsistent with the previous benchmarks for `unescape()` and `unescape_attribute()`. hash/hashmap time: [22.337 ns 22.432 ns 22.548 ns] hash/hashify time: [13.932 ns 14.036 ns 14.150 ns] hash/phf time: [23.504 ns 23.601 ns 23.703 ns] hash/quickphf time: [14.353 ns 14.427 ns 14.520 ns] hash/matchgen time: [9.0193 ns 9.0741 ns 9.1346 ns] (The hashmap benchmark uses `std::collections::HashMap` for reference.)
1 parent 8f7b4e1 commit 7c46b96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+18560
-50
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ name = "unescape"
7373
harness = false
7474
required-features = ["bench", "_unescape_either"]
7575

76+
[[bench]]
77+
name = "hash"
78+
harness = false
79+
required-features = ["bench", "unescape", "unescape_phf", "unescape_quick", "unescape_fast"]
80+
7681
[lints]
7782
workspace = true
7883

benches/hash.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Benchmark hashing functions with [`criterion`].
2+
3+
#![allow(clippy::missing_docs_in_private_items, missing_docs)]
4+
5+
use criterion::{criterion_group, criterion_main, Criterion};
6+
use htmlize::unescape::internal::{Expander, Hashify, PhfMap, QuickPhf};
7+
use std::collections::HashMap;
8+
9+
// Include function to match entities at the start of an iterator. Used in
10+
// `match_entity()`.
11+
//
12+
// fn entity_matcher<'a, I>(iter: &mut I) -> Option<(bool, &'static [u8])>
13+
// where
14+
// I: Iterator<Item = &'a u8> + Clone,
15+
include!(concat!(env!("OUT_DIR"), "/matcher.rs"));
16+
17+
fn benchmarks(c: &mut Criterion) {
18+
let mut group = c.benchmark_group("hash");
19+
let input = &b"&timesbar;"[..];
20+
21+
let mut map = HashMap::new();
22+
for (&key, &value) in &htmlize::ENTITIES {
23+
map.insert(key, value);
24+
}
25+
26+
group.bench_with_input("hashmap", input, |b, input| {
27+
b.iter(|| map.get(&input));
28+
});
29+
30+
group.bench_with_input("hashify", input, |b, input| {
31+
b.iter(|| Hashify::expand(input));
32+
});
33+
34+
group.bench_with_input("phf", input, |b, input| {
35+
b.iter(|| PhfMap::expand(input));
36+
});
37+
38+
group.bench_with_input("quickphf", input, |b, input| {
39+
b.iter(|| QuickPhf::expand(input));
40+
});
41+
42+
group.bench_with_input("matchgen", input, |b, input| {
43+
b.iter(|| entity_matcher(input));
44+
});
45+
group.finish();
46+
}
47+
48+
criterion_group!(hash_group, benchmarks);
49+
criterion_main!(hash_group);

target/criterion/data/main/hash/hashify/benchmark.cbor

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

target/criterion/data/main/hash/hashmap/benchmark.cbor

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

target/criterion/data/main/hash/matchgen/benchmark.cbor

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

target/criterion/data/main/hash/phf/benchmark.cbor

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

0 commit comments

Comments
 (0)