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

Avoid ledger copy and mutation in Sparse_ledger.of_ledger_subset_exn #14587

Conversation

mrmr1993
Copy link
Member

This PR builds upon #14586, removing the Ledger.copy and Ledger.create_empty_exn calls from Sparse_ledger.of_ledger_subset_exn. This function takes a significant amount of time in staged ledger diff application, and so this PR should further improve performance there.

Checklist:

  • Dependency versions are unchanged
    • Notify Velocity team if dependencies must change in CI
  • Modified the current draft of release notes with details on what is completed or incomplete within this project
  • Document code purpose, how to use it
    • Mention expected invariants, implicit constraints
  • Tests were added for the new behavior
    • Document test purpose, significance of failures
    • Test names should reflect their purpose
  • All tests pass (CI will check this if you didn't)
  • Serialized types are in stable-versioned modules
  • Does this close issues? List them

@mrmr1993
Copy link
Member Author

!ci-build-me

@mrmr1993
Copy link
Member Author

!ci-nightly-me

@mrmr1993
Copy link
Member Author

!ci-build-me

@sebastiencs
Copy link
Contributor

From what I understand, Ledger.create_empty_exn is slow because it indirectly calls Masking_merkle_tree.set, which re-compute all hashes for each new accounts:
https://github.com/MinaProtocol/mina/blob/55d20405f195b3f8e4d6603e216798191472d707/src/lib/merkle_mask/masking_merkle_tree.ml#L318C1-L327C38

There are multiple PRs opened to optimize transaction applications by avoiding access to disk, but I don't think that the slowness comes from those DB access.

The slowest part is that every modification to a Mask re-computes all hashes.
A solution would be to delay re-hashing a Mask until one of the method merkle_root or merkle_path is called.
In the case of Sparse_ledger.of_ledger_subset_exn, with new accounts, no hashing is even required since those are empty accounts.

@mrmr1993
Copy link
Member Author

From what I understand, Ledger.create_empty_exn is slow because it indirectly calls Masking_merkle_tree.set, which re-compute all hashes for each new accounts:

Indeed, we already knew and were working on a fix. To keep the PRs small and testing more modular, it's in its own PR here: #14594

@mrmr1993 mrmr1993 marked this pull request as ready for review November 21, 2023 18:23
@mrmr1993 mrmr1993 requested a review from a team as a code owner November 21, 2023 18:23
@mrmr1993
Copy link
Member Author

!ci-build-me

@georgeee georgeee force-pushed the feature/merkle-mask-dont-rehash branch from cd8f02c to 10fe9b6 Compare November 27, 2023 18:09
@georgeee georgeee force-pushed the feature/sparse-ledger-of-subset-no-mutability branch from d2f3c8d to dbfe750 Compare November 27, 2023 18:23
non_empty_locations
in
let merkle_paths = Ledger.merkle_path_batch oledger merkle_path_locations in
(let rec pop_empties num_empties merkle_paths locations acc =
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we just do a list.split here? We don't use the location of empty accounts later.
I think we could just have List.split merkle_paths num_new_accounts here.

Copy link
Member

Choose a reason for hiding this comment

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

@ghost-not-in-the-shell do you mind implementing this change and committing to this PR?

@georgeee georgeee force-pushed the feature/sparse-ledger-of-subset-no-mutability branch 3 times, most recently from 2120e7c to 44c86cc Compare November 28, 2023 20:15
@georgeee georgeee changed the base branch from feature/merkle-mask-dont-rehash to feature/merkle-mask-empty-account-preloading November 28, 2023 20:32
@georgeee georgeee force-pushed the feature/sparse-ledger-of-subset-no-mutability branch from 44c86cc to a8b5b8e Compare November 28, 2023 20:32
@ghost-not-in-the-shell
Copy link
Contributor

!ci-build-me

@deepthiskumar
Copy link
Member

!ci-nightly-me

@deepthiskumar
Copy link
Member

!ci-build-me

@deepthiskumar
Copy link
Member

!ci-nightly-me

@georgeee georgeee force-pushed the feature/sparse-ledger-of-subset-no-mutability branch from c86eced to 9c89cca Compare December 1, 2023 20:57
@deepthiskumar
Copy link
Member

!ci-nightly-me

@deepthiskumar deepthiskumar merged commit dc434fa into feature/merkle-mask-empty-account-preloading Dec 5, 2023
1 check passed
@deepthiskumar deepthiskumar deleted the feature/sparse-ledger-of-subset-no-mutability branch December 5, 2023 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants