Skip to content

Commit

Permalink
test: Improve criterion benchmark use of black_box
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Dec 23, 2023
1 parent 79a15d7 commit 617b25e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ jobs:
benchmark:
name: ⏱️ Benchmark
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- run: |
rustup update
rustup toolchain install nightly
Expand Down
48 changes: 16 additions & 32 deletions benches/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ fn trie_benchmark(c: &mut Criterion) {

c.bench_function("trie_mismatch", |b| {
let mut t = ptrie::Trie::new();
t.insert("test".bytes(), String::from("test"));
t.insert(black_box("test".bytes()), black_box(String::from("test")));
b.iter(|| {
assert!(!t.contains_key("tst".bytes()));
assert!(!t.contains_key(black_box("tst".bytes())));
});
});

c.bench_function("trie_massive_match", |b| {
let mut t = ptrie::Trie::new();
let keys = generate_keys();
for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}
b.iter(|| {
for key in &keys {
assert!(t.contains_key(key.bytes()));
assert!(t.contains_key(black_box(key.bytes())));
}
});
});
Expand All @@ -50,14 +50,12 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let mismatching = String::from("0999");
let keys = generate_keys();

for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}

b.iter(|| {
for _ in 0..keys.len() {
assert!(!t.contains_key(mismatching.bytes()));
assert!(!t.contains_key(black_box(mismatching.bytes())));
}
});
});
Expand All @@ -66,11 +64,9 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let mismatching = String::from("9099");
let keys = generate_keys();

for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}

b.iter(|| {
for _ in 0..keys.len() {
assert!(!t.contains_key(mismatching.bytes()));
Expand All @@ -82,14 +78,12 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let mismatching = String::from("9909");
let keys = generate_keys();

for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}

b.iter(|| {
for _ in 0..keys.len() {
assert!(!t.contains_key(mismatching.bytes()));
assert!(!t.contains_key(black_box(mismatching.bytes())));
}
});
});
Expand All @@ -103,7 +97,7 @@ fn trie_benchmark(c: &mut Criterion) {
}
b.iter(|| {
for _ in 0..keys.len() {
assert!(!t.contains_key(mismatching.bytes()));
assert!(!t.contains_key(black_box(mismatching.bytes())));
}
});
});
Expand All @@ -112,11 +106,11 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let keys = generate_keys();
for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}
b.iter(|| {
for key in &keys {
assert!(!t.find_prefixes(key.bytes()).is_empty());
assert!(!t.find_prefixes(black_box(key.bytes())).is_empty());
}
});
});
Expand All @@ -125,11 +119,11 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let keys = generate_keys();
for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}
b.iter(|| {
for key in &keys {
assert!(!t.find_postfixes(key.bytes()).is_empty());
assert!(!t.find_postfixes(black_box(key.bytes())).is_empty());
}
});
});
Expand All @@ -138,11 +132,11 @@ fn trie_benchmark(c: &mut Criterion) {
let mut t = ptrie::Trie::new();
let keys = generate_keys();
for key in &keys {
t.insert(key.bytes(), key.clone());
t.insert(black_box(key.bytes()), black_box(key.clone()));
}
b.iter(|| {
for key in &keys {
assert!(t.find_longest_prefix(key.bytes()).is_some());
assert!(t.find_longest_prefix(black_box(key.bytes())).is_some());
}
});
});
Expand All @@ -152,9 +146,7 @@ fn hashmap_benchmark(c: &mut Criterion) {
c.bench_function("hashmap_match", |b| {
let mut h = HashMap::new();
let key = String::from("test");

h.insert(key.clone(), true);

b.iter(|| {
h.get(&key);
});
Expand All @@ -164,9 +156,7 @@ fn hashmap_benchmark(c: &mut Criterion) {
let mut h = HashMap::new();
let key = String::from("test");
let notkey = String::from("tst");

h.insert(key, true);

b.iter(|| {
h.get(&notkey);
});
Expand All @@ -175,11 +165,9 @@ fn hashmap_benchmark(c: &mut Criterion) {
c.bench_function("hashmap_massive_match", |b| {
let mut h = HashMap::new();
let keys = generate_keys();

for key in &keys {
h.insert(key.clone(), key.clone());
}

b.iter(|| {
for key in &keys {
assert!(h.contains_key(key));
Expand All @@ -191,11 +179,9 @@ fn hashmap_benchmark(c: &mut Criterion) {
let mut h = HashMap::new();
let mismatching = String::from("0999");
let keys = generate_keys();

for key in &keys {
h.insert(key.clone(), key.clone());
}

b.iter(|| {
for _ in 0..keys.len() {
assert!(!h.contains_key(&mismatching));
Expand All @@ -207,11 +193,9 @@ fn hashmap_benchmark(c: &mut Criterion) {
let mut h = HashMap::new();
let mismatching = String::from("0");
let keys = generate_keys();

for key in &keys {
h.insert(key.clone(), key.clone());
}

b.iter(|| {
for _ in 0..keys.len() {
assert!(!h.contains_key(&mismatching));
Expand Down

0 comments on commit 617b25e

Please sign in to comment.