solana-labs / solana

Web-Scale Blockchain for fast, secure, scalable, decentralized apps and marketplaces.
https://solanalabs.com
Apache License 2.0
12.89k stars 4.11k forks source link

Optimize Merkle Tree Creation #32622

Open ryleung-solana opened 1 year ago

ryleung-solana commented 1 year ago

Problem

Jump have developed an optimized function for inserting nodes into a Merkle tree (https://github.com/firedancer-io/firedancer/blob/31d260e06a55a7c6ef40f393fb94372756a625d4/src/ballet/bmtree/fd_bmtree_tmpl.c#L396-L451), which might help speed up Merklization.

Proposed Solution

Port this function to Rust, find appropriate use cases and bench.

ryleung-solana commented 1 year ago

@t-nelson for further details on use cases.

ryleung-solana commented 1 year ago

CC @t-nelson based on discussions we've concluded we want to exploit Jump's O(1) space implementation to lower memory consumption and make it more predictable.

t-nelson commented 1 year ago

correct. that's the goal for the places where the only output we care about is the root

ryleung-solana commented 11 months ago

Update: testing on mainnet has shown no improvement in memory usage, and actually it seems to have actually worsened memory consumption (left side of the screenshot is with the change, right side is without - tested against v1.16). This is possibly due to the fact that the constant memory used for calculating the merkle root is higher than the average memory consumed from just building the merkle tree for most data sets seen in practice. It may be possible to if we wanted, to drill a little deeper, but given that mem usage is constant, the only real prospect I can see for reducing memory consumption is to reduce the constant-sized memory buffer to below the size we'd need to deal with all possible cases, then reallocate as needed. However, given that the memory buffer is already quite modest-sized (only 63 elements), that likely tells us that the merkle trees we're dealing with are simply so small that it's probably not worth the effort imho. CC @t-nelson for opinions.

Screenshot from 2023-09-27 20-43-48