Skip to content

Commit

Permalink
build: basic code changes for using rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Apr 16, 2024
1 parent fdbfc1c commit d949355
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 16 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ license-file = "LICENSE"
clap = { version = "4.5.4", features = ["cargo", "derive"] }
derive_more = "0.99.17"
pretty_assertions = "1.2.1"
rayon = "1.10.0"
rstest = "0.17.0"
starknet-types-core = { version = "0.0.11", features = ["hash"] }

Expand Down
2 changes: 2 additions & 0 deletions crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ rstest.workspace = true

[dependencies]
derive_more.workspace = true
rayon.workspace = true
starknet-types-core.workspace = true

2 changes: 1 addition & 1 deletion crates/committer/src/hash/hash_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::types::Felt;

pub(crate) struct HashInputPair(pub Felt, pub Felt);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub(crate) struct HashOutput(pub Felt);

#[allow(dead_code)]
Expand Down
3 changes: 3 additions & 0 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
pub(crate) enum OriginalSkeletonTreeError {}

#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum UpdatedSkeletonTreeError {
MissingNode,
PoisonedLock(String),
NonDroppedPointer(String),
}

#[derive(Debug)]
Expand Down
78 changes: 63 additions & 15 deletions crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::{Arc, Mutex, RwLock};

use crate::hash::hash_trait::{HashFunction, HashOutput};
use crate::patricia_merkle_tree::errors::UpdatedSkeletonTreeError;
Expand Down Expand Up @@ -31,7 +31,10 @@ struct UpdatedSkeletonTreeImpl<L: LeafDataTrait + std::clone::Clone> {
skeleton_tree: HashMap<NodeIndex, UpdatedSkeletonNode<L>>,
}

impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTreeImpl<L> {
#[allow(dead_code)]
impl<L: LeafDataTrait + std::clone::Clone + std::marker::Sync + std::marker::Send>
UpdatedSkeletonTreeImpl<L>
{
fn get_node(
&self,
index: NodeIndex,
Expand All @@ -45,23 +48,41 @@ impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTreeImpl<L> {
fn compute_filled_tree_rec<H: HashFunction, TH: TreeHashFunction<L, H>>(
&self,
index: NodeIndex,
output_map: &mut HashMap<NodeIndex, Mutex<FilledNode<L>>>,
output_map: Arc<RwLock<HashMap<NodeIndex, Mutex<FilledNode<L>>>>>,
) -> Result<HashOutput, UpdatedSkeletonTreeError> {
let node = self.get_node(index)?;
match node {
UpdatedSkeletonNode::Binary => {
let left_index = NodeIndex(index.0 * Felt::TWO);
let right_index = NodeIndex(left_index.0 + Felt::ONE);

let left_hash = self.compute_filled_tree_rec::<H, TH>(left_index, output_map)?;
let right_hash = self.compute_filled_tree_rec::<H, TH>(right_index, output_map)?;
let mut left_hash = Ok(Default::default());
let mut right_hash = Ok(Default::default());

rayon::join(
|| {
left_hash = self
.compute_filled_tree_rec::<H, TH>(left_index, Arc::clone(&output_map));
},
|| {
right_hash = self
.compute_filled_tree_rec::<H, TH>(right_index, Arc::clone(&output_map));
},
);

let left_hash = left_hash?;
let right_hash = right_hash?;

let data = NodeData::Binary(BinaryData {
left_hash,
right_hash,
});
let hash_value = TH::compute_node_hash(&data);
output_map.insert(
// TODO (Aner, 15/4/21): Change to use interior mutability.
let mut write_locked_map = output_map.write().map_err(|_| {
UpdatedSkeletonTreeError::PoisonedLock("Cannot write to output map.".to_owned())
})?;
write_locked_map.insert(
index,
Mutex::new(FilledNode {
hash: hash_value,
Expand All @@ -72,14 +93,18 @@ impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTreeImpl<L> {
}
UpdatedSkeletonNode::Edge { path_to_bottom } => {
let bottom_node_index = NodeIndex::compute_bottom_index(index, *path_to_bottom);
let bottom_hash =
self.compute_filled_tree_rec::<H, TH>(bottom_node_index, output_map)?;
let bottom_hash = self
.compute_filled_tree_rec::<H, TH>(bottom_node_index, Arc::clone(&output_map))?;
let data = NodeData::Edge(EdgeData {
path_to_bottom: *path_to_bottom,
bottom_hash,
});
let hash_value = TH::compute_node_hash(&data);
output_map.insert(
// TODO (Aner, 15/4/21): Change to use interior mutability.
let mut write_locked_map = output_map.write().map_err(|_| {
UpdatedSkeletonTreeError::PoisonedLock("Cannot write to output map.".to_owned())
})?;
write_locked_map.insert(
index,
Mutex::new(FilledNode {
hash: hash_value,
Expand All @@ -91,7 +116,11 @@ impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTreeImpl<L> {
UpdatedSkeletonNode::Sibling(hash_result) => Ok(*hash_result),
UpdatedSkeletonNode::Leaf(node_data) => {
let hash_value = TH::compute_node_hash(&NodeData::Leaf(node_data.clone()));
output_map.insert(
// TODO (Aner, 15/4/21): Change to use interior mutability.
let mut write_locked_map = output_map.write().map_err(|_| {
UpdatedSkeletonTreeError::PoisonedLock("Cannot write to output map.".to_owned())
})?;
write_locked_map.insert(
index,
Mutex::new(FilledNode {
hash: hash_value,
Expand All @@ -104,15 +133,34 @@ impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTreeImpl<L> {
}
}

impl<L: LeafDataTrait + std::clone::Clone> UpdatedSkeletonTree<L> for UpdatedSkeletonTreeImpl<L> {
impl<L: LeafDataTrait + std::clone::Clone + std::marker::Sync + std::marker::Send>
UpdatedSkeletonTree<L> for UpdatedSkeletonTreeImpl<L>
{
fn compute_filled_tree<H: HashFunction, TH: TreeHashFunction<L, H>>(
&self,
) -> Result<impl FilledTree<L>, UpdatedSkeletonTreeError> {
// 1. Create a new hashmap for the filled tree.
let mut filled_tree_map = HashMap::new();
// TODO (Aner, 15/4/21): Change to use interior mutability.
let filled_tree_map = Arc::new(RwLock::new(HashMap::new()));
// 2. Compute the filled tree hashmap from the skeleton_tree.
self.compute_filled_tree_rec::<H, TH>(NodeIndex::root_index(), &mut filled_tree_map)?;
// 3. Create a new FilledTreeImpl from the hashmap.
Ok(FilledTreeImpl::new(filled_tree_map))
self.compute_filled_tree_rec::<H, TH>(
NodeIndex::root_index(),
Arc::clone(&filled_tree_map),
)?;
// 3. Create and return a new FilledTreeImpl from the hashmap.
Ok(FilledTreeImpl::new(
Arc::try_unwrap(filled_tree_map)
.map_err(|_| {
UpdatedSkeletonTreeError::NonDroppedPointer(
"Unable to unwrap the arc of the filled_tree_map".to_owned(),
)
})?
.into_inner()
.map_err(|_| {
UpdatedSkeletonTreeError::PoisonedLock(
"Cannot take filled tree map.".to_owned(),
)
})?,
))
}
}

0 comments on commit d949355

Please sign in to comment.