From 46ba537bbd41ef9582d4bae578dfd88496a9b632 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Thu, 13 Feb 2025 16:46:59 -0500 Subject: [PATCH] `black_box` benchmark inputs and outputs --- benches/accumulator.rs | 14 ++++++++---- benches/compare.rs | 49 +++++++++++++++++++++++++++--------------- benches/incremental.rs | 18 ++++++++++------ 3 files changed, 53 insertions(+), 28 deletions(-) diff --git a/benches/accumulator.rs b/benches/accumulator.rs index 16ca7f71..b1a461e8 100644 --- a/benches/accumulator.rs +++ b/benches/accumulator.rs @@ -1,3 +1,5 @@ +use std::hint::black_box; + use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; use salsa::Accumulator; @@ -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::(db, *input); + let diagnostics = root::accumulated::(black_box(db), *black_box(input)); - assert_eq!(diagnostics.len(), 1000); + assert_eq!(black_box(diagnostics).len(), 1000); }, BatchSize::SmallInput, ); diff --git a/benches/compare.rs b/benches/compare.rs index d2ed9e1b..2a2e5394 100644 --- a/benches/compare.rs +++ b/benches/compare.rs @@ -1,3 +1,4 @@ +use std::hint::black_box; use std::mem::transmute; use codspeed_criterion_compat::{ @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) @@ -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, ) diff --git a/benches/incremental.rs b/benches/incremental.rs index 5e5aa5f4..7fa99268 100644 --- a/benches/incremental.rs +++ b/benches/incremental.rs @@ -1,3 +1,5 @@ +use std::hint::black_box; + use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; use salsa::Setter; @@ -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, );