1

I have a loop in call which depends on array from arguments, so how can i specify weight to be multiplied to array length from arguments

1
  • Please ensure the quality of this StackExchange by including code examples and explaining your problem in detail such that others can profit from it as well. Sep 27, 2022 at 19:45

1 Answer 1

1

You can use a linear component if your call accepts a collection.
As example take this one:

pub fn do_something(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult
{
    // Loop over data.
    Ok(())
}    

Which can then be benchmarked by defining a linear component over the length of the input vector.

benchmarks! {
    do_something {
        // Define linear component l. Must have one letter as name.
        let l in 0 .. T::MaxDataLen::get() as u32;

        let data = vec![255u8; l as usize];
    }: _(RawOrigin::Root, data)
    verify { /*optional*/ }
}

It is important that your function has at most a linear complexity over each component, otherwise the weight formulas will be calculated incorrectly.

Finally you can use the newly generated weight function as such:

#[pallet::weight(
    T::WeightInfo::do_something(data.len() as u32)
)]
pub fn do_something(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult
{
    // Loop over data.
    Ok(())
}    

You can see how the weight now depends on the vectors length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.