From cd1e51a4e72adbd21b9b1d399701a9935ed90709 Mon Sep 17 00:00:00 2001 From: gupnik Date: Fri, 14 Apr 2023 22:54:26 +0530 Subject: [PATCH] Updates Benchmark macro parsing to use Generic Argument (#13919) * WIP Signed-off-by: Oliver Tale-Yazdi * 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 Co-authored-by: Oliver Tale-Yazdi Co-authored-by: command-bot <> --- .../frame/support/procedural/src/benchmark.rs | 32 ++++++++----------- .../tests/benchmark_ui/bad_param_range.rs | 16 ---------- .../tests/benchmark_ui/bad_param_range.stderr | 5 --- .../benchmark_ui/pass/valid_const_expr.rs | 28 ++++++++++++++++ 4 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 substrate/frame/support/test/tests/benchmark_ui/bad_param_range.rs delete mode 100644 substrate/frame/support/test/tests/benchmark_ui/bad_param_range.stderr create mode 100644 substrate/frame/support/test/tests/benchmark_ui/pass/valid_const_expr.rs diff --git a/substrate/frame/support/procedural/src/benchmark.rs b/substrate/frame/support/procedural/src/benchmark.rs index cf091e7cb0..315a6000bc 100644 --- a/substrate/frame/support/procedural/src/benchmark.rs +++ b/substrate/frame/support/procedural/src/benchmark.rs @@ -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> { 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::(args) else { return invalid_param(typ.span()) }; - let Ok(start) = args.start.base10_parse::() else { return invalid_param(args.start.span()) }; - let Ok(end) = args.end.base10_parse::() 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); diff --git a/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.rs b/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.rs deleted file mode 100644 index 993f2a0004..0000000000 --- a/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.rs +++ /dev/null @@ -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() {} diff --git a/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.stderr b/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.stderr deleted file mode 100644 index 1347af0a07..0000000000 --- a/substrate/frame/support/test/tests/benchmark_ui/bad_param_range.stderr +++ /dev/null @@ -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>) { - | ^ diff --git a/substrate/frame/support/test/tests/benchmark_ui/pass/valid_const_expr.rs b/substrate/frame/support/test/tests/benchmark_ui/pass/valid_const_expr.rs new file mode 100644 index 0000000000..bead3bf277 --- /dev/null +++ b/substrate/frame/support/test/tests/benchmark_ui/pass/valid_const_expr.rs @@ -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() {}