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

Optimize BlockLocators Iterator with Parallel Processing #3430

Draft
wants to merge 3 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions node/sync/locators/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ default = [ ]
test = [ ]
test_targets = [ "snarkvm/test_targets" ]

[dependencies]
rayon = "1.10"

[dependencies.anyhow]
version = "1.0"

Expand Down
43 changes: 39 additions & 4 deletions node/sync/locators/src/block_locators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

use snarkvm::prelude::{FromBytes, IoResult, Network, Read, ToBytes, Write, error, has_duplicates};

use rayon::prelude::*;
use rayon::iter::IntoParallelIterator;
use anyhow::{Result, bail, ensure};
use indexmap::{IndexMap, indexmap};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, btree_map::IntoIter};
use std::iter::FromIterator;

/// The number of recent blocks (near tip).
pub const NUM_RECENT_BLOCKS: usize = 100; // 100 blocks
Expand Down Expand Up @@ -61,11 +64,14 @@ impl<N: Network> IntoIterator for BlockLocators<N> {
type IntoIter = IntoIter<u32, N::BlockHash>;
type Item = (u32, N::BlockHash);

// TODO (howardwu): Consider using `BTreeMap::from_par_iter` if it is more performant.
// Check by sorting 300-1000 items and comparing the performance.
// (https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html#method.from_par_iter)
fn into_iter(self) -> Self::IntoIter {
BTreeMap::from_iter(self.checkpoints.into_iter().chain(self.recents)).into_iter()
BTreeMap::from_iter(
self.checkpoints
.into_par_iter()
.chain(self.recents.into_par_iter())
.collect::<Vec<_>>(),
Copy link
Collaborator

@ljedrz ljedrz Nov 7, 2024

Choose a reason for hiding this comment

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

can't this be collected directly into a BTreeMap via from_par_iter?

)
.into_iter()
}
}

Expand Down Expand Up @@ -390,11 +396,40 @@ pub mod test_helpers {
mod tests {
use super::*;
use snarkvm::prelude::Field;
use std::time::Instant;

use core::ops::Range;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

/// Generates a large dataset of BlockLocators to test the performance of the iterator.
fn generate_large_block_locators(num_blocks: u32) -> BlockLocators<CurrentNetwork> {
let mut recents = IndexMap::new();
let mut checkpoints = IndexMap::new();

for i in 0..num_blocks {
recents.insert(i, Field::<CurrentNetwork>::from_u32(i).into());
if i % CHECKPOINT_INTERVAL == 0 {
checkpoints.insert(i, Field::<CurrentNetwork>::from_u32(i).into());
}
}

BlockLocators::new_unchecked(recents, checkpoints)
}

#[test]
fn test_block_locators_iterator_performance() {
// Generate a dataset with 1000 items for testing
let block_locators = generate_large_block_locators(1000);

// Measure time taken for the iterator with parallel processing
let start = Instant::now();
let _: Vec<_> = block_locators.clone().into_iter().collect();
let duration_parallel = start.elapsed();

println!("Time taken with parallel processing: {:?}", duration_parallel);
}

/// Simulates block locators for a ledger within the given `heights` range.
fn check_is_valid(checkpoints: IndexMap<u32, <CurrentNetwork as Network>::BlockHash>, heights: Range<u32>) {
for height in heights {
Expand Down