Avoid looking up previous version of nodes that are new #331
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
When writing tree nodes to storage, we look up previous versions of the node to create the
TreeNodeWithPreviousValue
struct. However, this is not necessary when we know a node is new and does not have a previous value. By keeping track of whether a node is new, we save on incurring an unnecessary round trip to the database.This PR performs the requisite change.
I would have liked to add a test for this change, however I cannot think of a straightforward way to assert that the database is not read beyond the initial preload. I believe this is something that can be addressed by bringing in a mocking library to mock out the
Database
trait, or implementing an in-memory database which we can mock. Filed an issue regarding this: #332Manual Test
Added the following test to
append_only_zks.rs
, which batch inserts 10 nodes on an empty tree and prints database access metrics:The test was ran with the following command:
$ cargo test test_get_count -- --nocapture
If the change works, no gets are expected as the insert was performed on an empty tree with nothing to preload.
Before
On the
main
branch, with 7878bcb cherry-picked:19 gets are performed.
After
On this PR's branch:
Indeed the number of gets are now 0.