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

Improve Weight Template and API #13355

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
{{#if (ne benchmark.base_calculated_proof_size "0")}}
Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}})
{{else}}
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{/if}}
Copy link
Member

Choose a reason for hiding this comment

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

This will always add 0. Maybe keep the if?

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 think adding 0 might not be bad? We assume most things will have a base PoV size, and if it doesnt, this can explicitly call it out. I mean i dont really have a strong opinion, but could make it clear that a PoV WAS calculated to be zero, versus not sure if it was calculated.

Copy link
Member

Choose a reason for hiding this comment

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

Okay. Yea I think its fine either way.

.saturating_add(Weight::from_proof_size({{benchmark.base_calculated_proof_size}}))
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
Expand Down Expand Up @@ -99,11 +96,8 @@ impl WeightInfo for () {
// Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
{{#if (ne benchmark.base_calculated_proof_size "0")}}
Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}})
{{else}}
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{/if}}
.saturating_add(Weight::from_proof_size({{benchmark.base_calculated_proof_size}}))
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
Expand Down
26 changes: 20 additions & 6 deletions primitives/weights/src/weight_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,32 @@ impl Weight {
Self { ref_time: 0, proof_size: 0 }
}

/// Constant version of Add with u64.
/// Constant version of Add for `ref_time` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
pub const fn add(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time + scalar, proof_size: self.proof_size + scalar }
pub const fn add_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time + scalar, proof_size: self.proof_size }
}

/// Constant version of Sub with u64.
/// Constant version of Add for `proof_size` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
pub const fn sub(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time - scalar, proof_size: self.proof_size - scalar }
pub const fn add_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size + scalar }
}

/// Constant version of Sub for `ref_time` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
pub const fn sub_ref_time(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time - scalar, proof_size: self.proof_size }
}

/// Constant version of Sub for `proof_size` component with u64.
///
/// Is only overflow safe when evaluated at compile-time.
pub const fn sub_proof_size(self, scalar: u64) -> Self {
Self { ref_time: self.ref_time, proof_size: self.proof_size - scalar }
}

/// Constant version of Div with u64.
Expand Down
5 changes: 1 addition & 4 deletions utils/frame/benchmarking-cli/src/pallet/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ impl<T: frame_system::Config> {{pallet}}::WeightInfo for WeightInfo<T> {
// Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds.
{{#if (ne benchmark.base_calculated_proof_size "0")}}
Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}})
{{else}}
Weight::from_ref_time({{underscore benchmark.base_weight}})
{{/if}}
.saturating_add(Weight::from_proof_size({{benchmark.base_calculated_proof_size}}))
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into()))
Expand Down