Dynamic size optimization #24
Open
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.
Rationale
Binary deposit tree currently has a fixed depth of 40 levels. This means that on any new deposit creation or
withdrawal there happens at least 40 storage writings, which update all tree nodes from the deposit leaf to
the root. The total number of possible deposits is 2^40, which was reserved for the least possible chance of
overflow, but seems to not be needed for a meaningful amount of time, so the most nodes of the tree remain
unused. Currently in the protocol there are ~10k deposits, which corresponds to ~0.0000009% of deposit tree
used. The possibility of optimizing the tree and reducing storage writings may be desirable.
Description
The suggested optimization is to have a minimal tree size from the beginning and to double it gradually,
when it's needed.
For example, with 5 deposits it's needed to have an 8-leaf tree, which has depth 3 (+1 root). It will spend only
4 storage writings on deposit and withdraw.
For N deposits it's needed to have a tree with
[log2(N)] + 1
levels and2^[log2(N)]
leaves, where[x]
means
ceil(x)
. For example, the tree with 10000 deposits will have a depth of 15 andleaf number of 16384, which means 15 storage writing for deposit and withdraw, and it is ~3 times lower than
current gas spending.
The approach of implementation is having initial
root
value equal toLIQUIDITYNODES
. It willcorrespond to tree with 1 leaf - the root. All the other nodes are considered as "out of the tree".
The next step, when the number of deposits exceeds 1, is to double the tree, dividing the root
root /= 2
, soit opens the tree with 2 leaves - [LIQUIDITYNODES, LIQUIDITYNODES + 1]. The new root is initialized with the old value root amount to ensure consistency. The higher numbers of leaves
are considered invalid aswell. The next doubling will be after 2 deposits, then 4, 8 and so on, increasing the tree gradually.
Existing operations for the tree are just diving from the leaf until
the
root
, and updating root happens when a number of new deposits is exceeded. No other change to the algorithm is done.Result
The tested gas comsumption for most functions has significantly decreased (upper values are before, lower are after):