fix: eagerly build precomputed SpMV tables, remove OnceCell (v0.68.0 → v0.68.1)#484
fix: eagerly build precomputed SpMV tables, remove OnceCell (v0.68.0 → v0.68.1)#484srinathsetty wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR changes R1CSShape cloning behavior to avoid carrying over cached SpMV precomputations and the cached digest when an R1CSShape is cloned, and bumps the crate version accordingly. This targets correctness risks from stale cached precomputations when a cloned shape’s matrices/dimensions are later modified inside the crate.
Changes:
- Replace
#[derive(Clone)]onR1CSShapewith a manualCloneimpl that resetsdigestandprecomputed_{A,B,C}OnceCells. - Keep
Serialize/Deserializebehavior (skippingOnceCellfields) consistent with the new clone behavior. - Bump crate version from
0.68.0to0.68.1.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/r1cs/mod.rs |
Implements manual Clone for R1CSShape that clears cached digest and SpMV precomputations. |
Cargo.toml |
Version bump to 0.68.1. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b0a377e to
cae4074
Compare
Replace OnceCell<PrecomputedSparseMatrix> with eagerly-built PrecomputedSparseMatrix fields. All R1CSShape construction now goes through a central make() helper that builds precomputed tables in parallel. Clone and Deserialize are implemented manually to always rebuild from the matrices. This eliminates the stale-cache bug where clone() (e.g., in pad()) could carry over precomputed tables from the original shape, causing incorrect SpMV results on the cloned shape with different dimensions. Bump version to 0.68.1.
cae4074 to
d1eb2e6
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (2)
src/r1cs/mod.rs:440
R1CSShapeused to expose a publicprecompute_sparse_matrices(&self)method, but it has been removed in this PR. Even if it’s now redundant, removing a public method is a breaking API change and is unexpected for a patch bump (0.68.0 → 0.68.1). Consider keeping the method as a no-op/deprecated wrapper for backward compatibility, or bumping the crate version in a way that signals a breaking change.
|| self.precomputed_B.multiply_vec(z),
|| self.precomputed_C.multiply_vec(z),
)
},
);
Ok((Az, Bz, Cz))
}
/// Multiplies A, B, C by two vectors simultaneously in a single pass per matrix.
/// Returns ((Az1, Bz1, Cz1), (Az2, Bz2, Cz2)).
pub fn multiply_vec_pair(
&self,
z1: &[E::Scalar],
z2: &[E::Scalar],
) -> Result<
(
(Vec<E::Scalar>, Vec<E::Scalar>, Vec<E::Scalar>),
(Vec<E::Scalar>, Vec<E::Scalar>, Vec<E::Scalar>),
),
src/r1cs/mod.rs:209
make()now eagerly buildsPrecomputedSparseMatrixfor A/B/C on every construction path (includingnew(),clone(),pad(), and deserialization). This changes the performance/memory profile substantially vs the previous lazyOnceCellapproach (and can makeclone()unexpectedly expensive). If the intent is only to avoid stale caches when cloning/padding, consider keeping theOnceCellfields and resetting them in the manualCloneimpl (so precompute remains lazy/opt-in).
|| PrecomputedSparseMatrix::from_sparse(&B),
|| PrecomputedSparseMatrix::from_sparse(&C),
)
},
);
Self {
num_cons,
num_vars,
num_io,
A,
B,
C,
digest: OnceCell::new(),
precomputed_A,
precomputed_B,
precomputed_C,
}
}
/// Create an object of type `R1CSShape` from the explicitly specified R1CS matrices
pub fn new(
num_cons: usize,
num_vars: usize,
num_io: usize,
A: SparseMatrix<E::Scalar>,
B: SparseMatrix<E::Scalar>,
C: SparseMatrix<E::Scalar>,
) -> Result<R1CSShape<E>, NovaError> {
let is_valid = |num_cons: usize,
num_vars: usize,
num_io: usize,
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace OnceCell with plain PrecomputedSparseMatrix fields, built eagerly at construction time:
What doesn't change