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

Perf: Fix a bottleneck in calculate_hashes #41

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

Davidson-Souza
Copy link
Collaborator

After profiling Floresta I found that calculate_hashes, a method used for computing roots from a proof, was taking an absurd amount of time in one specific function called sorted_push. As the name implies, this function adds an element to a collection, and keeps the full collection sorted. The approach used was simply to push and then sort the array. But the successive calls to merge-sort caused a severe performance hit for large proofs.

This PR uses an elementary replacement for this function. We keep the invariant of sorting by appending the element right where it should be, if we want to keep the whole collection sorted. We find this element by performing a binary search over the collection.

For reference, I wrote a small doc with different approaches that could be taken. The mentioned profiling is this flamegraph. Here's how it looks like after this change. This pr also adds a benchmark for this method. Here's the result before and after it.

test accumulator::proof::bench::bench_calculate_hashes ... bench:      83,043 ns/iter (+/- 9,842)
test accumulator::proof::bench::bench_calculate_hashes ... bench:      57,712 ns/iter (+/- 1,221)

While profiling some downstream crate, I found that "calculate_hashes" is
dominated by "sorted_push" because it calls merge sort on every push.
After investigating some alternatives, finding the position with a binary
search and inserting at that position achieves the best result.
@Davidson-Souza Davidson-Souza merged commit b590e73 into mit-dci:main Sep 22, 2023
Davidson-Souza added a commit to Davidson-Souza/rustreexo that referenced this pull request Jun 8, 2024
This is useful for node solutions like umbrel and resplitz
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.

1 participant