Updates Benchmark macro parsing to use Generic Argument (#13919)

* WIP

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Removes POC code

* Adds assertion and UT

* adds runtime error

* removes const_assert

* ".git/.scripts/commands/fmt/fmt.sh"

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: command-bot <>
This commit is contained in:
gupnik
2023-04-14 22:54:26 +05:30
committed by GitHub
parent 8d06402e2e
commit cd1e51a4e7
4 changed files with 41 additions and 40 deletions
@@ -28,9 +28,9 @@ use syn::{
punctuated::Punctuated,
spanned::Spanned,
token::{Comma, Gt, Lt, PathSep},
Attribute, Error, Expr, ExprBlock, ExprCall, ExprPath, FnArg, Item, ItemFn, ItemMod, LitInt,
Pat, Path, PathArguments, PathSegment, Result, ReturnType, Signature, Stmt, Token, Type,
TypePath, Visibility, WhereClause,
Attribute, Error, Expr, ExprBlock, ExprCall, ExprPath, FnArg, Item, ItemFn, ItemMod, Pat, Path,
PathArguments, PathSegment, Result, ReturnType, Signature, Stmt, Token, Type, TypePath,
Visibility, WhereClause,
};
mod keywords {
@@ -54,17 +54,17 @@ mod keywords {
struct ParamDef {
name: String,
typ: Type,
start: u32,
end: u32,
start: syn::GenericArgument,
end: syn::GenericArgument,
}
/// Allows easy parsing of the `<10, 20>` component of `x: Linear<10, 20>`.
#[derive(Parse)]
struct RangeArgs {
_lt_token: Lt,
start: LitInt,
start: syn::GenericArgument,
_comma: Comma,
end: LitInt,
end: syn::GenericArgument,
_gt_token: Gt,
}
@@ -228,17 +228,8 @@ fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
let Some(segment) = tpath.path.segments.last() else { return invalid_param(typ.span()) };
let args = segment.arguments.to_token_stream().into();
let Ok(args) = syn::parse::<RangeArgs>(args) else { return invalid_param(typ.span()) };
let Ok(start) = args.start.base10_parse::<u32>() else { return invalid_param(args.start.span()) };
let Ok(end) = args.end.base10_parse::<u32>() else { return invalid_param(args.end.span()) };
if end < start {
return Err(Error::new(
args.start.span(),
"The start of a `ParamRange` must be less than or equal to the end",
))
}
params.push(ParamDef { name, typ: typ.clone(), start, end });
params.push(ParamDef { name, typ: typ.clone(), start: args.start, end: args.end });
}
Ok(params)
}
@@ -700,8 +691,8 @@ impl UnrolledParams {
.iter()
.map(|p| {
let name = Ident::new(&p.name, Span::call_site());
let start = p.start;
let end = p.end;
let start = &p.start;
let end = &p.end;
quote!(#name, #start, #end)
})
.collect();
@@ -987,6 +978,9 @@ fn expand_benchmark(
// Test the lowest, highest (if its different from the lowest)
// and up to num_values-2 more equidistant values in between.
// For 0..10 and num_values=6 this would mean: [0, 2, 4, 6, 8, 10]
if high < low {
return Err("The start of a `ParamRange` must be less than or equal to the end".into());
}
let mut values = #krate::vec![low];
let diff = (high - low).min(num_values - 1);
@@ -1,16 +0,0 @@
use frame_benchmarking::v2::*;
#[allow(unused_imports)]
use frame_support_test::Config;
#[benchmarks]
mod benches {
use super::*;
#[benchmark]
fn bench(x: Linear<3, 1>) {
#[block]
{}
}
}
fn main() {}
@@ -1,5 +0,0 @@
error: The start of a `ParamRange` must be less than or equal to the end
--> tests/benchmark_ui/bad_param_range.rs:10:21
|
10 | fn bench(x: Linear<3, 1>) {
| ^
@@ -0,0 +1,28 @@
use frame_benchmarking::v2::*;
use frame_support_test::Config;
use frame_support::parameter_types;
#[benchmarks]
mod benches {
use super::*;
const MY_CONST: u32 = 100;
const fn my_fn() -> u32 {
200
}
parameter_types! {
const MyConst: u32 = MY_CONST;
}
#[benchmark(skip_meta, extra)]
fn bench(a: Linear<{MY_CONST * 2}, {my_fn() + MyConst::get()}>) {
let a = 2 + 2;
#[block]
{}
assert_eq!(a, 4);
}
}
fn main() {}