Skip to content

Clamp benchmark ranks to respect MaxRank#7720

Merged
ggwpez merged 13 commits intoparitytech:masterfrom
Doordashcon:GREM
Apr 15, 2025
Merged

Clamp benchmark ranks to respect MaxRank#7720
ggwpez merged 13 commits intoparitytech:masterfrom
Doordashcon:GREM

Conversation

@Doordashcon
Copy link
Contributor

@Doordashcon Doordashcon commented Feb 25, 2025

resolves #7517

Issues

1. Compile-Time vs Runtime Rank Mismatch

  • promote_fast Benchmark uses mock runtime's MaxRank during benchmark generation while allowing runtime overrides. Creating potential parameter mismatch between benchmark metadata(i.e. generated r values) and runtime configuration(i.e. T::MaxRank).

2. Static Rank Assumptions

  • bump_demote Benchmark initialized members at rank 2, making it incompatible with MaxRank=1 configurations
  • promote Benchmark contained hardcoded rank values (1 → 2) which fails for MaxRank=1

Changes

Dynamic Rank Clamping

// promote_fast
// Get target rank for promotion.
let max_rank = T::MaxRank::get();
let target_rank = (r as u16).min(max_rank);

// promote
// Set `to_rank` dynamically based on `max_rank`.
let to_rank = (current_rank + 1).min(max_rank);

@Doordashcon Doordashcon marked this pull request as ready for review March 2, 2025 13:30
@Doordashcon Doordashcon requested a review from a team as a code owner March 2, 2025 13:30
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

The general logic looks good, just some nit improvements to make this better :)

@paritytech-review-bot paritytech-review-bot bot requested a review from a team March 9, 2025 01:13
@bkchr bkchr added the T2-pallets This PR/Issue is related to a particular pallet. label Mar 10, 2025
@bkchr bkchr requested a review from ggwpez March 10, 2025 09:49
@bkchr bkchr requested a review from ggwpez April 11, 2025 17:19
Copy link
Contributor

@acatangiu acatangiu left a comment

Choose a reason for hiding this comment

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

I like the benchmark fix, I personally don’t like the u32->u16 type change because it adds more complexity with some custom converters types and is a breaking change for runtime builders, for no strong reason/benefit.

Not blocking the PR for this, but want to get your opinion on above.

@bkchr
Copy link
Member

bkchr commented Apr 12, 2025

Not blocking the PR for this, but want to get your opinion on above.

The core fellowship is only used by the collectives chain. The user base of this isn't that big and also given the required change I think that the change is reasonable.

@ggwpez ggwpez enabled auto-merge April 15, 2025 11:15
@ggwpez ggwpez added this pull request to the merge queue Apr 15, 2025
Merged via the queue into paritytech:master with commit 4bbcf42 Apr 15, 2025
234 of 243 checks passed
Krayt78 pushed a commit to Krayt78/polkadot-sdk that referenced this pull request Apr 18, 2025
resolves paritytech#7517 

### Issues

#### 1. Compile-Time vs Runtime Rank Mismatch
- **`promote_fast` Benchmark** uses mock runtime's `MaxRank` during
benchmark generation while allowing runtime overrides. Creating
potential parameter mismatch between benchmark metadata(i.e. generated
`r` values) and runtime configuration(i.e. `T::MaxRank`).


#### 2. Static Rank Assumptions
- **`bump_demote` Benchmark** initialized members at rank 2, making it
incompatible with `MaxRank=1` configurations
- **`promote` Benchmark** contained hardcoded rank values (1 → 2) which
fails for `MaxRank=1`


## Changes

#### Dynamic Rank Clamping
```rust
// promote_fast
// Get target rank for promotion.
let max_rank = T::MaxRank::get();
let target_rank = (r as u16).min(max_rank);

// promote
// Set `to_rank` dynamically based on `max_rank`.
let to_rank = (current_rank + 1).min(max_rank);

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
castillax pushed a commit that referenced this pull request May 12, 2025
resolves #7517 

### Issues

#### 1. Compile-Time vs Runtime Rank Mismatch
- **`promote_fast` Benchmark** uses mock runtime's `MaxRank` during
benchmark generation while allowing runtime overrides. Creating
potential parameter mismatch between benchmark metadata(i.e. generated
`r` values) and runtime configuration(i.e. `T::MaxRank`).


#### 2. Static Rank Assumptions
- **`bump_demote` Benchmark** initialized members at rank 2, making it
incompatible with `MaxRank=1` configurations
- **`promote` Benchmark** contained hardcoded rank values (1 → 2) which
fails for `MaxRank=1`


## Changes

#### Dynamic Rank Clamping
```rust
// promote_fast
// Get target rank for promotion.
let max_rank = T::MaxRank::get();
let target_rank = (r as u16).min(max_rank);

// promote
// Set `to_rank` dynamically based on `max_rank`.
let to_rank = (current_rank + 1).min(max_rank);

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@bkontur bkontur mentioned this pull request Aug 19, 2025
1 task
fn induct() -> Weight;
fn promote() -> Weight;
fn promote_fast(r: u32, ) -> Weight;
fn promote_fast(r: u16, ) -> Weight;
Copy link
Contributor

Choose a reason for hiding this comment

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

@Doordashcon Changed this back to u32 because Linear currently supports only u32. Using u16 here caused a compilation issue when generating weights, more info and fix: #9516.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T2-pallets This PR/Issue is related to a particular pallet.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Benchmark promote_fast and others in pallet-core-fellowship Generates Ranks Exceeding MaxRank

6 participants