-
Notifications
You must be signed in to change notification settings - Fork 38
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
refactor: simplify mpt_trie's API #400
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, overall looking good to me. Mostly nit comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Aatif!
We may want another pair of eyes on the refactor (perhaps @BGluth given the library is his work), but otherwise LGTM.
Hmm... So I get what you're trying to do, but we're throwing away one important optimization with this PR. With zk_evm/trace_decoder/src/decoding.rs Lines 227 to 231 in 8b1a1b4
zk_evm/trace_decoder/src/decoding.rs Lines 435 to 451 in 8b1a1b4
Also if we ever call I personally would not go with the freeze API change and keep the existing setup (maybe without with |
Thanks for your review @BGluth :) I don't quite follow the claim of optimization loss though - could you help me understand? AIUI,
Contain the exact same number of calls to Am I missing something? |
I see that this causes duplicate work, but I think the previous implementation did this too - I feel positive about a |
So the number of calls to With the old setup, every node actually does cache it's own hash. It's actually not obvious when you glance at the code since the definition of zk_evm/mpt_trie/src/partial_trie.rs Lines 300 to 307 in 3336055
The idea here is technically a single This means that two different tries can share a common child. Like consider this:
Where the extension node has a very dense sub-trie beneath it. If we perform an insert that converts the extension node into a branch node:
Then both the extension node and branch node will contain the same child node sub-trie (like in terms of the same piece of memory). If the entire child sub-trie was cached, then they will both have access to the already cached hashes. Also, I'm fine if you want to remove |
This comment has been minimized.
This comment has been minimized.
Ah I totally missed that! I'll refactor accordingly :) thanks for the patient explanation :) As for |
@@ -1,126 +1,37 @@ | |||
//! Definitions for the core types [`PartialTrie`] and [`Nibbles`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are few places to update comments from PartialTrie
to Node
where | ||
K: Into<Nibbles>, | ||
{ | ||
TriePathIter { | ||
curr_node: trie.clone().into(), | ||
curr_node: Arc::new(trie.clone()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: Could we skip cloning here and work with references?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit late, but yeah tries in mpt_trie
are all essentially references, so cloning is very cheap.
I have skimmed through the PR, seems fine to me. It does simplify navigating the tries implementation. |
@0xaatif Yeah sounds good! Feel free to remove |
trait PartialTrie
abstracts over a fully hydrated trieStandardTrie
, and a partial trieHashedPartialTrie
:zk_evm/mpt_trie/src/partial_trie.rs
Lines 19 to 30 in 25a8145
However, our code only uses
HashedPartialTrie
- we can simplify our API, which will make moving towards a unified backend for SMT and MPT easier (#275)Changes
StandardTrie
.trait PartialTrie
,HashedPartialTrie
, and replace them with a top levelmpt_trie::Node
.Arc<Box<Node>>
->Arc<Node>
.FrozenNode
to handle hash caching, instead ofRwLock
-ing onHashedPartialTrie
.It turns out that
evm_arithmetization
only usesFrozenNode
, which is a good thing for me to know :)