Merkle nodes with even-length paths made shorter #354
Closed
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.
This PR amends the way
Leaf
s of Merkle trie with even paths are stored, so that if the path starts with a nibble in form of0b11__
it will be one byte shorter to store. As0b11
is a one of four prefixes, with a uniform distribution 1/4 of even paths should require one byte less to store.The optimization uses the fact that
Type.Leaf
is encoded in first byte as0b1000_0000
. This plus the fact that other higher bits are not used by other cases, allows to use0b11
as a discriminator of a leaf that has some property. In that case it's the property that the path starts with0b11
which allows us to not store any additional data beside the leaf'sNibblePath
. The leaf type is encoded in the path itself.Benchmark
Importing
Sepolia
db.Before
After