optimize get_proof_positions to not use sort #66
Merged
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.
Calling sort on every iteration for a method that's called at least once every block is a huge perf hit. This commit rework
get_proof_positions
to work without sorting.The reason why we sort is that we always work upwards in the tree, starting at row 0 and going all the way to the top-most root. We always need a node and it's sibling (either from the proof or computed as we go). Searching for a sibling is also incredibly slow, given that we might have up to hundreds of thousands of nodes. We want to have siblings adjacent to each other inside our container. If all leaves were at row 0, that would always happen. But since leaves may not be at row 0, it is possible to have siblings being scattered around. To fix this, we might just sort the vector, siblings are numerically adjacent. However, sorting a few thousand nodes is also expensive.
This commit uses a BTreeMap instead of a vector. We trade-off some small performance on searching, but now we can add stuff in a self-sorting way that's much lighter.