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

Use trie-cache for validate_block #2813

Merged
merged 30 commits into from
Jul 19, 2023

Conversation

skunert
Copy link
Contributor

@skunert skunert commented Jul 3, 2023

Follow up of paritytech/substrate#14403
Closes #2432

This PR adds a simple trie-cache to the validate_block function. In contrast to the trie-cache in substrate there is only a single value and a single node cache here, as proposed in #2432.

The ReadOnceBackend is not included, I think we can be conservative and just have the cache for now.

Performance improvements

Benchmark master this PR change
validate_block 314.42 ms 281.20 ms -10.594%
validate_block_glutton (compute = 1, storage = 0) 352.33 ms 353.21 ms +0.1847%
validate_block_glutton (compute = 1, storage = 1) 366.24 ms 365.13 ms -0.2676%

For the validate_block benchmark, we see a roughly 10% improvement. The benchmark using glutton is not benefiting from the cache at all, since no values are accessed twice.

@skunert skunert added B0-silent Changes should not be mentioned in any release notes A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. labels Jul 3, 2023
@skunert skunert requested review from bkchr and a team July 3, 2023 16:49
pallets/parachain-system/src/validate_block/trie_cache.rs Outdated Show resolved Hide resolved
pallets/parachain-system/src/validate_block/trie_cache.rs Outdated Show resolved Hide resolved
pallets/parachain-system/src/validate_block/trie_cache.rs Outdated Show resolved Hide resolved
pallets/parachain-system/src/validate_block/trie_cache.rs Outdated Show resolved Hide resolved
pallets/parachain-system/src/validate_block/trie_cache.rs Outdated Show resolved Hide resolved
Comment on lines 98 to 103
fn as_trie_db_mut_cache(&self) -> Self::Cache<'_> {
SimpleTrieCache {
value_cache: self.value_cache.borrow_mut(),
node_cache: self.node_cache.borrow_mut(),
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This function is called when we call storage_root. The values that are being created while calculating the storage_root should not be added to the value_cache as otherwise they may would overwrite some cached value from the "real trie" (we only distinguish using the key and the key stays the same in each trie). If another operation then would want to fetch this overwritten value, it would get some invalid data.

The nodes are accessed by their hash, so that should be no problem. However, the question in general is if we even need to cache these nodes. These nodes will never be accessible from the "storage root" we are operating on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The value cache is now disabled when building storage roots. Due to how get_or_insert_node works I think it does still make sense to take ownership of the fetched node and cache it.

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Anton <anton.kalyaev@gmail.com>
@skunert
Copy link
Contributor Author

skunert commented Jul 19, 2023

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot paritytech-processbot bot merged commit 5386d18 into paritytech:master Jul 19, 2023
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders.
Projects
Status: done
Development

Successfully merging this pull request may close these issues.

Implement TrieCache for validate_block
4 participants