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

Improve TrieCache size by reducing size_of NodeOwned #216

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

alexggh
Copy link

@alexggh alexggh commented Feb 10, 2025

NodeOwned uses static arrays to represent the children of a NodeOwned::Branch and NodeOwned::NibbledBranch, because the enum's size is the size of largest variant it ends up occupying 752 bytes.

The substrate ShareTrieCache ends up keeping a hash map with elements of NodeOwned, so it's size quickly balloons, although most of the children seem to be empty.

The trie cache size benefits greatly from using a dynamic array instead of static one: paritytech/polkadot-sdk#6131 (comment), using westend-asset-hub, we can see a ~4 times reduction in memory size for the TrieCache.

Fixes: paritytech/polkadot-sdk#7386

Signed-off-by: Alexandru Gheorghe <[email protected]>
@alexggh alexggh requested a review from bkchr February 10, 2025 13:55
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.

Nice work 👍

I just want some improvements and then we almost need no changes.

trie-db/src/node.rs Outdated Show resolved Hide resolved
trie-db/src/node.rs Outdated Show resolved Hide resolved
trie-db/src/node.rs Outdated Show resolved Hide resolved
trie-db/src/node.rs Outdated Show resolved Hide resolved
trie-db/src/node.rs Outdated Show resolved Hide resolved
trie-db/src/node.rs Outdated Show resolved Hide resolved
Signed-off-by: Alexandru Gheorghe <[email protected]>
@alexggh
Copy link
Author

alexggh commented Feb 11, 2025

Nice work 👍

I just want some improvements and then we almost need no changes.

Done, thank you!

Signed-off-by: Alexandru Gheorghe <[email protected]>
@alexggh alexggh requested a review from cheme February 12, 2025 09:02
/// Returns the number of children.
pub fn len(&self) -> usize {
self.0.len()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Might be good to have an is_empty for a complete API if we are exposing this to the user (https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty)

Copy link
Contributor

@lexnv lexnv left a comment

Choose a reason for hiding this comment

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

Nicely done! 4x reduction is amazing 🚀

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.

Triecache: Optimize size occupied by SharedNodeCacheMap
4 participants