diff --git a/src/public_parameters/disk_cache.rs b/src/public_parameters/disk_cache.rs index 33a1a7b1c6..2d0f306ad7 100644 --- a/src/public_parameters/disk_cache.rs +++ b/src/public_parameters/disk_cache.rs @@ -1,9 +1,10 @@ use std::fs::create_dir_all; -use std::io::{BufReader, BufWriter, Read}; +use std::io::{BufReader, BufWriter}; use std::marker::PhantomData; use abomonation::{encode, Abomonation}; use camino::{Utf8Path, Utf8PathBuf}; +use memmap::MmapMut; use nova::traits::Group; use crate::coprocessor::Coprocessor; @@ -50,15 +51,10 @@ where }) } - pub(crate) fn read_bytes( - &self, - instance: &Instance<'a, F, C, M>, - byte_sink: &mut Vec, - ) -> Result<(), Error> { + pub(crate) fn read_mmap(&self, instance: &Instance<'a, F, C, M>) -> Result { let file = instance.open(&self.dir)?; - let mut reader = BufReader::new(file); - reader.read_to_end(byte_sink)?; - Ok(()) + let mmap = unsafe { MmapMut::map_mut(&file)? }; + Ok(mmap) } pub(crate) fn write( diff --git a/src/public_parameters/mem_cache.rs b/src/public_parameters/mem_cache.rs index 4c181ffc58..4db84740b7 100644 --- a/src/public_parameters/mem_cache.rs +++ b/src/public_parameters/mem_cache.rs @@ -58,11 +58,10 @@ impl PublicParamMemCache { // read the file if it exists, otherwise initialize if instance.abomonated { - let mut bytes = vec![]; - match disk_cache.read_bytes(instance, &mut bytes) { - Ok(()) => { + match disk_cache.read_mmap(instance) { + Ok(mut mmap) => { info!("loading abomonated {}", instance.key()); - let (pp, rest) = unsafe { decode::>(&mut bytes).unwrap() }; + let (pp, rest) = unsafe { decode::>(&mut mmap).unwrap() }; assert!(rest.is_empty()); Ok(Arc::new(pp.clone())) // this clone is VERY expensive } diff --git a/src/public_parameters/mod.rs b/src/public_parameters/mod.rs index e8462ca187..968cb29290 100644 --- a/src/public_parameters/mod.rs +++ b/src/public_parameters/mod.rs @@ -77,16 +77,14 @@ where |instance: &Instance<'a, F, C, M>| nova::public_params(instance.rc, instance.lang()); let disk_cache = DiskCache::::new(disk_cache_path).unwrap(); - let mut bytes = vec![]; - let pp = disk_cache.read_bytes(instance, &mut bytes).and_then(|()| { - if let Some((pp, remaining)) = unsafe { decode(&mut bytes) } { - assert!(remaining.is_empty()); - eprintln!("Using disk-cached public params for {}", instance.key()); - Ok(pp) - } else { - Err(Error::CacheError("failed to decode bytes".into())) - } - }); + let mut mmap = disk_cache.read_mmap(instance)?; + let pp = if let Some((pp, remaining)) = unsafe { decode(&mut mmap) } { + assert!(remaining.is_empty()); + eprintln!("Using disk-cached public params for {}", instance.key()); + Ok(pp) + } else { + Err(Error::CacheError("failed to decode bytes".into())) + }; match pp { Ok(pp) => Ok(bind(pp)), @@ -116,16 +114,14 @@ where { let disk_cache = DiskCache::::new(disk_cache_path).unwrap(); - let mut bytes = vec![]; - disk_cache.read_bytes(instance, &mut bytes).and_then(|()| { - if let Some((pp, remaining)) = unsafe { decode::>(&mut bytes) } { - assert!(remaining.is_empty()); - eprintln!("Using disk-cached public params for {}", instance.key()); - Ok(pp.clone()) - } else { - Err(Error::CacheError("failed to decode bytes".into())) - } - }) + let mut mmap = disk_cache.read_mmap(instance)?; + if let Some((pp, remaining)) = unsafe { decode::>(&mut mmap) } { + assert!(remaining.is_empty()); + eprintln!("Using disk-cached public params for {}", instance.key()); + Ok(pp.clone()) + } else { + Err(Error::CacheError("failed to decode bytes".into())) + } } pub fn supernova_aux_params< @@ -145,17 +141,13 @@ where { let disk_cache = DiskCache::::new(disk_cache_path).unwrap(); - let mut bytes = vec![]; - disk_cache.read_bytes(instance, &mut bytes).and_then(|()| { - if let Some((aux_params, remaining)) = - unsafe { decode::>(&mut bytes) } - { - assert!(remaining.is_empty()); - Ok(aux_params.clone()) - } else { - Err(Error::CacheError("failed to decode bytes".into())) - } - }) + let mut mmap = disk_cache.read_mmap(instance)?; + if let Some((aux_params, remaining)) = unsafe { decode::>(&mut mmap) } { + assert!(remaining.is_empty()); + Ok(aux_params.clone()) + } else { + Err(Error::CacheError("failed to decode bytes".into())) + } } /// Attempts to extract abomonated public parameters.