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

Further improvements to Merkleization #2328

Merged
merged 54 commits into from
Jul 3, 2024
Merged

Further improvements to Merkleization #2328

merged 54 commits into from
Jul 3, 2024

Conversation

eljobe
Copy link
Member

@eljobe eljobe commented May 22, 2024

This PR encompasses two important changes:

  1. The flush_module! macro is removed from per-operation calls and the same work is done just before hashing the machine.

  2. The memory type now tracks dirty indices in the memory and only sets the corresponding leaves in the merkle tree just before hashing.

These two combine to make the performance of validating a whole block ~39% faster than on merkle-perf.

Based on #2273
Related to https://linear.app/offchain-labs/issue/NIT-2411/arbitrator-optimizations

eljobe added 22 commits May 8, 2024 13:49
This allows us to dump ValidationInput messages from a runing nitro
node, or even a system test, and use them to drive the benchmarking
code.
We need to be able to see how frequently the memory is being hashed
and from where.

The problem is that with always_merkleize off, there are thousands of
fewer calls to `Merkle::root()` and this is to help us figure out
where the calls are coming from.

Spoiler alert: It's from the flush_module macro.
The high-level benefit of this change is a 16.5% reduction in block
processing time from ~17.6s to ~14.7s.

The only job of that macro was to recalculate the hash of the module
and update the module merkle tree's corresponding leaf with the new
hash.

Instead, the waits until the hash for the module is about to be
calculated and calls `Merkle::set()` for each module in the modules
vector.

This eliminates thousands of calls to the possibly expensive
`Merkle::root()` call on the `Memory` of each module.

This pattern of delaying when to actually update the leaves of merkle
trees until they will be used has a lot of potential for additional
performance boosts.
This change just marks memory as dirty when storing new values rather
than actually calculating the new hash for the data in memory and then
calculates the hashes for dirty indices just before creating the
merkle tree.

On one test block, this ended up saving 23 million calls to keccack.
This is to prove that the code is still fast enough to pass the
test-go suite of tests if the `always_merkleize` feature is enabled.
This is now fast enough to just use it as the only implementation and
simplify the code a bit.
The prover no longer runs in any other mode.
The default behavior of an Arc is to allow sharing between different
threads and different instances. So, the derived behavior for cloning
was to put the same references on the clone as on the orignal.

But, we actually want separate clones of our merkle trees to be
independent. That is, changing the data on the clone, should have no
affect on the original (and vice versa.)

The test added in this commit failed before the change and passes
afer.
@cla-bot cla-bot bot added the s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA. label May 22, 2024
@eljobe eljobe requested a review from tsahee May 22, 2024 13:02
Copy link
Contributor

@rauljordan rauljordan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comments on this one. I can't pinpoint yet where there are any issues with the challenge system test aside from the removal of the flush_module calls...

@eljobe eljobe marked this pull request as draft June 27, 2024 22:28
@eljobe eljobe marked this pull request as ready for review July 2, 2024 21:22
@eljobe eljobe requested review from PlasmaPower and removed request for tsahee July 2, 2024 21:22
@eljobe eljobe merged commit 41cabc4 into merkle-perf Jul 3, 2024
8 checks passed
@eljobe eljobe deleted the merkle-perf-a branch July 3, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants