Skip to content

Commit b5e339c

Browse files
authored
perf(core,levm): remove some unnecessary clones and make functions const (#2438)
**Motivation** Increase perfomance, improve code. <!-- Why does this pull request exist? What are its goals? --> **Description** Some methods took Vec by value just to take it's length, requiring a costly clone each time. Some methods could be made const, if the compiler can make use of this it may increase perfomance. Changed a drain to a into_iter, which is simpler and has better perfomance. Aided by the following clippy command: ``` cargo clippy --all-features -- -D clippy::perfomance -D clippy::nursery -A clippy::use_self -A clippy::too_long_first_doc_paragraph -A clippy::derive_partial_eq_without_eq -A clippy::option_if_let_else ```
1 parent 717e3ff commit b5e339c

File tree

13 files changed

+23
-25
lines changed

13 files changed

+23
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### 2025-04-11
66

7+
- Removed some unnecessary clones and made some functions const: [2438](https://github.com/lambdaclass/ethrex/pull/2438)
8+
79
- Asyncify some DB read APIs, as well as its users [#2430](https://github.com/lambdaclass/ethrex/pull/2430)
810

911
### 2025-04-09

crates/common/rlp/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use thiserror::Error;
22

33
// TODO: improve errors
4-
#[derive(Debug, Error, PartialEq)]
4+
#[derive(Debug, Error, PartialEq, Eq)]
55
pub enum RLPDecodeError {
66
#[error("InvalidLength")]
77
InvalidLength,

crates/common/rlp/structs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a> Decoder<'a> {
9595

9696
/// Finishes encoding the struct and returns the remaining bytes after the item.
9797
/// If the item's payload is not empty, returns an error.
98-
pub fn finish(self) -> Result<&'a [u8], RLPDecodeError> {
98+
pub const fn finish(self) -> Result<&'a [u8], RLPDecodeError> {
9999
if self.payload.is_empty() {
100100
Ok(self.remaining)
101101
} else {
@@ -104,13 +104,13 @@ impl<'a> Decoder<'a> {
104104
}
105105

106106
/// Returns true if the decoder has finished decoding the given input
107-
pub fn is_done(&self) -> bool {
107+
pub const fn is_done(&self) -> bool {
108108
self.payload.is_empty()
109109
}
110110

111111
/// Same as [`finish`](Self::finish), but discards the item's remaining payload
112112
/// instead of failing.
113-
pub fn finish_unchecked(self) -> &'a [u8] {
113+
pub const fn finish_unchecked(self) -> &'a [u8] {
114114
self.remaining
115115
}
116116
}

crates/common/trie/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct InMemoryTrieDB {
1717
}
1818

1919
impl InMemoryTrieDB {
20-
pub fn new(map: Arc<Mutex<HashMap<Vec<u8>, Vec<u8>>>>) -> Self {
20+
pub const fn new(map: Arc<Mutex<HashMap<Vec<u8>, Vec<u8>>>>) -> Self {
2121
Self { inner: map }
2222
}
2323
pub fn new_empty() -> Self {

crates/common/trie/nibbles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Nibbles {
1515

1616
impl Nibbles {
1717
/// Create `Nibbles` from hex-encoded nibbles
18-
pub fn from_hex(hex: Vec<u8>) -> Self {
18+
pub const fn from_hex(hex: Vec<u8>) -> Self {
1919
Self { data: hex }
2020
}
2121

crates/common/trie/node/branch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl BranchNode {
4343
}
4444

4545
/// Creates a new branch node given its children and value
46-
pub fn new_with_value(choices: Box<[NodeHash; 16]>, value: ValueRLP) -> Self {
46+
pub const fn new_with_value(choices: Box<[NodeHash; 16]>, value: ValueRLP) -> Self {
4747
Self { choices, value }
4848
}
4949

crates/common/trie/node/extension.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct ExtensionNode {
1818

1919
impl ExtensionNode {
2020
/// Creates a new extension node given its child hash and prefix
21-
pub(crate) fn new(prefix: Nibbles, child: NodeHash) -> Self {
21+
pub(crate) const fn new(prefix: Nibbles, child: NodeHash) -> Self {
2222
Self { prefix, child }
2323
}
2424

@@ -60,8 +60,7 @@ impl ExtensionNode {
6060
let child_node = state
6161
.get_node(self.child)?
6262
.ok_or(TrieError::InconsistentTree)?;
63-
let new_child_node =
64-
child_node.insert(state, path.offset(match_index), value.clone())?;
63+
let new_child_node = child_node.insert(state, path.offset(match_index), value)?;
6564
self.child = new_child_node.insert_self(state)?;
6665
Ok(self.into())
6766
} else if match_index == 0 {

crates/common/trie/node/leaf.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct LeafNode {
1616

1717
impl LeafNode {
1818
/// Creates a new leaf node and stores the given (path, value) pair
19-
pub fn new(partial: Nibbles, value: ValueRLP) -> Self {
19+
pub const fn new(partial: Nibbles, value: ValueRLP) -> Self {
2020
Self { partial, value }
2121
}
2222

@@ -64,7 +64,7 @@ impl LeafNode {
6464
// Create a branch node with self as a child and store the value in the branch node
6565
// Branch { [Self,...], Value }
6666
let mut choices = BranchNode::EMPTY_CHOICES;
67-
choices[self_choice_idx] = self.clone().insert_self(state)?;
67+
choices[self_choice_idx] = self.insert_self(state)?;
6868
BranchNode::new_with_value(Box::new(choices), value)
6969
} else {
7070
// Create a new leaf node and store the path and value in it
@@ -73,7 +73,7 @@ impl LeafNode {
7373
let new_leaf = LeafNode::new(path.offset(match_index + 1), value);
7474
let mut choices = BranchNode::EMPTY_CHOICES;
7575
choices[new_leaf_choice_idx] = new_leaf.insert_self(state)?;
76-
choices[self_choice_idx] = self.clone().insert_self(state)?;
76+
choices[self_choice_idx] = self.insert_self(state)?;
7777
BranchNode::new(Box::new(choices))
7878
};
7979

crates/common/trie/rlp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum NodeType {
1616
}
1717

1818
impl NodeType {
19-
fn from_u8(val: u8) -> Option<Self> {
19+
const fn from_u8(val: u8) -> Option<Self> {
2020
match val {
2121
0 => Some(Self::Branch),
2222
1 => Some(Self::Extension),

crates/common/trie/trie.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ impl Trie {
9090
.flatten()
9191
{
9292
// If the trie is not empty, call the root node's insertion logic
93-
let root_node =
94-
root_node.insert(&mut self.state, Nibbles::from_bytes(&path), value.clone())?;
93+
let root_node = root_node.insert(&mut self.state, Nibbles::from_bytes(&path), value)?;
9594
self.root = Some(root_node.insert_self(&mut self.state)?)
9695
} else {
9796
// If the trie is empty, just add a leaf.
@@ -195,7 +194,7 @@ impl Trie {
195194
// dedup
196195
// TODO: really inefficient, by making the traversing smarter we can avoid having
197196
// duplicates
198-
let node_path: HashSet<_> = node_path.drain(..).collect();
197+
let node_path: HashSet<_> = node_path.into_iter().collect();
199198
let node_path = Vec::from_iter(node_path);
200199
Ok((Some(root_node.encode_raw()), node_path))
201200
}

0 commit comments

Comments
 (0)