linera-protocol icon indicating copy to clipboard operation
linera-protocol copied to clipboard

Maybe implement Merkle Patricia Tree for storing key/values of keys.

Open MathieuDutSik opened this issue 3 months ago • 3 comments

We have in several parts of the code BTreeSet<Vec<u8>> and BTreeMap<Vec<u8>, T>.

However, the keys share a lot of prefixes. Therefore, there is a possibility of improving the code.

MathieuDutSik avatar Sep 17 '25 13:09 MathieuDutSik

The first step would be the change the base key of Linera views to be a sequence of Vec<u8> instead of a sequence of u8. @Twey has similar ideas as well

ma2bd avatar Sep 17 '25 16:09 ma2bd

Why Merkle? Wouldn't a generic radix/patricia trie be good enough?

ndr-ds avatar Sep 17 '25 16:09 ndr-ds

Radix tries have one major limitation: they are inefficient. If you want to store one (path, value) binding where the path, like in Ethereum, is 64 characters long (the number of nibbles in bytes32), we will need over a kilobyte of extra space to store one level per character, and each lookup or delete will take the full 64 steps. The Patricia trie introduced in the following solves this issue.

source: https://ethereum.org/developers/docs/data-structures-and-encoding/patricia-merkle-trie#merkle-patricia-trees

deuszx avatar Sep 18 '25 14:09 deuszx