Skip to content

Commit

Permalink
eliminate 'cloned' call on hydration; recover parallel hydration on L…
Browse files Browse the repository at this point in the history
…EM store
  • Loading branch information
arthurpaulino committed Sep 26, 2023
1 parent e85a6fd commit 9c416ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
39 changes: 14 additions & 25 deletions src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use arc_swap::ArcSwap;
use elsa::sync::{FrozenMap, FrozenVec};
use elsa::sync_index_set::FrozenIndexSet;
use nom::{sequence::preceded, Parser};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{cell::RefCell, rc::Rc, sync::Arc};

use crate::{
Expand Down Expand Up @@ -51,9 +52,8 @@ pub struct Store<F: LurkField> {

pub poseidon_cache: PoseidonCache<F>,

dehydrated: FrozenVec<Box<Ptr<F>>>,
dehydrated: ArcSwap<FrozenVec<Box<Ptr<F>>>>,
z_cache: FrozenMap<Ptr<F>, Box<ZPtr<F>>>,
next_hydration_idx: ArcSwap<usize>,

comms: FrozenMap<FWrap<F>, Box<(F, Ptr<F>)>>, // hash -> (secret, src)
}
Expand All @@ -65,7 +65,7 @@ impl<F: LurkField> Store<F> {
let ptr = Ptr::Tuple2(tag, idx);
if inserted {
// this is for `hydrate_z_cache`
self.dehydrated.push(Box::new(ptr));
self.dehydrated.load().push(Box::new(ptr));
}
ptr
}
Expand All @@ -85,7 +85,7 @@ impl<F: LurkField> Store<F> {
let ptr = Ptr::Tuple3(tag, idx);
if inserted {
// this is for `hydrate_z_cache`
self.dehydrated.push(Box::new(ptr));
self.dehydrated.load().push(Box::new(ptr));
}
ptr
}
Expand All @@ -112,7 +112,7 @@ impl<F: LurkField> Store<F> {
let ptr = Ptr::Tuple4(tag, idx);
if inserted {
// this is for `hydrate_z_cache`
self.dehydrated.push(Box::new(ptr));
self.dehydrated.load().push(Box::new(ptr));
}
ptr
}
Expand Down Expand Up @@ -536,26 +536,15 @@ impl<F: LurkField> Store<F> {
/// Hashes `Ptr` trees from the bottom to the top, avoiding deep recursions
/// in `hash_ptr`.
pub fn hydrate_z_cache(&self) {
let dehydrated_len = self.dehydrated.len();
// TODO: can we recover the parallel hydration?
for i in **self.next_hydration_idx.load()..dehydrated_len {
let ptr = self
.dehydrated
.get(i)
.expect("Out of bounds. Implementation broken");
self.hash_ptr(ptr).expect("failed to hash_expr");
}
self.next_hydration_idx.swap(Arc::new(dehydrated_len));
}

pub fn clear_dehydrated_queue(&mut self) {
self.dehydrated = FrozenVec::new();
self.next_hydration_idx.swap(Arc::new(0));
}

pub fn hydrate_z_cache_and_clear_queue(&mut self) {
self.hydrate_z_cache();
self.clear_dehydrated_queue();
self.dehydrated
.load()
.iter()
.collect::<Vec<_>>()
.par_iter()
.for_each(|ptr| {
self.hash_ptr(ptr).expect("failed to hash_ptr");
});
self.dehydrated.swap(Arc::new(FrozenVec::default()));
}

pub fn to_vector(&self, ptrs: &[Ptr<F>]) -> Result<Vec<F>> {
Expand Down
27 changes: 14 additions & 13 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1363,24 +1363,25 @@ impl<F: LurkField> Store<F> {
pub fn hydrate_scalar_cache(&self) {
self.ensure_constants();

let dehydrated = self.dehydrated.load().iter().cloned().collect::<Vec<_>>();

dehydrated.par_iter().for_each(|ptr| {
self.hash_expr(ptr).expect("failed to hash_expr");
});
self.dehydrated
.load()
.iter()
.collect::<Vec<_>>()
.par_iter()
.for_each(|ptr| {
self.hash_expr(ptr).expect("failed to hash_expr");
});

self.dehydrated.swap(Arc::new(FrozenVec::default()));

let dehydrated_cont = self
.dehydrated_cont
self.dehydrated_cont
.load()
.iter()
.cloned()
.collect::<Vec<_>>();

dehydrated_cont.par_iter().for_each(|ptr| {
self.hash_cont(ptr).expect("failed to hash_cont");
});
.collect::<Vec<_>>()
.par_iter()
.for_each(|ptr| {
self.hash_cont(ptr).expect("failed to hash_cont");
});

self.dehydrated_cont.swap(Arc::new(FrozenVec::default()));
}
Expand Down

0 comments on commit 9c416ad

Please sign in to comment.