Skip to content

Commit

Permalink
black_box benchmark inputs and outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Feb 13, 2025
1 parent 03197cb commit 46ba537
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
14 changes: 10 additions & 4 deletions benches/accumulator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion};
use salsa::Accumulator;

Expand Down Expand Up @@ -43,17 +45,21 @@ fn accumulator(criterion: &mut Criterion) {
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::new();
let input = Input::new(&db, 10_000);

let input = Input::new(black_box(&db), black_box(10_000));

// Pre-warm
let _ = root(&db, input);
let result = root(black_box(&db), black_box(input));
assert!(!black_box(result).is_empty());

(db, input)
},
|(db, input)| {
// Measure the cost of collecting accumulators ignoring the cost of running the
// query itself.
let diagnostics = root::accumulated::<Diagnostic>(db, *input);
let diagnostics = root::accumulated::<Diagnostic>(black_box(db), *black_box(input));

assert_eq!(diagnostics.len(), 1000);
assert_eq!(black_box(diagnostics).len(), 1000);
},
BatchSize::SmallInput,
);
Expand Down
49 changes: 32 additions & 17 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::hint::black_box;
use std::mem::transmute;

use codspeed_criterion_compat::{
Expand Down Expand Up @@ -41,16 +42,16 @@ fn mutating_inputs(c: &mut Criterion) {
let string = base_string.clone().repeat(*n);
let new_len = string.len();

let input = Input::new(&db, base_string.clone());
let input = Input::new(black_box(&db), black_box(base_string.clone()));
let actual_len = length(&db, input);
assert_eq!(base_len, actual_len);
assert_eq!(black_box(actual_len), base_len);

(db, input, string, new_len)
},
|&mut (ref mut db, input, ref string, new_len)| {
input.set_text(db).to(string.clone());
input.set_text(black_box(db)).to(black_box(string).clone());
let actual_len = length(db, input);
assert_eq!(new_len, actual_len);
assert_eq!(black_box(actual_len), new_len);
},
BatchSize::SmallInput,
)
Expand All @@ -70,13 +71,17 @@ fn inputs(c: &mut Criterion) {
|| {
let db = salsa::DatabaseImpl::default();
// Prepopulate ingredients.
let input = InternedInput::new(&db, "hello, world!".to_owned());
interned_length(&db, input);
let input =
InternedInput::new(black_box(&db), black_box("hello, world!".to_owned()));
let interned_len = interned_length(black_box(&db), black_box(input));
assert_eq!(black_box(interned_len), 13);
db
},
|db| {
let input: InternedInput = InternedInput::new(db, "hello, world!".to_owned());
interned_length(db, input);
let input =
InternedInput::new(black_box(db), black_box("hello, world!".to_owned()));
let interned_len = interned_length(black_box(db), black_box(input));
assert_eq!(black_box(interned_len), 13);
},
BatchSize::SmallInput,
)
Expand All @@ -89,11 +94,13 @@ fn inputs(c: &mut Criterion) {
// we can't pass this along otherwise, and the lifetime is generally informational
let input: InternedInput<'static> =
unsafe { transmute(InternedInput::new(&db, "hello, world!".to_owned())) };
let _ = interned_length(&db, input);
let interned_len = interned_length(black_box(&db), black_box(input));
assert_eq!(black_box(interned_len), 13);
(db, input)
},
|&mut (ref db, input)| {
interned_length(db, input);
let interned_len = interned_length(black_box(db), black_box(input));
assert_eq!(black_box(interned_len), 13);
},
BatchSize::SmallInput,
)
Expand All @@ -103,14 +110,18 @@ fn inputs(c: &mut Criterion) {
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();

// Prepopulate ingredients.
let input = Input::new(&db, "hello, world!".to_owned());
length(&db, input);
let input = Input::new(black_box(&db), black_box("hello, world!".to_owned()));
let len = length(black_box(&db), black_box(input));
assert_eq!(black_box(len), 13);

db
},
|db| {
let input = Input::new(db, "hello, world!".to_owned());
length(db, input);
let input = Input::new(black_box(db), black_box("hello, world!".to_owned()));
let len = length(black_box(db), black_box(input));
assert_eq!(black_box(len), 13);
},
BatchSize::SmallInput,
)
Expand All @@ -120,12 +131,16 @@ fn inputs(c: &mut Criterion) {
b.iter_batched_ref(
|| {
let db = salsa::DatabaseImpl::default();
let input = Input::new(&db, "hello, world!".to_owned());
let _ = length(&db, input);

let input = Input::new(black_box(&db), black_box("hello, world!".to_owned()));
let len = length(black_box(&db), black_box(input));
assert_eq!(black_box(len), 13);

(db, input)
},
|&mut (ref db, input)| {
length(db, input);
let len = length(black_box(db), black_box(input));
assert_eq!(black_box(len), 13);
},
BatchSize::SmallInput,
)
Expand Down
18 changes: 11 additions & 7 deletions benches/incremental.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::hint::black_box;

use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion};
use salsa::Setter;

Expand Down Expand Up @@ -28,22 +30,24 @@ fn many_tracked_structs(criterion: &mut Criterion) {
|| {
let db = salsa::DatabaseImpl::new();

let input = Input::new(&db, 1_000);
let input2 = Input::new(&db, 1);
let input = Input::new(black_box(&db), black_box(1_000));
let input2 = Input::new(black_box(&db), black_box(1));

// prewarm cache
let _ = root(&db, input);
let _ = root(&db, input2);
let root1 = root(black_box(&db), black_box(input));
assert_eq!(black_box(root1), 1_000);
let root2 = root(black_box(&db), black_box(input2));
assert_eq!(black_box(root2), 1);

(db, input, input2)
},
|(db, input, input2)| {
// Make a change, but fetch the result for the other input
input2.set_field(db).to(2);
input2.set_field(black_box(db)).to(black_box(2));

let result = root(db, *input);
let result = root(black_box(db), *black_box(input));

assert_eq!(result, 1_000);
assert_eq!(black_box(result), 1_000);
},
BatchSize::LargeInput,
);
Expand Down

0 comments on commit 46ba537

Please sign in to comment.