From e198a38d628bdb60650f75d2a2a5724bbb543fe0 Mon Sep 17 00:00:00 2001 From: malik Date: Mon, 20 Oct 2025 16:04:31 +0100 Subject: [PATCH] perf: batch byte for serialization (#19096) Co-authored-by: Matthias Seitz --- Cargo.lock | 1 + Cargo.toml | 1 + crates/trie/common/Cargo.toml | 5 ++++- crates/trie/common/src/nibbles.rs | 21 ++++++++++----------- crates/trie/common/src/storage.rs | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73f63b42f1..228c078305 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10683,6 +10683,7 @@ dependencies = [ "alloy-serde", "alloy-trie", "arbitrary", + "arrayvec", "bincode 1.3.3", "bytes", "codspeed-criterion-compat", diff --git a/Cargo.toml b/Cargo.toml index 414e387ee2..0804101564 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -534,6 +534,7 @@ op-alloy-flz = { version = "0.13.1", default-features = false } # misc either = { version = "1.15.0", default-features = false } +arrayvec = { version = "0.7.6", default-features = false } aquamarine = "0.6" auto_impl = "1" backon = { version = "1.2", default-features = false, features = ["std-blocking-sleep", "tokio-sleep"] } diff --git a/crates/trie/common/Cargo.toml b/crates/trie/common/Cargo.toml index 0aa93adb59..f10e53a838 100644 --- a/crates/trie/common/Cargo.toml +++ b/crates/trie/common/Cargo.toml @@ -23,6 +23,7 @@ reth-codecs = { workspace = true, optional = true } alloy-rpc-types-eth = { workspace = true, optional = true } alloy-serde = { workspace = true, optional = true } +arrayvec = { workspace = true, optional = true } bytes = { workspace = true, optional = true } derive_more.workspace = true itertools = { workspace = true, features = ["use_alloc"] } @@ -83,10 +84,12 @@ std = [ "serde_json/std", "revm-database/std", "revm-state/std", + "arrayvec?/std", ] eip1186 = ["alloy-rpc-types-eth/serde", "dep:alloy-serde"] serde = [ "dep:serde", + "arrayvec?/serde", "bytes?/serde", "nybbles/serde", "alloy-primitives/serde", @@ -98,7 +101,7 @@ serde = [ "revm-database/serde", "revm-state/serde", ] -reth-codec = ["dep:reth-codecs", "dep:bytes"] +reth-codec = ["dep:reth-codecs", "dep:bytes", "dep:arrayvec"] serde-bincode-compat = [ "serde", "reth-primitives-traits/serde-bincode-compat", diff --git a/crates/trie/common/src/nibbles.rs b/crates/trie/common/src/nibbles.rs index 7d9e6670be..82d710395f 100644 --- a/crates/trie/common/src/nibbles.rs +++ b/crates/trie/common/src/nibbles.rs @@ -28,10 +28,9 @@ impl reth_codecs::Compact for StoredNibbles { where B: bytes::BufMut + AsMut<[u8]>, { - for i in self.0.iter() { - buf.put_u8(i); - } - self.0.len() + let bytes = self.0.iter().collect::>(); + buf.put_slice(&bytes); + bytes.len() } fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) { @@ -78,14 +77,14 @@ impl reth_codecs::Compact for StoredNibblesSubKey { { assert!(self.0.len() <= 64); - // right-pad with zeros - for i in self.0.iter() { - buf.put_u8(i); - } - static ZERO: &[u8; 64] = &[0; 64]; - buf.put_slice(&ZERO[self.0.len()..]); + let bytes = self.0.iter().collect::>(); + buf.put_slice(&bytes); - buf.put_u8(self.0.len() as u8); + // Right-pad with zeros + static ZERO: &[u8; 64] = &[0; 64]; + buf.put_slice(&ZERO[bytes.len()..]); + + buf.put_u8(bytes.len() as u8); 64 + 1 } diff --git a/crates/trie/common/src/storage.rs b/crates/trie/common/src/storage.rs index 557b9e4a60..1e56739386 100644 --- a/crates/trie/common/src/storage.rs +++ b/crates/trie/common/src/storage.rs @@ -1,4 +1,4 @@ -use super::{BranchNodeCompact, Nibbles, StoredNibblesSubKey}; +use super::{BranchNodeCompact, StoredNibblesSubKey}; /// Account storage trie node. /// @@ -61,7 +61,7 @@ impl reth_codecs::Compact for TrieChangeSetsEntry { if len == 0 { // Return an empty entry without trying to parse anything return ( - Self { nibbles: StoredNibblesSubKey::from(Nibbles::default()), node: None }, + Self { nibbles: StoredNibblesSubKey::from(super::Nibbles::default()), node: None }, buf, ) }