Skip to content
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

Contracts: Only exec parsed code in benchmarks #3915

Merged
merged 90 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
a3ddd5e
Update benchmarking
pgherveou Apr 2, 2024
db22ac0
Fix tests
pgherveou Apr 2, 2024
d3a6ccb
PR doc
pgherveou Apr 2, 2024
03ba18e
fix staking benchmark
pgherveou Apr 2, 2024
31e7f4c
fix prdoc
pgherveou Apr 2, 2024
7b4d0c8
Add on_before_start closure
pgherveou Apr 2, 2024
a560470
Fix fix
pgherveou Apr 2, 2024
c92091b
fix comment
pgherveou Apr 2, 2024
d701524
fix
pgherveou Apr 2, 2024
f311681
fix
pgherveou Apr 2, 2024
e52c535
PR comment
pgherveou Apr 3, 2024
f8f14ce
nit
pgherveou Apr 3, 2024
9bf84f8
fix bug
pgherveou Apr 3, 2024
c98c102
Merge branch 'master' into pg/bench_update
pgherveou Apr 3, 2024
cd46b3f
nit
pgherveou Apr 3, 2024
7623c2a
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Apr 3, 2024
aa523d6
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
57d6d62
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
86e8349
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
b8f79bb
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
c07f03e
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
bbd3969
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
fafc4f7
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
81b98f9
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
6af2ae4
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
e0cd224
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
ae2ad48
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
9fe72a1
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
30b417c
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
b73ce1a
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
bef9679
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
9a8c341
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
2aa4306
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
f2d6a65
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
f55563a
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
183537e
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
141982f
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
50c12d2
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
a16e8b5
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
e39d75d
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
1f22c66
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
5da51b2
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
23e41ef
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
a3ed19c
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
c11f98b
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
ab93a87
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
5aa62a8
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
a513333
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
b94810f
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
93b1050
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
a4472d7
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
6ee198d
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
038ab82
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
e3ce75b
fixes
pgherveou Apr 3, 2024
b85041b
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
3368aae
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
dd3406e
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
b9459bb
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
2172ac9
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
7f5c8bb
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
2ceaa1a
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
3c1ff83
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
96f8123
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
a7a0f64
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
3450dc9
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
5209cd7
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
d15781f
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
5d57d63
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
7d2cdf4
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
8448264
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 3, 2024
c1da25e
wip
pgherveou Apr 2, 2024
746e7be
Remove hack
pgherveou Apr 2, 2024
f12749d
wip
pgherveou Apr 3, 2024
d65baf6
fix
pgherveou Apr 4, 2024
a76530b
wip
pgherveou Apr 4, 2024
e045993
clippy
pgherveou Apr 4, 2024
990d06b
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 4, 2024
dc07afc
wip
pgherveou Apr 4, 2024
47430dd
fix up
pgherveou Apr 4, 2024
ad1f5c0
fixes
pgherveou Apr 4, 2024
5367d4d
nit comment
pgherveou Apr 4, 2024
a83ed58
nit
pgherveou Apr 4, 2024
5eec31f
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 4, 2024
9dfb05e
fix tabs
pgherveou Apr 8, 2024
4ca115f
Add PR doc
pgherveou Apr 8, 2024
54f7a6b
Add comment
pgherveou Apr 9, 2024
e4f2389
update constant
pgherveou Apr 9, 2024
1890a66
Merge branch 'master' into pg/bench_tweaks
pgherveou Apr 10, 2024
67cdfbe
reset weights changes
pgherveou Apr 10, 2024
4912fe0
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 10, 2024
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
14 changes: 14 additions & 0 deletions prdoc/pr_3915.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "[pallet-contracts] Weights update"

doc:
- audience: Runtime Dev
description: |
Update Host functions benchmarks, instead of benchmarking the whole call extrinsic, this PR solely benchmark the execution of the Host function.
Previously, some benchmarks would overestimate the weight as both the parsing and execution of the contract were included in the benchmark.

crates:
- name: pallet-contracts
bump: patch
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub extern "C" fn call() {
output: [u8],
);

// Burn some PoV, clear_storage consumes some PoV as in order to clear the storage we need to we
// need to read its size first.
api::clear_storage_v1(b"");

let exit_status = uapi::ReturnFlags::from_bits(exit_status[0] as u32).unwrap();
api::return_value(exit_status, output);
}
170 changes: 170 additions & 0 deletions substrate/frame/contracts/src/benchmarking/call_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
benchmarking::{Contract, WasmModule},
exec::Stack,
storage::meter::Meter,
wasm::Runtime,
BalanceOf, Config, DebugBufferVec, Determinism, ExecReturnValue, GasMeter, Origin, Schedule,
TypeInfo, WasmBlob, Weight,
};
use codec::{Encode, HasCompact};
use core::fmt::Debug;
use sp_core::Get;
use sp_std::prelude::*;

type StackExt<'a, T> = Stack<'a, T, WasmBlob<T>>;

/// A prepared contract call ready to be executed.
pub struct PreparedCall<'a, T: Config> {
func: wasmi::Func,
store: wasmi::Store<Runtime<'a, StackExt<'a, T>>>,
}

impl<'a, T: Config> PreparedCall<'a, T> {
pub fn call(mut self) -> ExecReturnValue {
let result = self.func.call(&mut self.store, &[], &mut []);
WasmBlob::<T>::process_result(self.store, result).unwrap()
}
}

/// A builder used to prepare a contract call.
pub struct CallSetup<T: Config> {
contract: Contract<T>,
dest: T::AccountId,
origin: Origin<T>,
gas_meter: GasMeter<T>,
storage_meter: Meter<T>,
schedule: Schedule<T>,
value: BalanceOf<T>,
debug_message: Option<DebugBufferVec<T>>,
determinism: Determinism,
data: Vec<u8>,
}

impl<T> CallSetup<T>
where
T: Config + pallet_balances::Config,
<BalanceOf<T> as HasCompact>::Type: Clone + Eq + PartialEq + Debug + TypeInfo + Encode,
{
/// Setup a new call for the given module.
pub fn new(module: WasmModule<T>) -> Self {
let contract = Contract::<T>::new(module.clone(), vec![]).unwrap();
let dest = contract.account_id.clone();
let origin = Origin::from_account_id(contract.caller.clone());

let storage_meter = Meter::new(&origin, None, 0u32.into()).unwrap();

Self {
contract,
dest,
origin,
gas_meter: GasMeter::new(Weight::MAX),
storage_meter,
schedule: T::Schedule::get(),
value: 0u32.into(),
debug_message: None,
determinism: Determinism::Enforced,
data: vec![],
}
}

/// Set the meter's storage deposit limit.
pub fn set_storage_deposit_limit(&mut self, balance: BalanceOf<T>) {
self.storage_meter = Meter::new(&self.origin, Some(balance), 0u32.into()).unwrap();
}

/// Set the call's origin.
pub fn set_origin(&mut self, origin: Origin<T>) {
self.origin = origin;
}

/// Set the contract's balance.
pub fn set_balance(&mut self, value: BalanceOf<T>) {
self.contract.set_balance(value);
}

/// Set the call's input data.
pub fn set_data(&mut self, value: Vec<u8>) {
self.data = value;
}

/// Set the debug message.
pub fn enable_debug_message(&mut self) {
self.debug_message = Some(Default::default());
}

/// Get the debug message.
pub fn debug_message(&self) -> Option<DebugBufferVec<T>> {
self.debug_message.clone()
}

/// Get the call's input data.
pub fn data(&self) -> Vec<u8> {
self.data.clone()
}

/// Get the call's contract.
pub fn contract(&self) -> Contract<T> {
self.contract.clone()
}

/// Build the call stack.
pub fn ext(&mut self) -> (StackExt<'_, T>, WasmBlob<T>) {
StackExt::bench_new_call(
self.dest.clone(),
self.origin.clone(),
&mut self.gas_meter,
&mut self.storage_meter,
&self.schedule,
self.value,
self.debug_message.as_mut(),
self.determinism,
)
}

/// Prepare a call to the module.
pub fn prepare_call<'a>(
ext: &'a mut StackExt<'a, T>,
module: WasmBlob<T>,
input: Vec<u8>,
) -> PreparedCall<'a, T> {
let (func, store) = module.bench_prepare_call(ext, input);
PreparedCall { func, store }
}
}

#[macro_export]
macro_rules! call_builder(
($func: ident, $module:expr) => {
$crate::call_builder!($func, _contract, $module);
};
($func: ident, $contract: ident, $module:expr) => {
let mut setup = CallSetup::<T>::new($module);
$crate::call_builder!($func, $contract, setup: setup);
};
($func:ident, setup: $setup: ident) => {
$crate::call_builder!($func, _contract, setup: $setup);
};
($func:ident, $contract: ident, setup: $setup: ident) => {
let data = $setup.data();
let $contract = $setup.contract();
let (mut ext, module) = $setup.ext();
let $func = CallSetup::<T>::prepare_call(&mut ext, module, data);
};
Comment on lines +164 to +169
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make the call side really hard to read because it just crates bindings which are not seen at the call side. I would prefer to return a tuple or a struct here.

Copy link
Contributor Author

@pgherveou pgherveou Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I would do that, without raising some borrowing compilation errors.
$func borrows from ext that borrows from $setup, if I make it a function or try to return something, rustc will complain that something does not live long enough.

Copy link
Member

@athei athei Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize you input the indentifiers so it is not actually as bad as I thought. My bad. You could move the ext into the $func`, though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, to do so we would need to update

pub struct Runtime<'a, E: Ext + 'a> {
	ext: &'a mut E,
	input_data: Option<Vec<u8>>,
	memory: Option<Memory>,
	chain_extension: Option<Box<<E::T as Config>::ChainExtension>>,
}

to take ownership of ext instead, is that what you had in mind ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a big change and I am not sure if this would actually work. I meant just moving the Ext into the PreparedCall if that is easily possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well then I don't see how we could do that easily without bothering the borrow checker.
Not too sure what that would buy us as well ...

);
Loading
Loading