From f6895126dd9abcb490410856feb2480889627f76 Mon Sep 17 00:00:00 2001 From: Hai | RISE <150876604+hai-rise@users.noreply.github.com> Date: Fri, 29 Nov 2024 21:40:17 +0700 Subject: [PATCH] perf: remove clone in trie walker (#13004) --- crates/trie/db/tests/walker.rs | 7 ++++--- crates/trie/trie/src/walker.rs | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/trie/db/tests/walker.rs b/crates/trie/db/tests/walker.rs index 06355ff6d4..0e0b094920 100644 --- a/crates/trie/db/tests/walker.rs +++ b/crates/trie/db/tests/walker.rs @@ -63,13 +63,14 @@ where // We're traversing the path in lexicographical order. for expected in expected { - let got = walker.advance().unwrap(); + walker.advance().unwrap(); + let got = walker.key().cloned(); assert_eq!(got.unwrap(), Nibbles::from_nibbles_unchecked(expected.clone())); } // There should be 8 paths traversed in total from 3 branches. - let got = walker.advance().unwrap(); - assert!(got.is_none()); + walker.advance().unwrap(); + assert!(walker.key().is_none()); } #[test] diff --git a/crates/trie/trie/src/walker.rs b/crates/trie/trie/src/walker.rs index d1c5247966..647c1486ef 100644 --- a/crates/trie/trie/src/walker.rs +++ b/crates/trie/trie/src/walker.rs @@ -145,11 +145,12 @@ impl TrieWalker { } /// Advances the walker to the next trie node and updates the skip node flag. + /// The new key can then be obtained via `key()`. /// /// # Returns /// - /// * `Result, Error>` - The next key in the trie or an error. - pub fn advance(&mut self) -> Result, DatabaseError> { + /// * `Result<(), Error>` - Unit on success or an error. + pub fn advance(&mut self) -> Result<(), DatabaseError> { if let Some(last) = self.stack.last() { if !self.can_skip_current_node && self.children_are_in_trie() { // If we can't skip the current node and the children are in the trie, @@ -167,8 +168,7 @@ impl TrieWalker { self.update_skip_node(); } - // Return the current key. - Ok(self.key().cloned()) + Ok(()) } /// Retrieves the current root node from the DB, seeking either the exact node or the next one.