Skip to content

Commit

Permalink
fix: use mmap when loading public params
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-h-zhang committed Oct 21, 2023
1 parent 99d101f commit ac94358
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 44 deletions.
14 changes: 5 additions & 9 deletions src/public_parameters/disk_cache.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,15 +51,10 @@ where
})
}

pub(crate) fn read_bytes(
&self,
instance: &Instance<'a, F, C, M>,
byte_sink: &mut Vec<u8>,
) -> Result<(), Error> {
pub(crate) fn read_mmap(&self, instance: &Instance<'a, F, C, M>) -> Result<MmapMut, Error> {
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)

Check warning on line 57 in src/public_parameters/disk_cache.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/disk_cache.rs#L57

Added line #L57 was not covered by tests
}

pub(crate) fn write(
Expand Down
7 changes: 3 additions & 4 deletions src/public_parameters/mem_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Check warning on line 62 in src/public_parameters/mem_cache.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mem_cache.rs#L62

Added line #L62 was not covered by tests
info!("loading abomonated {}", instance.key());
let (pp, rest) = unsafe { decode::<PublicParams<F, M>>(&mut bytes).unwrap() };
let (pp, rest) = unsafe { decode::<PublicParams<F, M>>(&mut mmap).unwrap() };

Check warning on line 64 in src/public_parameters/mem_cache.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mem_cache.rs#L64

Added line #L64 was not covered by tests
assert!(rest.is_empty());
Ok(Arc::new(pp.clone())) // this clone is VERY expensive
}
Expand Down
54 changes: 23 additions & 31 deletions src/public_parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ where
|instance: &Instance<'a, F, C, M>| nova::public_params(instance.rc, instance.lang());
let disk_cache = DiskCache::<F, C, M>::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)

Check warning on line 84 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L80-L84

Added lines #L80 - L84 were not covered by tests
} else {
Err(Error::CacheError("failed to decode bytes".into()))

Check warning on line 86 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L86

Added line #L86 was not covered by tests
};

match pp {
Ok(pp) => Ok(bind(pp)),
Expand Down Expand Up @@ -116,16 +114,14 @@ where
{
let disk_cache = DiskCache::<F, C, M>::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::<NovaCircuitShape<F>>(&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::<NovaCircuitShape<F>>(&mut mmap) } {
assert!(remaining.is_empty());
eprintln!("Using disk-cached public params for {}", instance.key());
Ok(pp.clone())

Check warning on line 121 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L117-L121

Added lines #L117 - L121 were not covered by tests
} else {
Err(Error::CacheError("failed to decode bytes".into()))

Check warning on line 123 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L123

Added line #L123 was not covered by tests
}
}

pub fn supernova_aux_params<
Expand All @@ -145,17 +141,13 @@ where
{
let disk_cache = DiskCache::<F, C, M>::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::<SuperNovaAuxParams<F>>(&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::<SuperNovaAuxParams<F>>(&mut mmap) } {
assert!(remaining.is_empty());
Ok(aux_params.clone())

Check warning on line 147 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L144-L147

Added lines #L144 - L147 were not covered by tests
} else {
Err(Error::CacheError("failed to decode bytes".into()))

Check warning on line 149 in src/public_parameters/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/public_parameters/mod.rs#L149

Added line #L149 was not covered by tests
}
}

/// Attempts to extract abomonated public parameters.
Expand Down

0 comments on commit ac94358

Please sign in to comment.