Skip to content

Commit

Permalink
chore: parallel clones for large data structures (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-h-zhang authored Oct 23, 2023
1 parent 5a21b12 commit 8afd43f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/provider/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};

/// A type that holds commitment generators
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[abomonation_omit_bounds]
pub struct CommitmentKey<G: Group> {
#[abomonate_with(Vec<[u64; 8]>)] // this is a hack; we just assume the size of the element.
ck: Vec<G::PreprocessedGroupElement>,
}

/// [CommitmentKey]s are often large, and this helps with cloning bottlenecks
impl<G: Group> Clone for CommitmentKey<G> {
fn clone(&self) -> Self {
Self {
ck: self.ck.par_iter().cloned().collect(),
}
}
}

impl<G: Group> Len for CommitmentKey<G> {
fn length(&self) -> usize {
self.ck.len()
Expand Down
14 changes: 13 additions & 1 deletion src/r1cs/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};

/// CSR format sparse matrix, We follow the names used by scipy.
/// Detailed explanation here: <https://stackoverflow.com/questions/52299420/scipy-csr-matrix-understand-indptr>
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
#[abomonation_bounds(where <F as PrimeField>::Repr: Abomonation)]
pub struct SparseMatrix<F: PrimeField> {
#[abomonate_with(Vec<F::Repr>)]
Expand All @@ -26,6 +26,18 @@ pub struct SparseMatrix<F: PrimeField> {
pub cols: usize,
}

/// [SparseMatrix]s are often large, and this helps with cloning bottlenecks
impl<F: PrimeField> Clone for SparseMatrix<F> {
fn clone(&self) -> Self {
Self {
data: self.data.par_iter().cloned().collect(),
indices: self.indices.par_iter().cloned().collect(),
indptr: self.indptr.par_iter().cloned().collect(),
cols: self.cols,
}
}
}

impl<F: PrimeField> SparseMatrix<F> {
/// 0x0 empty matrix
pub fn empty() -> Self {
Expand Down

0 comments on commit 8afd43f

Please sign in to comment.