diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index d94c33cb680..0ac45a686a5 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -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::::free_range(r, 1)?; }: { @@ -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::::free_range(1, p)?; }: { diff --git a/pallets/gear/src/benchmarking/syscalls.rs b/pallets/gear/src/benchmarking/syscalls.rs index d3ddc8905f2..8972880a4b0 100644 --- a/pallets/gear/src/benchmarking/syscalls.rs +++ b/pallets/gear/src/benchmarking/syscalls.rs @@ -262,28 +262,31 @@ where Self::prepare_handle(module, 0) } - // repetitions pub fn free_range(repetitions: u32, pages_per_call: u32) -> Result, &'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::() 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 { @@ -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, &'static str> {