Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ gum-proc-macro = { path = "polkadot/node/gum/proc-macro", default-features = fal
handlebars = { version = "5.1.0" }
hash-db = { version = "0.16.0", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
hashbrown = "0.15.3"
hex = { version = "0.4.3", default-features = false }
hex-literal = { version = "0.4.1", default-features = false }
hkdf = { version = "0.12.0" }
Expand Down
1 change: 1 addition & 0 deletions cumulus/pallets/parachain-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
bytes = { workspace = true }
codec = { features = ["derive"], workspace = true }
environmental = { workspace = true }
hashbrown = { workspace = true }
impl-trait-for-tuples = { workspace = true }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use alloc::{
boxed::Box,
collections::btree_map::{BTreeMap, Entry},
};
use alloc::boxed::Box;

use core::cell::{RefCell, RefMut};
use hashbrown::{hash_map::Entry, HashMap};
use sp_state_machine::TrieCacheProvider;
use sp_trie::NodeCodec;
use trie_db::{node::NodeOwned, Hasher};
Expand All @@ -28,8 +27,8 @@ use trie_db::{node::NodeOwned, Hasher};
/// of values. To be used in `validate_block` to serve values and nodes that
/// have already been loaded and decoded from the storage proof.
pub struct TrieCache<'a, H: Hasher> {
node_cache: RefMut<'a, BTreeMap<H::Out, NodeOwned<H::Out>>>,
value_cache: Option<RefMut<'a, BTreeMap<Box<[u8]>, trie_db::CachedValue<H::Out>>>>,
node_cache: RefMut<'a, HashMap<H::Out, NodeOwned<H::Out>>>,
value_cache: Option<RefMut<'a, HashMap<Box<[u8]>, trie_db::CachedValue<H::Out>>>>,
}

impl<'a, H: Hasher> trie_db::TrieCache<NodeCodec<H>> for TrieCache<'a, H> {
Expand Down Expand Up @@ -66,14 +65,14 @@ impl<'a, H: Hasher> trie_db::TrieCache<NodeCodec<H>> for TrieCache<'a, H> {

/// Provider of [`TrieCache`] instances.
pub struct CacheProvider<H: Hasher> {
node_cache: RefCell<BTreeMap<H::Out, NodeOwned<H::Out>>>,
node_cache: RefCell<HashMap<H::Out, NodeOwned<H::Out>>>,
/// Cache: `storage_root` => `storage_key` => `value`.
///
/// One `block` can for example use multiple tries (child tries) and we need to distinguish the
/// cached (`storage_key`, `value`) between them. For this we are using the `storage_root` to
/// distinguish them (even if the storage root is the same for two child tries, it just means
/// that both are exactly the same trie and there would happen no collision).
value_cache: RefCell<BTreeMap<H::Out, BTreeMap<Box<[u8]>, trie_db::CachedValue<H::Out>>>>,
value_cache: RefCell<HashMap<H::Out, HashMap<Box<[u8]>, trie_db::CachedValue<H::Out>>>>,
}

impl<H: Hasher> CacheProvider<H> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@

use codec::Encode;

use alloc::{
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
rc::Rc,
};
use alloc::rc::Rc;

use core::cell::{RefCell, RefMut};
use hashbrown::{HashMap, HashSet};
use sp_trie::{NodeCodec, ProofSizeProvider, StorageProof};
use trie_db::{Hasher, RecordedForKey, TrieAccess};

Expand All @@ -35,9 +34,9 @@ use trie_db::{Hasher, RecordedForKey, TrieAccess};
/// The internal size counting logic should align
/// with ['sp_trie::recorder::Recorder'].
pub struct SizeOnlyRecorder<'a, H: Hasher> {
seen_nodes: RefMut<'a, BTreeSet<H::Out>>,
seen_nodes: RefMut<'a, HashSet<H::Out>>,
encoded_size: RefMut<'a, usize>,
recorded_keys: RefMut<'a, BTreeMap<Rc<[u8]>, RecordedForKey>>,
recorded_keys: RefMut<'a, HashMap<Rc<[u8]>, RecordedForKey>>,
}

impl<'a, H: trie_db::Hasher> trie_db::TrieRecorder<H::Out> for SizeOnlyRecorder<'a, H> {
Expand Down Expand Up @@ -91,9 +90,9 @@ impl<'a, H: trie_db::Hasher> trie_db::TrieRecorder<H::Out> for SizeOnlyRecorder<

#[derive(Clone)]
pub struct SizeOnlyRecorderProvider<H: Hasher> {
seen_nodes: Rc<RefCell<BTreeSet<H::Out>>>,
seen_nodes: Rc<RefCell<HashSet<H::Out>>>,
encoded_size: Rc<RefCell<usize>>,
recorded_keys: Rc<RefCell<BTreeMap<Rc<[u8]>, RecordedForKey>>>,
recorded_keys: Rc<RefCell<HashMap<Rc<[u8]>, RecordedForKey>>>,
}

impl<H: Hasher> Default for SizeOnlyRecorderProvider<H> {
Expand Down
10 changes: 10 additions & 0 deletions prdoc/pr_8606.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Use hashbrown hashmap/hashset in validation context
doc:
- audience: Node Dev
description: |-
Discovered while profiling https://github.com/paritytech/polkadot-sdk/issues/6131#issuecomment-2891523233 with the benchmark https://github.com/paritytech/polkadot-sdk/pull/8069 that when running in validation a big chunk of the time is spent inserting and retrieving data from the BTreeMap/BTreeSet.

By switching to hashbrown HashMap/HashSet in validation TrieCache and TrieRecorder and the memory-db https://github.com/paritytech/trie/pull/221 read costs improve with around ~40% and write with about ~20%
crates:
- name: cumulus-pallet-parachain-system
bump: minor