perf: misc changes (#5701)

This commit is contained in:
DaniPopes
2023-12-05 20:13:49 +01:00
committed by GitHub
parent b0c4d99cac
commit 926766d482
4 changed files with 97 additions and 51 deletions

View File

@ -203,20 +203,24 @@ impl HashBuilder {
trace!(target: "trie::hash_builder", ?current, ?succeeding, "updating merkle tree");
let mut i = 0;
let mut i = 0usize;
let span = tracing::trace_span!(
target: "trie::hash_builder",
"loop",
i = tracing::field::Empty,
current = tracing::field::Empty,
build_extensions = tracing::field::Empty,
)
.entered();
loop {
let span = tracing::span!(
target: "trie::hash_builder",
tracing::Level::TRACE,
"loop",
i,
?current,
?build_extensions
);
let _enter = span.enter();
if !span.is_disabled() {
span.record("i", i);
span.record("current", &format!("{current:?}"));
span.record("build_extensions", build_extensions);
}
let preceding_exists = !self.groups.is_empty();
let preceding_len: usize = self.groups.len().saturating_sub(1);
let preceding_len = self.groups.len().saturating_sub(1);
let common_prefix_len = succeeding.common_prefix_length(current.as_slice());
let len = std::cmp::max(preceding_len, common_prefix_len);
@ -254,7 +258,7 @@ impl HashBuilder {
if !succeeding.is_empty() || preceding_exists {
len_from += 1;
}
trace!(target: "trie::hash_builder", "skipping {} nibbles", len_from);
trace!(target: "trie::hash_builder", "skipping {len_from} nibbles");
// The key without the common prefix
let short_node_key = current.slice(len_from..);

View File

@ -67,7 +67,6 @@ impl Compact for StoredNibblesSubKey {
/// The internal representation is a [`SmallVec`] that stores one nibble per byte. This means that
/// each byte has its upper 4 bits set to zero and the lower 4 bits representing the nibble value.
#[derive(
Clone,
Default,
PartialEq,
Eq,
@ -83,6 +82,19 @@ impl Compact for StoredNibblesSubKey {
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
pub struct Nibbles(SmallVec<[u8; 64]>);
// Override `SmallVec::from` since it's not specialized for `Copy` types.
impl Clone for Nibbles {
#[inline]
fn clone(&self) -> Self {
Self(SmallVec::from_slice(&self.0))
}
#[inline]
fn clone_from(&mut self, source: &Self) {
self.0.clone_from(&source.0);
}
}
impl alloy_rlp::Encodable for Nibbles {
#[inline]
fn length(&self) -> usize {

View File

@ -28,5 +28,9 @@ fn rlp_node(rlp: &[u8]) -> Vec<u8> {
// TODO: this could return [u8; 33] but Vec is needed everywhere this function is used
#[inline]
pub fn word_rlp(word: &B256) -> Vec<u8> {
[&[EMPTY_STRING_CODE + B256::len_bytes() as u8][..], &word[..]].concat()
// Gets optimized to alloc + write directly into it: https://godbolt.org/z/rfWGG6ebq
let mut arr = [0; 33];
arr[0] = EMPTY_STRING_CODE + 32;
arr[1..].copy_from_slice(word.as_slice());
arr.to_vec()
}