Skip to content

feat(divan_compat): support types and mange types and args in codspeed uri #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ jobs:
- run: cargo codspeed build -p codspeed-bencher-compat
- run: cargo codspeed build --features async_futures -p codspeed-criterion-compat

- run: cargo codspeed build -p codspeed-divan-compat
- run: cargo codspeed build -p codspeed-divan-compat-examples

- name: Run the benchmarks
uses: CodSpeedHQ/action@main
with:
Expand All @@ -69,6 +72,7 @@ jobs:
- run: cargo install --path crates/cargo-codspeed --locked

- run: cargo codspeed build -p codspeed-divan-compat
- run: cargo codspeed build -p codspeed-divan-compat-examples

- name: Run the benchmarks
uses: CodSpeedHQ/action@main
Expand Down
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ members = [
resolver = "2"

[workspace.dependencies]
itertools = "0.14.0"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
2 changes: 1 addition & 1 deletion crates/cargo-codspeed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cargo_metadata = "0.19.1"
clap = { version = "=4.5.17", features = ["derive", "env"] }
termcolor = "1.4"
anyhow = "1.0.86"
itertools = "0.13.0"
itertools = { workspace = true }
anstyle = "1.0.8"
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/divan_compat/benches/basic_example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use codspeed_divan_compat::Bencher;

fn fibo(n: i32) -> i32 {
let mut a = 0;
let mut b = 1;
Expand All @@ -21,6 +23,15 @@ fn fibo_10() -> i32 {
codspeed_divan_compat::black_box(fibo(10))
}

#[codspeed_divan_compat::bench]
fn mut_borrow(bencher: Bencher) {
let mut bytes = Vec::<i32>::new();

bencher.bench_local(|| {
bytes.push(42);
});
}

fn main() {
codspeed_divan_compat::main();
}
122 changes: 89 additions & 33 deletions crates/divan_compat/divan_fork/src/divan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,39 +310,11 @@ impl Divan {

if should_compute_stats {
let stats = bench_context.compute_stats();
{
// WARNING: Keep in sync with `codspeed-divan-compat::uri::generate`
// Not worth doing the work of actually using the same code since this fork
// is temporary
let name = bench_entry.display_name().to_string();
let file = bench_entry.meta().location.file;
let mut module_path = bench_entry
.meta()
.module_path_components()
.skip(1)
.collect::<Vec<_>>()
.join("::");
if !module_path.is_empty() {
module_path.push_str("::");
}
let uri = format!("{file}::{module_path}{name}");
let iter_per_round = bench_context.samples.sample_size;
let times_ns: Vec<_> = bench_context
.samples
.time_samples
.iter()
.map(|s| s.duration.picos / 1_000)
.collect();
let max_time_ns = options.max_time.map(|t| t.as_nanos());
::codspeed::walltime::collect_raw_walltime_results(
"divan",
name,
uri,
iter_per_round,
max_time_ns,
times_ns,
);
};
codspeed::collect_walltime_results(
&bench_context,
&bench_entry,
bench_display_name,
);
tree_painter.borrow_mut().finish_leaf(
is_last_thread_count,
&stats,
Expand Down Expand Up @@ -383,6 +355,90 @@ impl Divan {
}
}

mod codspeed {
use crate::bench::BenchContext;
use crate::entry::AnyBenchEntry;

pub(crate) fn collect_walltime_results(
bench_context: &BenchContext,
bench_entry: &AnyBenchEntry,
closure_bench_display_name: &str,
) {
// WARNING: Keep URI generation in sync with `codspeed-divan-compat::uri::generate`
// Not worth doing the work of actually using the same code since this fork is temporary
let (bench_name, uri) = {
let bench_function_name = bench_entry.meta().display_name;

let (bench_type_name, bench_arg_name) = {
let bench_function_or_type_name = bench_entry.display_name().to_string();

let type_name = if bench_function_or_type_name == bench_function_name {
None
} else {
Some(bench_function_or_type_name)
};

let arg_name = match type_name.as_ref() {
None => {
if closure_bench_display_name == bench_function_name {
None
} else {
Some(closure_bench_display_name)
}
}
Some(type_name) => {
if closure_bench_display_name == type_name {
None
} else {
Some(closure_bench_display_name)
}
}
};

(type_name, arg_name)
};

let mut bench_name = bench_function_name.to_string();

match (bench_type_name, bench_arg_name) {
(None, None) => {}
(Some(type_name), None) => {
bench_name.push_str(format!("[{type_name}]").as_str());
}
(None, Some(arg_name)) => {
bench_name.push_str(format!("[{arg_name}]").as_str());
}
(Some(type_name), Some(arg_name)) => {
bench_name.push_str(format!("[{type_name}, {arg_name}]").as_str());
}
}

let file = bench_entry.meta().location.file;
let mut module_path =
bench_entry.meta().module_path_components().skip(1).collect::<Vec<_>>().join("::");
if !module_path.is_empty() {
module_path.push_str("::");
}
let uri = format!("{file}::{module_path}{bench_name}");
(bench_name, uri)
};

let iter_per_round = bench_context.samples.sample_size;
let times_ns: Vec<_> =
bench_context.samples.time_samples.iter().map(|s| s.duration.picos / 1_000).collect();
let max_time_ns = bench_context.options.max_time.map(|t| t.as_nanos());

::codspeed::walltime::collect_raw_walltime_results(
"divan",
bench_name,
uri,
iter_per_round,
max_time_ns,
times_ns,
);
}
}

/// Makes `Divan::skip_regex` input polymorphic.
pub trait SkipRegex {
fn skip_regex(self, divan: &mut Divan);
Expand Down
3 changes: 1 addition & 2 deletions crates/divan_compat/examples/benches/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn sub() -> i32 {
black_box(2) - black_box(1)
}

#[divan::bench]
#[divan::bench(max_time = 1)]
fn mul() -> i32 {
black_box(2) * black_box(1)
}
Expand Down Expand Up @@ -91,7 +91,6 @@ mod fibonacci {

// Will be ignored in instrumented mode as we do not support type generics yet
// O(n)
#[cfg(not(codspeed))]
#[divan::bench(
types = [BTreeMap<u64, u64>, HashMap<u64, u64>],
args = VALUES,
Expand Down
2 changes: 2 additions & 0 deletions crates/divan_compat/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ proc-macro = true

[dependencies]
divan-macros = { version = "=0.1.17" }
itertools = { workspace = true }
proc-macro-crate = "3.2.0"
proc-macro2 = "1"
quote = { version = "1", default-features = false }
Expand All @@ -32,4 +33,5 @@ syn = { version = "^2.0.18", default-features = false, features = [
"parsing",
"printing",
"proc-macro",
"extra-traits",
] }
107 changes: 107 additions & 0 deletions crates/divan_compat/macros/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use itertools::Itertools;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, Parser},
Expr, Meta, MetaNameValue, Token, Type,
};

/// Values from parsed options shared between `#[divan::bench]` and
/// `#[divan::bench_group]`.
///
/// The `crate` option is not included because it is only needed to get proper
/// access to `__private`.
#[derive(Default)]
pub(crate) struct AttrOptions {
pub(crate) types: Option<GenericTypes>,
pub(crate) crate_: bool,
pub(crate) other_args: Vec<Meta>,
}

#[allow(unreachable_code)]
impl AttrOptions {
pub fn parse(tokens: TokenStream) -> Result<Self, TokenStream> {
let mut attr_options = Self::default();

let attr_parser = syn::meta::parser(|meta| {
let Some(ident) = meta.path.get_ident() else {
return Err(meta.error("Unexpected attribute"));
};

let ident_name = ident.to_string();
let ident_name = ident_name.strip_prefix("r#").unwrap_or(&ident_name);

match ident_name {
// Divan accepts type syntax that is not parseable into syn::Meta out of the box,
// so we parse and rebuild the arguments manually.
"types" => {
attr_options.types = Some(meta.value()?.parse()?);
}
"crate" => {
attr_options.crate_ = true;
meta.value()?.parse::<Expr>()?; // Discard the value
}
"min_time" | "max_time" | "sample_size" | "sample_count" | "skip_ext_time" => {
// These arguments are ignored for codspeed runs
meta.value()?.parse::<Expr>()?; // Discard the value
}
_ => {
let path = meta.path.clone();
let parsed_meta = if meta.input.is_empty() {
Meta::Path(path)
} else {
let value: syn::Expr = meta.value()?.parse()?;
Meta::NameValue(MetaNameValue {
path,
eq_token: Default::default(),
value: Expr::Verbatim(value.into_token_stream()),
})
};

attr_options.other_args.push(parsed_meta);
}
}

Ok(())
});

match attr_parser.parse(tokens) {
Ok(()) => {}
Err(error) => return Err(error.into_compile_error().into()),
}

Ok(attr_options)
}
}

/// Generic types over which to instantiate benchmark functions.
pub(crate) enum GenericTypes {
/// List of types, e.g. `[i32, String, ()]`.
List(Vec<proc_macro2::TokenStream>),
}

impl Parse for GenericTypes {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let content;
syn::bracketed!(content in input);

Ok(Self::List(
content
.parse_terminated(Type::parse, Token![,])?
.into_iter()
.map(|ty| ty.into_token_stream())
.collect(),
))
}
}

impl ToTokens for GenericTypes {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
Self::List(list) => {
let type_tokens = list.iter().cloned().map_into::<proc_macro2::TokenStream>();
tokens.extend(quote! { [ #(#type_tokens),* ] });
}
}
}
}
Loading