Skip to content

Commit

Permalink
fix: be a bit more clever with memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Jan 17, 2024
1 parent fdd9f31 commit 0d82735
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/public_parameters/mem_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ impl PublicParamMemCache {
// 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.
// TODO: enforce this in a better way
fn get_from_disk_cache_or_update_with<
'a,
F: CurveCycleEquipped,
C: Coprocessor<F> + 'static,
Fn: FnOnce(&Instance<'static, F, C>) -> Arc<PublicParams<F, C1LEM<'a, F, C>>>,
C: Coprocessor<F> + 'a,
Fn: FnOnce(&Instance<'a, F, C>) -> Arc<PublicParams<F, C1LEM<'a, F, C>>>,
>(
&'static self,
instance: &Instance<'static, F, C>,
instance: &Instance<'a, F, C>,
default: Fn,
) -> Result<Arc<PublicParams<F, C1LEM<'a, F, C>>>, Error>
where
Expand All @@ -67,10 +68,12 @@ impl PublicParamMemCache {
match disk_cache.read_bytes(instance, &mut bytes) {
Ok(()) => {
info!("loading abomonated {}", instance.key());
let (pp, rest) =
unsafe { decode::<PublicParams<F, C1LEM<'a, F, C>>>(&mut bytes).unwrap() };
let (pp, rest) = unsafe {
decode::<PublicParams<F, C1LEM<'a, F, C>>>(&mut bytes).unwrap()
};
assert!(rest.is_empty());
Ok(unsafe { Arc::from_raw(pp as *const PublicParams<F, C1LEM<'a, F, C>>) })
let pp = unsafe { std::mem::transmute_copy::<PublicParams<F, C1LEM<'a, F, C>>, PublicParams<F, C1LEM<'a, F, C>>>(pp) };
Ok(Arc::new(pp))
}
Err(Error::IO(e)) => {
warn!("{e}");
Expand Down
4 changes: 2 additions & 2 deletions src/public_parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::public_parameters::error::Error;
use self::disk_cache::DiskCache;
use self::instance::Instance;

pub fn public_params<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'static>(
pub fn public_params<F: CurveCycleEquipped, C: Coprocessor<F> + 'static>(
instance: &Instance<'static, F, C>,
) -> Result<Arc<PublicParams<F, C1LEM<'a, F, C>>>, Error>
) -> Result<Arc<PublicParams<F, C1LEM<'static, F, C>>>, Error>
where
<<E1<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
<<E2<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
Expand Down

0 comments on commit 0d82735

Please sign in to comment.