Skip to content

Commit

Permalink
bench
Browse files Browse the repository at this point in the history
Signed-off-by: xermicus <[email protected]>
  • Loading branch information
xermicus committed Dec 13, 2024
1 parent 6796023 commit 3acb518
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
45 changes: 19 additions & 26 deletions substrate/frame/revive/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ mod benchmarks {

// We just call a dummy contract to measure the overhead of the call extrinsic.
// The size of the data has no influence on the costs of this extrinsic as long as the contract
// won't call `seal_input` in its constructor to copy the data to contract memory.
// won't call `seal_call_data_copy` in its constructor to copy the data to contract memory.
// The dummy contract used here does not do this. The costs for the data copy is billed as
// part of `seal_input`. The costs for invoking a contract of a specific size are not part
// part of `seal_call_data_copy`. The costs for invoking a contract of a specific size are not part
// of this benchmark because we cannot know the size of the contract when issuing a call
// transaction. See `call_with_code_per_byte` for this.
#[benchmark(pov_mode = Measured)]
Expand Down Expand Up @@ -771,21 +771,6 @@ mod benchmarks {
assert_eq!(U256::from_little_endian(&memory[..]), runtime.ext().minimum_balance());
}

#[benchmark(pov_mode = Measured)]
fn seal_call_data_size() {
let mut setup = CallSetup::<T>::default();
let (mut ext, _) = setup.ext();
let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![42u8; 128 as usize]);
let mut memory = memory!(vec![0u8; 32 as usize],);
let result;
#[block]
{
result = runtime.bench_call_data_size(memory.as_mut_slice(), 0);
}
assert_ok!(result);
assert_eq!(U256::from_little_endian(&memory[..]), U256::from(128));
}

#[benchmark(pov_mode = Measured)]
fn seal_block_number() {
build_runtime!(runtime, memory: [[0u8;32], ]);
Expand Down Expand Up @@ -854,33 +839,41 @@ mod benchmarks {
}

#[benchmark(pov_mode = Measured)]
fn seal_call_data_load() {
fn seal_copy_to_contract(n: Linear<0, { limits::code::BLOB_BYTES - 4 }>) {
let mut setup = CallSetup::<T>::default();
let (mut ext, _) = setup.ext();
let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![42u8; 32]);
let mut memory = memory!(vec![0u8; 32],);
let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![]);
let mut memory = memory!(n.encode(), vec![0u8; n as usize],);
let result;
#[block]
{
result = runtime.bench_call_data_load(memory.as_mut_slice(), 0, 0);
result = runtime.write_sandbox_output(
memory.as_mut_slice(),
4,
0,
&vec![42u8; n as usize],
false,
|_| None,
);
}
assert_ok!(result);
assert_eq!(&memory[..], &vec![42u8; 32]);
assert_eq!(&memory[..4], &n.encode());
assert_eq!(&memory[4..], &vec![42u8; n as usize]);
}

#[benchmark(pov_mode = Measured)]
fn seal_input(n: Linear<0, { limits::code::BLOB_BYTES - 4 }>) {
fn seal_call_data_copy(n: Linear<0, { limits::code::BLOB_BYTES }>) {
let mut setup = CallSetup::<T>::default();
let (mut ext, _) = setup.ext();
let mut runtime = crate::wasm::Runtime::new(&mut ext, vec![42u8; n as usize]);
let mut memory = memory!(n.to_le_bytes(), vec![0u8; n as usize],);
let mut memory = memory!(vec![0u8; n as usize],);
let result;
#[block]
{
result = runtime.bench_input(memory.as_mut_slice(), 4, 0);
result = runtime.bench_call_data_copy(memory.as_mut_slice(), 0, n, 0);
}
assert_ok!(result);
assert_eq!(&memory[4..], &vec![42u8; n as usize]);
assert_eq!(&memory[..], &vec![42u8; n as usize]);
}

#[benchmark(pov_mode = Measured)]
Expand Down
12 changes: 11 additions & 1 deletion substrate/frame/revive/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl<T: Config> Memory<T> for [u8] {
bound_checked.copy_from_slice(buf);
Ok(())
}


fn zero(&mut self, ptr: u32, len: u32) -> Result<(), DispatchError> {
<[u8] as Memory<T>>::write(self, ptr, &vec![0; len as usize])
}
}

impl<T: Config> Memory<T> for polkavm::RawInstance {
Expand Down Expand Up @@ -280,6 +285,8 @@ pub enum RuntimeCosts {
CopyToContract(u32),
/// Weight of calling `seal_call_data_load``.
CallDataLoad,
/// Weight of calling `seal_call_data_copy`.
CallDataCopy(u32),
/// Weight of calling `seal_caller`.
Caller,
/// Weight of calling `seal_call_data_size`.
Expand Down Expand Up @@ -442,10 +449,11 @@ impl<T: Config> Token<T> for RuntimeCosts {
use self::RuntimeCosts::*;
match *self {
HostFn => cost_args!(noop_host_fn, 1),
CopyToContract(len) => T::WeightInfo::seal_input(len),
CopyToContract(len) => T::WeightInfo::seal_copy_to_contract(len),
CopyFromContract(len) => T::WeightInfo::seal_return(len),
CallDataSize => T::WeightInfo::seal_call_data_size(),
CallDataLoad => T::WeightInfo::seal_call_data_load(),
CallDataCopy(len) => T::WeightInfo::seal_call_data_copy(len),
Caller => T::WeightInfo::seal_caller(),
Origin => T::WeightInfo::seal_origin(),
IsContract => T::WeightInfo::seal_is_contract(),
Expand Down Expand Up @@ -1343,6 +1351,8 @@ pub mod env {
out_len: u32,
offset: u32,
) -> Result<(), TrapReason> {
self.charge_gas(RuntimeCosts::CallDataCopy(out_len))?;

let Some(input) = self.input_data.as_ref() else {
return Err(Error::<E::T>::InputForwarded.into());
};
Expand Down
27 changes: 24 additions & 3 deletions substrate/frame/revive/src/weights.rs

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

2 changes: 1 addition & 1 deletion substrate/frame/revive/uapi/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bitflags! {
///
/// A forwarding call will consume the current contracts input. Any attempt to
/// access the input after this call returns will lead to [`Error::InputForwarded`].
/// It does not matter if this is due to calling `seal_input` or trying another
/// It does not matter if this is due to calling `call_data_copy` or trying another
/// forwarding call. Consider using [`Self::CLONE_INPUT`] in order to preserve
/// the input.
const FORWARD_INPUT = 0b0000_0001;
Expand Down

0 comments on commit 3acb518

Please sign in to comment.