Weight v1.5 Follow Ups (#12155)

* update api

* update

* remove unused

* remove `one` api

* fix unused

* fmt

* add saturating accrue

* remove `Weight::new()`

* use some macros

* div makes no sense

* Update weight_v2.rs

* missed some

* more patch

* fixes

* more fixes

* more fix

* more fix

* Update frame/support/src/weights/weight_v2.rs

* not needed

* fix weight file
This commit is contained in:
Shawn Tabrizi
2022-09-01 18:48:03 +01:00
committed by GitHub
parent 6be21e0397
commit 1df4561676
80 changed files with 965 additions and 960 deletions
+1 -1
View File
@@ -1643,7 +1643,7 @@ macro_rules! decl_module {
pallet_name,
);
$crate::dispatch::Weight::new()
$crate::dispatch::Weight::zero()
}
#[cfg(feature = "try-runtime")]
+1 -1
View File
@@ -48,7 +48,7 @@ impl<T: GetStorageVersion + PalletInfoAccess> PalletVersionToStorageVersionHelpe
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
impl PalletVersionToStorageVersionHelper for T {
fn migrate(db_weight: &RuntimeDbWeight) -> Weight {
let mut weight = Weight::new();
let mut weight = Weight::zero();
for_tuples!( #( weight = weight.saturating_add(T::migrate(db_weight)); )* );
+9 -9
View File
@@ -35,7 +35,7 @@ pub trait OnInitialize<BlockNumber> {
/// including inherent extrinsics. Hence for instance, if you runtime includes
/// `pallet_timestamp`, the `timestamp` is not yet up to date at this point.
fn on_initialize(_n: BlockNumber) -> Weight {
Weight::new()
Weight::zero()
}
}
@@ -44,7 +44,7 @@ pub trait OnInitialize<BlockNumber> {
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
fn on_initialize(n: BlockNumber) -> Weight {
let mut weight = Weight::new();
let mut weight = Weight::zero();
for_tuples!( #( weight = weight.saturating_add(Tuple::on_initialize(n.clone())); )* );
weight
}
@@ -77,7 +77,7 @@ pub trait OnIdle<BlockNumber> {
/// NOTE: This function is called AFTER ALL extrinsics - including inherent extrinsics -
/// in a block are applied but before `on_finalize` is executed.
fn on_idle(_n: BlockNumber, _remaining_weight: Weight) -> Weight {
Weight::new()
Weight::zero()
}
}
@@ -88,7 +88,7 @@ impl<BlockNumber: Copy + AtLeast32BitUnsigned> OnIdle<BlockNumber> for Tuple {
fn on_idle(n: BlockNumber, remaining_weight: Weight) -> Weight {
let on_idle_functions: &[fn(BlockNumber, Weight) -> Weight] =
&[for_tuples!( #( Tuple::on_idle ),* )];
let mut weight = Weight::new();
let mut weight = Weight::zero();
let len = on_idle_functions.len();
let start_index = n % (len as u32).into();
let start_index = start_index.try_into().ok().expect(
@@ -130,7 +130,7 @@ pub trait OnRuntimeUpgrade {
///
/// Return the non-negotiable weight consumed for runtime upgrade.
fn on_runtime_upgrade() -> Weight {
Weight::new()
Weight::zero()
}
/// Execute some pre-checks prior to a runtime upgrade.
@@ -155,7 +155,7 @@ pub trait OnRuntimeUpgrade {
#[cfg_attr(feature = "tuples-128", impl_for_tuples(128))]
impl OnRuntimeUpgrade for Tuple {
fn on_runtime_upgrade() -> Weight {
let mut weight = Weight::new();
let mut weight = Weight::zero();
for_tuples!( #( weight = weight.saturating_add(Tuple::on_runtime_upgrade()); )* );
weight
}
@@ -199,14 +199,14 @@ pub trait Hooks<BlockNumber> {
/// Return the weight used, the hook will subtract it from current weight used
/// and pass the result to the next `on_idle` hook if it exists.
fn on_idle(_n: BlockNumber, _remaining_weight: Weight) -> Weight {
Weight::new()
Weight::zero()
}
/// The block is being initialized. Implement to have something happen.
///
/// Return the non-negotiable weight consumed in the block.
fn on_initialize(_n: BlockNumber) -> Weight {
Weight::new()
Weight::zero()
}
/// Perform a module upgrade.
@@ -229,7 +229,7 @@ pub trait Hooks<BlockNumber> {
/// logic as a free-function from your pallet, and pass it to `type Executive` from the
/// top-level runtime.
fn on_runtime_upgrade() -> Weight {
Weight::new()
Weight::zero()
}
/// Execute the sanity checks of this pallet, per block.
+1 -1
View File
@@ -650,7 +650,7 @@ impl<T: Clone> PerDispatchClass<T> {
impl PerDispatchClass<Weight> {
/// Returns the total weight consumed by all extrinsics in the block.
pub fn total(&self) -> Weight {
let mut sum = Weight::new();
let mut sum = Weight::zero();
for class in DispatchClass::all() {
sum = sum.saturating_add(*self.get(*class));
}
@@ -53,7 +53,7 @@ parameter_types! {
/// 99th: 5_489_273
/// 95th: 5_433_314
/// 75th: 5_354_812
pub const BlockExecutionWeight: Weight = WEIGHT_PER_NANOS.scalar_saturating_mul(5_346_284);
pub const BlockExecutionWeight: Weight = WEIGHT_PER_NANOS.saturating_mul(5_346_284);
}
#[cfg(test)]
@@ -68,8 +68,8 @@ mod test_weights {
let w = super::BlockExecutionWeight::get();
// At least 100 µs.
assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs.");
assert!(w >= 100u32 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs.");
// At most 50 ms.
assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms.");
assert!(w <= 50u32 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms.");
}
}
@@ -53,7 +53,7 @@ parameter_types! {
/// 99th: 86_924
/// 95th: 86_828
/// 75th: 86_347
pub const ExtrinsicBaseWeight: Weight = WEIGHT_PER_NANOS.scalar_saturating_mul(86_298);
pub const ExtrinsicBaseWeight: Weight = WEIGHT_PER_NANOS.saturating_mul(86_298);
}
#[cfg(test)]
@@ -68,7 +68,7 @@ mod test_weights {
let w = super::ExtrinsicBaseWeight::get();
// At least 10 µs.
assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs.");
assert!(w >= 10u32 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs.");
// At most 1 ms.
assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms.");
}
+101 -108
View File
@@ -18,8 +18,8 @@
use codec::{CompactAs, Decode, Encode, MaxEncodedLen};
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use sp_runtime::{
traits::{Bounded, CheckedAdd, CheckedSub, One, Zero},
Perquintill, RuntimeDebug,
traits::{Bounded, CheckedAdd, CheckedSub, Zero},
RuntimeDebug,
};
use super::*;
@@ -50,11 +50,6 @@ pub struct Weight {
}
impl Weight {
/// Create a new Weight with zero.
pub const fn new() -> Self {
Self { ref_time: 0 }
}
/// Set the reference time part of the weight.
pub const fn set_ref_time(mut self, c: RefTimeWeight) -> Self {
self.ref_time = c;
@@ -93,27 +88,84 @@ impl Weight {
}
}
/// Construct with reference time weight.
/// Construct [`Weight`] with reference time weight.
pub const fn from_ref_time(ref_time: RefTimeWeight) -> Self {
Self { ref_time }
}
pub fn checked_mul(self, rhs: u64) -> Option<Self> {
let ref_time = self.ref_time.checked_mul(rhs)?;
Some(Self { ref_time })
/// Saturating [`Weight`] addition. Computes `self + rhs`, saturating at the numeric bounds of
/// all fields instead of overflowing.
pub const fn saturating_add(self, rhs: Self) -> Self {
Self { ref_time: self.ref_time.saturating_add(rhs.ref_time) }
}
pub fn checked_div(self, rhs: u64) -> Option<Self> {
let ref_time = self.ref_time.checked_div(rhs)?;
Some(Self { ref_time })
/// Saturating [`Weight`] subtraction. Computes `self - rhs`, saturating at the numeric bounds
/// of all fields instead of overflowing.
pub const fn saturating_sub(self, rhs: Self) -> Self {
Self { ref_time: self.ref_time.saturating_sub(rhs.ref_time) }
}
pub const fn scalar_saturating_mul(self, rhs: u64) -> Self {
Self { ref_time: self.ref_time.saturating_mul(rhs) }
/// Saturating [`Weight`] scalar multiplication. Computes `self.field * scalar` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
pub const fn saturating_mul(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time.saturating_mul(scalar) }
}
pub const fn scalar_div(self, rhs: u64) -> Self {
Self { ref_time: self.ref_time / rhs }
/// Saturating [`Weight`] scalar division. Computes `self.field / scalar` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
pub const fn saturating_div(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time.saturating_div(scalar) }
}
/// Saturating [`Weight`] scalar exponentiation. Computes `self.field.pow(exp)` for all fields,
/// saturating at the numeric bounds of all fields instead of overflowing.
pub const fn saturating_pow(self, exp: u32) -> Self {
Self { ref_time: self.ref_time.saturating_pow(exp) }
}
/// Increment [`Weight`] by `amount` via saturating addition.
pub fn saturating_accrue(&mut self, amount: Self) {
*self = self.saturating_add(amount);
}
/// Checked [`Weight`] addition. Computes `self + rhs`, returning `None` if overflow occurred.
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
match self.ref_time.checked_add(rhs.ref_time) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
/// Checked [`Weight`] subtraction. Computes `self - rhs`, returning `None` if overflow
/// occurred.
pub const fn checked_sub(&self, rhs: &Self) -> Option<Self> {
match self.ref_time.checked_sub(rhs.ref_time) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
/// Checked [`Weight`] scalar multiplication. Computes `self.field * scalar` for each field,
/// returning `None` if overflow occurred.
pub const fn checked_mul(self, scalar: u64) -> Option<Self> {
match self.ref_time.checked_mul(scalar) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
/// Checked [`Weight`] scalar division. Computes `self.field / scalar` for each field, returning
/// `None` if overflow occurred.
pub const fn checked_div(self, scalar: u64) -> Option<Self> {
match self.ref_time.checked_div(scalar) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
/// Return a [`Weight`] where all fields are zero.
pub const fn zero() -> Self {
Self { ref_time: 0 }
}
}
@@ -127,12 +179,6 @@ impl Zero for Weight {
}
}
impl One for Weight {
fn one() -> Self {
Self::one()
}
}
impl Add for Weight {
type Output = Self;
fn add(self, rhs: Self) -> Self {
@@ -147,13 +193,6 @@ impl Sub for Weight {
}
}
impl Mul for Weight {
type Output = Self;
fn mul(self, b: Self) -> Self {
Self { ref_time: b.ref_time * self.ref_time }
}
}
impl<T> Mul<T> for Weight
where
T: Mul<u64, Output = u64> + Copy,
@@ -164,26 +203,39 @@ where
}
}
impl Mul<Weight> for Perbill {
type Output = Weight;
fn mul(self, b: Weight) -> Weight {
Weight { ref_time: self * b.ref_time }
macro_rules! weight_mul_per_impl {
($($t:ty),* $(,)?) => {
$(
impl Mul<Weight> for $t {
type Output = Weight;
fn mul(self, b: Weight) -> Weight {
Weight { ref_time: self * b.ref_time }
}
}
)*
}
}
weight_mul_per_impl!(
sp_runtime::Percent,
sp_runtime::PerU16,
sp_runtime::Permill,
sp_runtime::Perbill,
sp_runtime::Perquintill,
);
impl Mul<Weight> for Perquintill {
type Output = Weight;
fn mul(self, b: Weight) -> Weight {
Weight { ref_time: self * b.ref_time }
}
}
impl Mul<Weight> for u64 {
type Output = Weight;
fn mul(self, b: Weight) -> Weight {
Weight { ref_time: self * b.ref_time }
macro_rules! weight_mul_primitive_impl {
($($t:ty),* $(,)?) => {
$(
impl Mul<Weight> for $t {
type Output = Weight;
fn mul(self, b: Weight) -> Weight {
Weight { ref_time: u64::from(self) * b.ref_time }
}
}
)*
}
}
weight_mul_primitive_impl!(u8, u16, u32, u64);
impl<T> Div<T> for Weight
where
@@ -196,24 +248,6 @@ where
}
}
impl Saturating for Weight {
fn saturating_add(self, rhs: Self) -> Self {
self.saturating_add(rhs)
}
fn saturating_sub(self, rhs: Self) -> Self {
self.saturating_sub(rhs)
}
fn saturating_mul(self, rhs: Self) -> Self {
self.saturating_mul(rhs)
}
fn saturating_pow(self, exp: usize) -> Self {
self.saturating_pow(exp)
}
}
impl CheckedAdd for Weight {
fn checked_add(&self, rhs: &Self) -> Option<Self> {
self.checked_add(rhs)
@@ -344,53 +378,12 @@ impl sp_runtime::traits::Printable for Weight {
}
}
// Re-export common functions so you do not need to import trait.
impl Weight {
pub const fn zero() -> Self {
Self { ref_time: 0 }
}
pub const fn one() -> Self {
Self { ref_time: 1 }
}
pub const fn saturating_add(self, rhs: Self) -> Self {
Self { ref_time: self.ref_time.saturating_add(rhs.ref_time) }
}
pub const fn saturating_sub(self, rhs: Self) -> Self {
Self { ref_time: self.ref_time.saturating_sub(rhs.ref_time) }
}
pub const fn saturating_mul(self, rhs: Self) -> Self {
Self { ref_time: self.ref_time.saturating_mul(rhs.ref_time) }
}
pub const fn saturating_pow(self, exp: usize) -> Self {
Self { ref_time: self.ref_time.saturating_pow(exp as u32) }
}
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
match self.ref_time.checked_add(rhs.ref_time) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
pub const fn checked_sub(&self, rhs: &Self) -> Option<Self> {
match self.ref_time.checked_sub(rhs.ref_time) {
Some(ref_time) => Some(Self { ref_time }),
None => None,
}
}
}
// TODO: Eventually remove these
impl From<Option<RefTimeWeight>> for PostDispatchInfo {
fn from(maybe_actual_computation: Option<RefTimeWeight>) -> Self {
let actual_weight = match maybe_actual_computation {
Some(actual_computation) => Some(Weight::new().set_ref_time(actual_computation)),
Some(actual_computation) => Some(Weight::zero().set_ref_time(actual_computation)),
None => None,
};
Self { actual_weight, pays_fee: Default::default() }
@@ -401,7 +394,7 @@ impl From<(Option<RefTimeWeight>, Pays)> for PostDispatchInfo {
fn from(post_weight_info: (Option<RefTimeWeight>, Pays)) -> Self {
let (maybe_actual_time, pays_fee) = post_weight_info;
let actual_weight = match maybe_actual_time {
Some(actual_time) => Some(Weight::new().set_ref_time(actual_time)),
Some(actual_time) => Some(Weight::zero().set_ref_time(actual_time)),
None => None,
};
Self { actual_weight, pays_fee }
@@ -410,7 +403,7 @@ impl From<(Option<RefTimeWeight>, Pays)> for PostDispatchInfo {
impl<T> WeighData<T> for RefTimeWeight {
fn weigh_data(&self, _: T) -> Weight {
return Weight::new().set_ref_time(*self)
return Weight::zero().set_ref_time(*self)
}
}