Compare commits

...

1 Commits

Author SHA1 Message Date
Georgios Konstantopoulos
f9f24d7d7d perf(trie): fix allocation hot paths with capacity hints and buffer reuse
- Replace clone() with mem::take pattern in update_account and
  update_account_storage_root to avoid buffer cloning on every account update
- Add capacity hints for child_stack, retained_proofs, and rlp_nodes_bufs
  in ProofCalculator to reduce Vec reallocation overhead

Amp-Thread-ID: https://ampcode.com/threads/T-019bfe25-43f3-75ac-98f7-32bf937b69e1
Co-authored-by: Amp <amp@ampcode.com>
2026-01-27 07:03:33 +00:00
2 changed files with 15 additions and 9 deletions

View File

@@ -884,9 +884,12 @@ where
trace!(target: "trie::sparse", ?address, "Updating account");
let nibbles = Nibbles::unpack(address);
self.account_rlp_buf.clear();
account.into_trie_account(storage_root).encode(&mut self.account_rlp_buf);
self.update_account_leaf(nibbles, self.account_rlp_buf.clone(), provider_factory)?;
let mut rlp_buf = core::mem::take(&mut self.account_rlp_buf);
rlp_buf.clear();
account.into_trie_account(storage_root).encode(&mut rlp_buf);
let result = self.update_account_leaf(nibbles, rlp_buf, provider_factory);
self.account_rlp_buf = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
result?;
Ok(true)
}
@@ -937,9 +940,12 @@ where
// Otherwise, update the account leaf.
trace!(target: "trie::sparse", ?address, "Updating account with the new storage root");
let nibbles = Nibbles::unpack(address);
self.account_rlp_buf.clear();
trie_account.encode(&mut self.account_rlp_buf);
self.update_account_leaf(nibbles, self.account_rlp_buf.clone(), provider_factory)?;
let mut rlp_buf = core::mem::take(&mut self.account_rlp_buf);
rlp_buf.clear();
trie_account.encode(&mut rlp_buf);
let result = self.update_account_leaf(nibbles, rlp_buf, provider_factory);
self.account_rlp_buf = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
result?;
Ok(true)
}

View File

@@ -105,10 +105,10 @@ impl<TC, HC, VE: LeafValueEncoder> ProofCalculator<TC, HC, VE> {
hashed_cursor,
branch_stack: Vec::<_>::with_capacity(64),
branch_path: Nibbles::new(),
child_stack: Vec::<_>::new(),
child_stack: Vec::<_>::with_capacity(64),
cached_branch_stack: Vec::<_>::with_capacity(64),
retained_proofs: Vec::<_>::new(),
rlp_nodes_bufs: Vec::<_>::new(),
retained_proofs: Vec::<_>::with_capacity(64),
rlp_nodes_bufs: Vec::<_>::with_capacity(8),
rlp_encode_buf: Vec::<_>::with_capacity(RLP_ENCODE_BUF_SIZE),
}
}