Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Weight v1.5 Follow Ups #12155

Merged
merged 23 commits into from
Sep 1, 2022
Merged

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Aug 31, 2022

This PR does some follow ups to Weight v1.5: #12138

  • Improve math APIs exposed by Weight.
    • Mul and Div are always scalar over the fields.
    • We do not support Weight * Weight or Weight / Weight which is nonsensical in the context of multi-dimensional weights.
  • Remove Weight::one() which doesn't really make sense with multi-dimensional weights.
  • Remove Weight::new() and use just Weight::zero() instead.
  • Introduce macros to multiply by u8, u16, u32, u64, Percent, PerU16, Permill, Perbill, Perquintill

polkadot companion: paritytech/polkadot#5949
cumulus companion: paritytech/cumulus#1584

@github-actions github-actions bot added the A0-please_review Pull request needs code review. label Aug 31, 2022
@shawntabrizi shawntabrizi added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Aug 31, 2022
@shawntabrizi
Copy link
Member Author

More follow up here: #12157

}

/// Checked [`Weight`] addition. Computes `self + rhs`, returning `None` if overflow occurred.
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use standalone functions instead of implementing Checked and Saturating traits?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do implement the checked trait below (where it was possible), but these are const functions.

Saturating implies too many things, and it did not make sense for me to implement all of them

}

/// Return a [`Weight`] where all fields are zero.
pub const fn zero() -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, trait Zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is implemented, but again, this is a const function, where the trait is not.

also to avoid needing to import zero every time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I didn't notice them being const.

To be fair though, I don't agree with the imports difficulty. You can take the same argument and never use Zero. The benefit is that you can get all the blanket implementations where T: Zero for free in return.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if I also implement the Zero trait if you look just a little lower on the file... so you get the best of both worlds?

In fact, I am a pretty big fan of having the const impls exposed via the struct, it just makes these functions easier to access, not to mention const.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this wouldn't be an issue if we had const Trait in Rust, which is awaiting stabilization.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a bit unclear why we implement standalone functions for operations that have standard traits, otherwise looks good.

@shawntabrizi
Copy link
Member Author

Just a bit unclear why we implement standalone functions for operations that have standard traits, otherwise looks good.

because the traits are not const, and things like saturating, imply that we must implement things like saturating_mul(Self), which does not make sense.

Finally, it can help reduce the number of trait imports you need when using the Weight type and functions. So in this case, every trait which I could implement, i did, but only as a re-export of the internal functions which are all const.

@@ -230,8 +230,7 @@ where
let weight_per_key = (T::WeightInfo::on_initialize_per_trie_key(1) -
T::WeightInfo::on_initialize_per_trie_key(0))
.ref_time();
let decoding_weight =
weight_per_queue_item.scalar_saturating_mul(queue_len as RefTimeWeight);
let decoding_weight = weight_per_queue_item.saturating_mul(queue_len as RefTimeWeight);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think queue_len is a weight:

Suggested change
let decoding_weight = weight_per_queue_item.saturating_mul(queue_len as RefTimeWeight);
let decoding_weight = weight_per_queue_item.saturating_mul(queue_len as u64);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed here: #12157

@shawntabrizi
Copy link
Member Author

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot paritytech-processbot bot merged commit ed12e60 into master Sep 1, 2022
@paritytech-processbot paritytech-processbot bot deleted the shawntabrizi-weight-v1.5-patches branch September 1, 2022 17:48
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants