Skip to content

Commit

Permalink
refactor: Improve memory efficiency in Nova's abomonation proving system
Browse files Browse the repository at this point in the history
- Removed the `Clone` trait from `PublicParams` struct in `nova.
- Enhanced doc for memory safety in `PublicParamMemCache` in `mem_cache.rs`
- Replaced cloning commands with `Arc::from_raw` function, resulting in improved memory utilization.
  • Loading branch information
huitseeker committed Jan 17, 2024
1 parent 944c1d9 commit fdd9f31
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/proof/nova.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub type NovaCircuitShape<F> = R1CSWithArity<E1<F>>;
pub type NovaPublicParams<F, C1> = nova::PublicParams<E1<F>, E2<F>, C1, C2<F>>;

/// A struct that contains public parameters for the Nova proving system.
#[derive(Clone, Serialize, Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(bound = "")]
pub struct PublicParams<F, SC: StepCircuit<F>>
where
Expand Down
6 changes: 5 additions & 1 deletion src/public_parameters/mem_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub(crate) static PUBLIC_PARAM_MEM_CACHE: Lazy<PublicParamMemCache> =
});

impl PublicParamMemCache {
/// ## Safety
// This assumes:
// 1. no one is mutating these params, which is reasonable if all accesses are Lurk processes,
// 2. our process only reads this once, otherwise the produced Arc will leak memory.
fn get_from_disk_cache_or_update_with<
'a,
F: CurveCycleEquipped,
Expand All @@ -66,7 +70,7 @@ impl PublicParamMemCache {
let (pp, rest) =
unsafe { decode::<PublicParams<F, C1LEM<'a, F, C>>>(&mut bytes).unwrap() };
assert!(rest.is_empty());
Ok(Arc::new(pp.clone())) // this clone is VERY expensive
Ok(unsafe { Arc::from_raw(pp as *const PublicParams<F, C1LEM<'a, F, C>>) })
}
Err(Error::IO(e)) => {
warn!("{e}");
Expand Down

0 comments on commit fdd9f31

Please sign in to comment.