Skip to content

Commit

Permalink
reduce free_range banchmark iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
grishasobol committed Nov 10, 2023
1 parent ec644b3 commit 2e90c3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pallets/gear/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ benchmarks! {
}

free_range {
let r in 0..512;
let r in 0 .. API_BENCHMARK_BATCHES;
let mut res = None;
let exec = Benches::<T>::free_range(r, 1)?;
}: {
Expand All @@ -838,7 +838,7 @@ benchmarks! {
}

free_range_per_page {
let p in 0..512;
let p in 1 .. API_BENCHMARK_BATCHES;
let mut res = None;
let exec = Benches::<T>::free_range(1, p)?;
}: {
Expand Down
39 changes: 21 additions & 18 deletions pallets/gear/src/benchmarking/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,31 @@ where
Self::prepare_handle(module, 0)
}

// repetitions
pub fn free_range(repetitions: u32, pages_per_call: u32) -> Result<Exec<T>, &'static str> {
const MAX_PAGES_OVERRIDE: u16 = u16::MAX;

use Instruction::*;

let mut instructions = vec![];

let n_pages = 512 * API_BENCHMARK_BATCH_SIZE;
assert!(n_pages <= MAX_PAGES_OVERRIDE as u32);

// allocate pages
instructions.extend([I32Const(n_pages as i32), Call(0), I32Const(-1)]);
unreachable_condition(&mut instructions, I32Eq); // if alloc returns -1 then it's error
let n_pages = repetitions.checked_mul(pages_per_call).unwrap();
assert!(n_pages <= max_pages::<T>() as u32);

for i in 0..(API_BENCHMARK_BATCH_SIZE * repetitions) {
// free them in steps
let start = i as i32 * pages_per_call as i32;
let end = i32::max(start + pages_per_call as i32 - 1, 0);
let mut instructions = vec![];
for _ in 0..API_BENCHMARK_BATCH_SIZE {
instructions.extend([I32Const(n_pages as i32), Call(0), I32Const(-1)]);
unreachable_condition(&mut instructions, I32Eq); // if alloc returns -1 then it's error

instructions.extend([I32Const(start), I32Const(end), Call(1), I32Const(0)]);
unreachable_condition(&mut instructions, I32Ne); // if free_range returns not 0 then it's error
for i in 0..repetitions {
let start = i.checked_mul(pages_per_call).unwrap();
let end = pages_per_call
.checked_sub(1)
.and_then(|x| start.checked_add(x))
.unwrap();
instructions.extend([
I32Const(start as i32),
I32Const(end as i32),
Call(1),
I32Const(0),
]);
unreachable_condition(&mut instructions, I32Ne); // if free returns 0 then it's error
}
}

let module = ModuleDefinition {
Expand All @@ -293,7 +296,7 @@ where
..Default::default()
};

Self::prepare_handle_override_max_pages(module, 0, MAX_PAGES_OVERRIDE.into())
Self::prepare_handle(module, 0)
}

pub fn gr_reserve_gas(r: u32) -> Result<Exec<T>, &'static str> {
Expand Down

0 comments on commit 2e90c3b

Please sign in to comment.