Skip to content
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

hexary trie get operation optimization #234

Open
jangko opened this issue May 6, 2020 · 1 comment
Open

hexary trie get operation optimization #234

jangko opened this issue May 6, 2020 · 1 comment

Comments

@jangko
Copy link
Contributor

jangko commented May 6, 2020

Something I've learned after implementing multiproof block witness.

Current implementation require the key converted to nibblesSeq with the role of a path.
Every time the algorithm move deeper into trie structure, it will create a slice of this path.
But the slice is actually a copy.

In SecureHexaryTrie, the key is the keccak hash value of the original key. The hash is 32 bytes or 64 nibbles. In a dense branch, the maximum trie depth at worst is 64. It also means there is possibility to make the copy of path 63 times at the deepest end of the trie, wasting memory and time.

When implementing multiproof, I avoid this kind of copy and conversion of path to NibblesSeq by adding an extra parameter depth to the recursive proc param. In multiproof algorithm we are dealing with multiple keys at once, not only one like in hexary trie.

If we encounter a branch node, the depth will increase one at the next recursion. If we encounter an extension node, we will increase the depth with the length of nibbles prefix from extension node.

This depth parameter will act as a starting-cursor for the immutable path when doing nibble(s) comparison.

Beside adding depth parameter, the nibbles comparison also need modification, not comparing NibblesSeq vs NibblesSeq anymore but openArray[byte] vs openArray[byte].

we could throw away NibblesSeq from get operation.

I have not look closer into the put operation, whether we can throw away NibblesSeq or not.

@zah
Copy link
Contributor

zah commented May 7, 2020

The old implementation of the Nimble range was assuming that slices don't produce a copy, but this was changed recently. Nevertheless, keeping the key as a fixed-sized stack variable is certainly better. If you wish, you may try the BitArray type in nim-stew to keep the keys smaller, but it's probably not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants