Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gas profile by parameter #8148

Merged
merged 14 commits into from
Jan 11, 2023
Merged

Conversation

jakmeier
Copy link
Contributor

@jakmeier jakmeier commented Dec 1, 2022

This resolves #8033, completing the gas profile by parameter effort.

The main achievement here is that we can now look at gas profiles and
know exactly for which parameter it has been spent.

Code wise, the old enum Cost used in profiles only is finally gone.

The tricky parts in this PR are to make sure that old profiles stored on DB
are still understood and presented correctly in RPC calls.

core/primitives/src/transaction.rs Outdated Show resolved Hide resolved
core/primitives-core/src/config.rs Outdated Show resolved Hide resolved
Comment on lines 40 to 41
for (cost, gas) in self.actions_profile.iter_mut() {
*gas = gas.saturating_add(other.actions_profile[cost]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider

Suggested change
for (cost, gas) in self.actions_profile.iter_mut() {
*gas = gas.saturating_add(other.actions_profile[cost]);
}
for ((_, gas), (_, other_gas)) in self.actions_profile.iter_mut().zip(other.actions_profile.iter()) {
*gas = gas.saturating_add(*other_gas);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, feels more complicated to achieve the same thing? Anything wrong with the original code?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indexing of the other.actions_profile[cost] is a panicky operation, and it needs to bound check every time too (as the indices are going upwards). The zip approach, avoids both of these problems by a) necessarily picking a handling mechanism (truncation) when the lengths mismatch, and b) being an iterator, thus not needing bound checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, these are convincing arguments. Updated!

core/primitives-core/src/profile.rs Outdated Show resolved Hide resolved
core/primitives-core/src/profile.rs Outdated Show resolved Hide resolved
core/primitives/res/runtime_configs/parameters.txt Outdated Show resolved Hide resolved
@jakmeier
Copy link
Contributor Author

jakmeier commented Dec 7, 2022

Hey @nagisa thanks for reviewing! Sorry, I only created this draft PR to run tests on CI, it builds on the changes in #8115 which have been hanging in review for 2 weeks. This probably made it a bit harder to review than necessary.

Now that the other PR is merged, I will rebase this PR, address your comments and then mark it as ready for review afterwards.

jakmeier and others added 4 commits December 7, 2022 09:53
- use ProfileDataV2 in new profiles
- removed all methods from ProfileDataV1 used to create new profiles
- added borsh support to ProfileDataV2
- test for borsh
- name profile data versions to be consistent with meta data versions
- move deprecated profile to profile_v2.rs
- use enum discriminants instead of manual index mapping
- add  changelog entry for new gas profiles
- update tests
@jakmeier
Copy link
Contributor Author

jakmeier commented Dec 7, 2022

Should be almost ready now but I want to add some tests for the RPC view output. I'll first need to work on more urgent things though...

@jakmeier jakmeier marked this pull request as ready for review January 5, 2023 12:33
@jakmeier jakmeier requested a review from a team as a code owner January 5, 2023 12:33
@jakmeier jakmeier requested a review from mm-near January 5, 2023 12:33
@jakmeier
Copy link
Contributor Author

jakmeier commented Jan 5, 2023

This should finally be ready for review.

@nagisa I think I addressed all your review comments on the draft. I would appreciate another review.

cc @Akashin since this affects work on parameter weights

@jakmeier jakmeier requested a review from nagisa January 5, 2023 12:35
@jakmeier
Copy link
Contributor Author

@nagisa do you think you will have time to look at this and are the right person to review it? I can also ask someone else but since you have already looked at an early version once (back in early December) you are the natural candidate.

@nagisa
Copy link
Collaborator

nagisa commented Jan 11, 2023

Aha, thanks for the reminder, I’m looking at it now.

core/primitives-core/src/profile.rs Outdated Show resolved Hide resolved
// Can't `sort_by_key` here because lifetime inference in
// closures is limited.
costs.sort_by(|lhs, rhs| {
lhs.cost_category.cmp(&rhs.cost_category).then(lhs.cost.cmp(&rhs.cost))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect clippy would complain about the then here, possibly saying something about it being executed regardless of whether the first check passed or not. I suspect the compiler optimizes this out well either way, though…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah right, then_with is more appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Track gas spending per parameter
2 participants