Hide benchmarks behind a feature flag (#5024)

* Hide benchmarks behind a feature flag

* Propage attributes in impl_runtime_apis macro

* Bump impl_version

* Fillter cfg attributes

* Hide more things under the feature

* Fix set_block_number availability

* Rename filter_attrs -> filter_cfg_attrs

* Rename runtime_benchmarks to runtime-benchmarks
This commit is contained in:
Benjamin Kampmann
2020-03-05 10:40:55 +01:00
committed by GitHub
parent 8bf855b09d
commit ff2a36d7cb
11 changed files with 68 additions and 16 deletions
+2 -1
View File
@@ -35,7 +35,7 @@ sp-version = { version = "2.0.0-alpha.2", default-features = false, path = "../.
# frame dependencies
frame-executive = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/executive" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/benchmarking" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/benchmarking", optional = true }
frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/support" }
frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/system" }
frame-system-rpc-runtime-api = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/system/rpc/runtime-api/" }
@@ -132,3 +132,4 @@ std = [
"pallet-recovery/std",
"pallet-vesting/std",
]
runtime-benchmarks = ["frame-benchmarking"]
+2 -1
View File
@@ -83,7 +83,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 227,
impl_version: 0,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
};
@@ -818,6 +818,7 @@ impl_runtime_apis! {
}
}
#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
module: Vec<u8>,
+2 -1
View File
@@ -14,7 +14,7 @@ codec = { package = "parity-scale-codec", version = "1.2.0", default-features =
sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" }
@@ -34,3 +34,4 @@ std = [
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = ["frame-benchmarking"]
+1
View File
@@ -155,6 +155,7 @@ mod tests_composite;
#[cfg(test)]
#[macro_use]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use sp_std::prelude::*;
+2 -1
View File
@@ -15,7 +15,7 @@ enumflags2 = { version = "0.6.2" }
sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" }
@@ -35,3 +35,4 @@ std = [
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = ["frame-benchmarking"]
+1
View File
@@ -78,6 +78,7 @@ use frame_support::{
};
use frame_system::{self as system, ensure_signed, ensure_root};
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
+1
View File
@@ -36,6 +36,7 @@ std = [
"sp-runtime/std",
"sp-version/std",
]
runtime-benchmarks = []
[[bench]]
name = "bench"
+1 -1
View File
@@ -885,7 +885,7 @@ impl<T: Trait> Module<T> {
/// Set the block number to something in particular. Can be used as an alternative to
/// `initialize` for tests that don't need to bother with the other environment entries.
#[cfg(any(feature = "std", test))]
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn set_block_number(n: T::BlockNumber) {
<Number<T>>::put(n);
}
+3 -2
View File
@@ -14,10 +14,10 @@ documentation = "https://docs.rs/pallet-timestamp"
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.2.0", default-features = false, features = ["derive"] }
sp-std = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io" }
sp-io = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/io", optional = true }
sp-runtime = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/runtime" }
sp-inherents = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/inherents" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking" }
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "2.0.0-alpha.2", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-alpha.2", default-features = false, path = "../system" }
sp-timestamp = { version = "2.0.0-alpha.2", default-features = false, path = "../../primitives/timestamp" }
@@ -40,3 +40,4 @@ std = [
"frame-system/std",
"sp-timestamp/std"
]
runtime-benchmarks = ["frame-benchmarking", "sp-io"]
+1
View File
@@ -90,6 +90,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
use sp_std::{result, cmp};
@@ -27,7 +27,7 @@ use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature,
spanned::Spanned, parse_macro_input, Ident, Type, ItemImpl, Path, Signature, Attribute,
ImplItem, parse::{Parse, ParseStream, Result, Error}, PathArguments, GenericArgument, TypePath,
fold::{self, Fold}, parse_quote,
};
@@ -141,7 +141,7 @@ fn extract_runtime_block_ident(trait_: &Path) -> Result<&TypePath> {
fn generate_impl_calls(
impls: &[ItemImpl],
input: &Ident
) -> Result<Vec<(Ident, Ident, TokenStream)>> {
) -> Result<Vec<(Ident, Ident, TokenStream, Vec<Attribute>)>> {
let mut impl_calls = Vec::new();
for impl_ in impls {
@@ -162,9 +162,12 @@ fn generate_impl_calls(
&impl_trait
)?;
impl_calls.push(
(impl_trait_ident.clone(), method.sig.ident.clone(), impl_call)
);
impl_calls.push((
impl_trait_ident.clone(),
method.sig.ident.clone(),
impl_call,
filter_cfg_attrs(&impl_.attrs),
));
}
}
}
@@ -178,9 +181,12 @@ fn generate_dispatch_function(impls: &[ItemImpl]) -> Result<TokenStream> {
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &data)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
.map(|(trait_, fn_name, impl_, attrs)| {
let name = prefix_function_with_trait(&trait_, &fn_name);
quote!( #name => Some(#c::Encode::encode(&{ #impl_ })), )
quote!(
#( #attrs )*
#name => Some(#c::Encode::encode(&{ #impl_ })),
)
});
Ok(quote!(
@@ -200,13 +206,14 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result<TokenStream> {
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &input)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
.map(|(trait_, fn_name, impl_, attrs)| {
let fn_name = Ident::new(
&prefix_function_with_trait(&trait_, &fn_name),
Span::call_site()
);
quote!(
#( #attrs )*
#[cfg(not(feature = "std"))]
#[no_mangle]
pub fn #fn_name(input_data: *mut u8, input_len: usize) -> u64 {
@@ -447,6 +454,7 @@ fn generate_api_impl_for_runtime(impls: &[ItemImpl]) -> Result<TokenStream> {
let trait_ = extend_with_runtime_decl_path(trait_);
impl_.trait_.as_mut().unwrap().1 = trait_;
impl_.attrs = filter_cfg_attrs(&impl_.attrs);
impls_prepared.push(impl_);
}
@@ -622,6 +630,8 @@ impl<'a> Fold for ApiRuntimeImplToApiRuntimeApiImpl<'a> {
}
);
input.attrs = filter_cfg_attrs(&input.attrs);
// The implementation for the `RuntimeApiImpl` is only required when compiling with
// the feature `std` or `test`.
input.attrs.push(parse_quote!( #[cfg(any(feature = "std", test))] ));
@@ -695,8 +705,12 @@ fn generate_runtime_api_versions(impls: &[ItemImpl]) -> Result<TokenStream> {
let id: Path = parse_quote!( #path ID );
let version: Path = parse_quote!( #path VERSION );
let attrs = filter_cfg_attrs(&impl_.attrs);
result.push(quote!( (#id, #version) ));
result.push(quote!(
#( #attrs )*
(#id, #version)
));
}
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
@@ -745,3 +759,32 @@ fn impl_runtime_apis_impl_inner(api_impls: &[ItemImpl]) -> Result<TokenStream> {
)
)
}
// Filters all attributes except the cfg ones.
fn filter_cfg_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
attrs.into_iter().filter(|a| a.path.is_ident("cfg")).cloned().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filter_non_cfg_attributes() {
let cfg_std: Attribute = parse_quote!(#[cfg(feature = "std")]);
let cfg_benchmarks: Attribute = parse_quote!(#[cfg(feature = "runtime-benchmarks")]);
let attrs = vec![
cfg_std.clone(),
parse_quote!(#[derive(Debug)]),
parse_quote!(#[test]),
cfg_benchmarks.clone(),
parse_quote!(#[allow(non_camel_case_types)]),
];
let filtered = filter_cfg_attrs(&attrs);
assert_eq!(filtered.len(), 2);
assert_eq!(cfg_std, filtered[0]);
assert_eq!(cfg_benchmarks, filtered[1]);
}
}