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

Stateful storage pov optimization #1757

Merged
merged 29 commits into from
Nov 16, 2023
Merged

Conversation

aramikm
Copy link
Collaborator

@aramikm aramikm commented Nov 3, 2023

Goal

The goal of this PR is to evaluate and minimize PoV consumption by stateful-storage pallet.

Closes #1782

Discussion

  • refactored benchmarks to calculate the max of time and PoV
  • decreased additional-trie-layers number
  • decreased MaxItemizedPageSizeBytes from 64KiB to around 10KiB (will allow around 292 of 32 bytes itemized public keys)

Improvements

  • by applying mentioned changes the PoV for apply_item_actions got reduced from 45KB to 15KB
  • by applying mentioned changes the PoV for upsert and delete pages got reduced from 12KB to 6KB

Future improvements

  • by using PoV clawback we can further reduce the PoV sizes. A PR to track

Checklist

  • Benchmarks added
  • Weights updated

Copy link

github-actions bot commented Nov 3, 2023

Running benchmarks and calculating weights. DO NOT MERGE! A new commit will be added upon completion...

Copy link

github-actions bot commented Nov 3, 2023

Running benchmarks and calculating weights. DO NOT MERGE! A new commit will be added upon completion...

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (36c4e4b) 87.62% compared to head (610bff8) 87.60%.

❗ Current head 610bff8 differs from pull request most recent head 64eb8a6. Consider uploading reports for the commit 64eb8a6 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1757      +/-   ##
==========================================
- Coverage   87.62%   87.60%   -0.02%     
==========================================
  Files          52       52              
  Lines        4346     4339       -7     
==========================================
- Hits         3808     3801       -7     
  Misses        538      538              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Nov 3, 2023

Finished running benchmarks. Updated weights have been committed to this PR branch in commit 18b7a8d.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 3, 2023
@github-actions github-actions bot added metadata-changed Metadata has changed since the latest full release and removed metadata-changed Metadata has changed since the latest full release labels Nov 13, 2023
@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Nov 13, 2023
Copy link

Running benchmarks and calculating weights. DO NOT MERGE! A new commit will be added upon completion...

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 13, 2023
@aramikm aramikm requested a review from JoeCap08055 November 13, 2023 23:07
@aramikm aramikm changed the title WIP: Stateful storage pov optimization Stateful storage pov optimization Nov 13, 2023
@aramikm aramikm marked this pull request as ready for review November 13, 2023 23:07
@aramikm aramikm requested a review from wilwade as a code owner November 13, 2023 23:07
@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 13, 2023
Copy link

Running benchmarks and calculating weights. DO NOT MERGE! A new commit will be added upon completion...

@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Nov 14, 2023
Copy link

Finished running benchmarks. Updated weights have been committed to this PR branch in commit 7994714.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 14, 2023
@github-actions github-actions bot added metadata-changed Metadata has changed since the latest full release and removed metadata-changed Metadata has changed since the latest full release labels Nov 14, 2023
Copy link
Collaborator

@JoeCap08055 JoeCap08055 left a comment

Choose a reason for hiding this comment

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

Looks pretty good. A couple of questions:

  • Do we really want to run all benchmarks? Seems like we could omit unrelated benchmark files.
  • Am I reading it wrong, or did some of the weights in stateful-storage increase?

@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Nov 14, 2023
@aramikm
Copy link
Collaborator Author

aramikm commented Nov 14, 2023

Looks pretty good. A couple of questions:

  • Do we really want to run all benchmarks? Seems like we could omit unrelated benchmark files.

Yes we do since changing the --additional-trie-layers to 5 decreases PoV for all pallets across the board

  • Am I reading it wrong, or did some of the weights in stateful-storage increase?

So PoV for all of them should be decreased.
Since the underlying logic is the same the ref-times should not have a significant change. Other than that I improved benchmarks for itemized related extrinsics to be more accurate.

Please let me know which one you think increased more than regular benchmark fluctuations.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 14, 2023
Copy link
Collaborator

@shannonwells shannonwells left a comment

Choose a reason for hiding this comment

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

Non-blocking comments only -

I did not look through all of the weights files but I did look through about a dozen and yes it seems that those changes are within the normal fluctuation of actual weights and error margins, whereas PoV is dramatically reduced. I do wonder about the child trie estimate though, how did you come up with the estimate of 1M child tries and why do you think that is a good estimate?

Is there a drawback of giving enough ceiling for 10M tries i.e. would it be too costly to overestimate the PoV, so does it make more sense to increase only when needed?

@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Nov 15, 2023
@aramikm
Copy link
Collaborator Author

aramikm commented Nov 15, 2023

I did not look through all of the weights files but I did look through about a dozen and yes it seems that those changes are within the normal fluctuation of actual weights and error margins, whereas PoV is dramatically reduced. I do wonder about the child trie estimate though, how did you come up with the estimate of 1M child tries and why do you think that is a good estimate?

The idea is that since child tries are different from regular StorageMaps in the sense that StorageMaps are bottom-heavy. Meaning, the number of nodes from root of the tree to the root of the storage is usually less than the number of nodes between the root of the storage to the last inserted node in map on average (Considering we have a map with around at least 1 million items). In child tries the path from root of the tree to the root of a child trie would require more node traversal compared to the path from root of child trie to a member (Considering each child trie's inserted items are low).
In another words for regular StorageMaps, step # 1 of PoV calculation mentioned in one of comments are much smaller compared to step # 2 but this is not the case for child tries.

Having the above explanation to make sure our PoV calculations are considering this fact we need to ensure that step # 1 of PoV calculations are going to consider the max number of existing child tries for benchmarks. Since on a balanced tree in substrate we will need to access Log16(X) nodes for X inserted items, if we consider having 1_000_000 child tries for benchmarks we will get Log16(1_000_000) ~ 5

Is there a drawback of giving enough ceiling for 10M tries i.e. would it be too costly to overestimate the PoV, so does it make more sense to increase only when needed?

A number of things to consider

  • it would be less efficient, a +1 change in that parameter would increase all PoVs in all pallets by around +600 bytes.
  • We would probably need to only isolate this --additional-trie-layers = 5 or more for only benchmarks running for stateful-storage in future as an optimization. It doesn't need to affect all pallets.
  • This is my current understanding of how everything work based on a number of experiments. To 100% make sure how they handle (or don't handle) child tires we will need to dive deep into the PoV calculation code.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Nov 15, 2023
@aramikm
Copy link
Collaborator Author

aramikm commented Nov 15, 2023

1 Million child trees was a guesstimate based on the number of users on chain. Considering we would need to update our benchmark params every 4-6 months if we reached that point can bump it along with other benchmark calculations like database access benchmarks and etc which should be based on the data inside merkle tree.

@JoeCap08055
Copy link
Collaborator

  • Am I reading it wrong, or did some of the weights in stateful-storage increase?

So PoV for all of them should be decreased. Since the underlying logic is the same the ref-times should not have a significant change. Other than that I improved benchmarks for itemized related extrinsics to be more accurate.

Please let me know which one you think increased more than regular benchmark fluctuations.

OK, looking again it was just some of the reference times that increased, which is probably normal fluctuation. PoVs decreased significantly 👍🏻

@aramikm aramikm merged commit 9691055 into main Nov 16, 2023
28 checks passed
@aramikm aramikm deleted the stateful_storage_pov_optimization branch November 16, 2023 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metadata-changed Metadata has changed since the latest full release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stateful-storage optimized for time and PoV
5 participants