Skip to content

perf(core): make TrieDb use NodeHash as key #2517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Perf

### 2025-04-28

- Make TrieDb trait use NodeHash as key [2517](https://github.com/lambdaclass/ethrex/pull/2517)

### 2025-04-22

- Avoid calculating state transitions after every block in bulk mode [2519](https://github.com/lambdaclass/ethrex/pull/2519)
Expand Down
179 changes: 179 additions & 0 deletions Cargo.lock

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

18 changes: 9 additions & 9 deletions crates/common/trie/db.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::error::TrieError;
use crate::{error::TrieError, NodeHash};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

pub trait TrieDB: Send + Sync {
fn get(&self, key: Vec<u8>) -> Result<Option<Vec<u8>>, TrieError>;
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), TrieError>;
fn get(&self, key: NodeHash) -> Result<Option<Vec<u8>>, TrieError>;
fn put(&self, key: NodeHash, value: Vec<u8>) -> Result<(), TrieError>;
// fn put_batch(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), TrieError>;
fn put_batch(&self, key_values: Vec<(Vec<u8>, Vec<u8>)>) -> Result<(), TrieError>;
fn put_batch(&self, key_values: Vec<(NodeHash, Vec<u8>)>) -> Result<(), TrieError>;
}

/// InMemory implementation for the TrieDB trait, with get and put operations.
pub struct InMemoryTrieDB {
inner: Arc<Mutex<HashMap<Vec<u8>, Vec<u8>>>>,
inner: Arc<Mutex<HashMap<NodeHash, Vec<u8>>>>,
}

impl InMemoryTrieDB {
pub const fn new(map: Arc<Mutex<HashMap<Vec<u8>, Vec<u8>>>>) -> Self {
pub const fn new(map: Arc<Mutex<HashMap<NodeHash, Vec<u8>>>>) -> Self {
Self { inner: map }
}
pub fn new_empty() -> Self {
Expand All @@ -28,7 +28,7 @@ impl InMemoryTrieDB {
}

impl TrieDB for InMemoryTrieDB {
fn get(&self, key: Vec<u8>) -> Result<Option<Vec<u8>>, TrieError> {
fn get(&self, key: NodeHash) -> Result<Option<Vec<u8>>, TrieError> {
Ok(self
.inner
.lock()
Expand All @@ -37,15 +37,15 @@ impl TrieDB for InMemoryTrieDB {
.cloned())
}

fn put(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), TrieError> {
fn put(&self, key: NodeHash, value: Vec<u8>) -> Result<(), TrieError> {
self.inner
.lock()
.map_err(|_| TrieError::LockError)?
.insert(key, value);
Ok(())
}

fn put_batch(&self, key_values: Vec<(Vec<u8>, Vec<u8>)>) -> Result<(), TrieError> {
fn put_batch(&self, key_values: Vec<(NodeHash, Vec<u8>)>) -> Result<(), TrieError> {
let mut db = self.inner.lock().map_err(|_| TrieError::LockError)?;

for (key, value) in key_values {
Expand Down
14 changes: 14 additions & 0 deletions crates/common/trie/node_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ impl NodeHash {
}
encoder
}

pub fn len(&self) -> usize {
match self {
NodeHash::Hashed(h256) => h256.as_bytes().len(),
NodeHash::Inline(value) => value.1 as usize,
}
}

pub fn is_empty(&self) -> bool {
match self {
NodeHash::Hashed(h256) => h256.as_bytes().is_empty(),
NodeHash::Inline(value) => value.1 == 0,
}
}
}

impl From<Vec<u8>> for NodeHash {
Expand Down
Loading
Loading