diff --git a/concrete-optimizer/src/optimization/decomposition/circuit_bootstrap.rs b/concrete-optimizer/src/optimization/decomposition/circuit_bootstrap.rs index fedfca5fb..bced23b8a 100644 --- a/concrete-optimizer/src/optimization/decomposition/circuit_bootstrap.rs +++ b/concrete-optimizer/src/optimization/decomposition/circuit_bootstrap.rs @@ -11,6 +11,8 @@ use crate::utils::cache::ephemeral::{CacheHashMap, EphemeralCache}; use crate::utils::cache::persistent::PersistentCacheHashMap; use crate::utils::square; +use super::common::VERSION; + #[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub struct CbComplexityNoise { pub decomp: BrDecompositionParameters, @@ -111,5 +113,5 @@ pub fn cache( glwe_params, ) }; - PersistentCacheHashMap::new(&path, "v0", function) + PersistentCacheHashMap::new(&path, VERSION, function) } diff --git a/concrete-optimizer/src/optimization/decomposition/common.rs b/concrete-optimizer/src/optimization/decomposition/common.rs index efb32473c..34e94034c 100644 --- a/concrete-optimizer/src/optimization/decomposition/common.rs +++ b/concrete-optimizer/src/optimization/decomposition/common.rs @@ -2,4 +2,4 @@ use crate::parameters::GlweParameters; pub type MacroParam = (GlweParameters, u64); -pub const VERSION: &str = "v1"; +pub const VERSION: u64 = 1; diff --git a/concrete-optimizer/src/optimization/decomposition/pp_switch.rs b/concrete-optimizer/src/optimization/decomposition/pp_switch.rs index e546c6a4a..303dc1ae3 100644 --- a/concrete-optimizer/src/optimization/decomposition/pp_switch.rs +++ b/concrete-optimizer/src/optimization/decomposition/pp_switch.rs @@ -15,7 +15,7 @@ use crate::utils::cache::persistent::PersistentCacheHashMap; use crate::{config, security}; use super::blind_rotate; -use super::common::MacroParam; +use super::common::{MacroParam, VERSION}; #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)] pub struct PpSwitchComplexityNoise { @@ -117,5 +117,5 @@ pub fn cache( &br, ) }; - PersistentCacheHashMap::new(&path, "v0", function) + PersistentCacheHashMap::new(&path, VERSION, function) } diff --git a/concrete-optimizer/src/utils/cache/persistent.rs b/concrete-optimizer/src/utils/cache/persistent.rs index ee2276c46..0da9752a4 100644 --- a/concrete-optimizer/src/utils/cache/persistent.rs +++ b/concrete-optimizer/src/utils/cache/persistent.rs @@ -22,7 +22,7 @@ where // path on disk path: String, // version to invalidate no longer valid cache - version: String, + version: u64, // content: the HashMap is read-only, but it can be a new HashMap content: RwLock>, // the HashMap is read once, never modified and shared content_changed: AtomicBool, // true if the content changed since loading from disk @@ -47,7 +47,7 @@ where { pub fn new( path: &str, - version: &str, + version: u64, function: impl 'static + Send + Sync + Fn(ROC::K) -> ROC::V, ) -> Self { let t0 = Instant::now(); @@ -61,7 +61,6 @@ where ); } let path = path.into(); - let version = version.into(); let content = RwLock::new(Arc::new(content)); let content_changed = AtomicBool::new(false); Self { @@ -117,7 +116,7 @@ where (lock, content) } - fn read_from_disk(path: &str, version: &str) -> Option { + fn read_from_disk(path: &str, version: u64) -> Option { if DISABLE_CACHE { return None; } @@ -170,7 +169,7 @@ where return; } }; - let maybe_disk_content = Self::read_given_lock(&filelock, &self.path, &self.version); + let maybe_disk_content = Self::read_given_lock(&filelock, &self.path, self.version); if let Some(disk_content) = maybe_disk_content { self.update_with(|content| ROC::merge(content, disk_content)); } @@ -184,7 +183,7 @@ where self.content_changed.store(false, Ordering::Relaxed); } - fn read_given_lock(filelock: &FileLock, path: &str, version: &str) -> Option { + fn read_given_lock(filelock: &FileLock, path: &str, version: u64) -> Option { match filelock.file.metadata() { Ok(metadata) => { if metadata.size() == 0 { @@ -201,11 +200,11 @@ where }; let mut buf = BufReader::new(&filelock.file); - let disk_version: Result = bincode::deserialize_from(buf.borrow_mut()); + let disk_version: Result = bincode::deserialize_from(buf.borrow_mut()); match disk_version { Ok(disk_version) => { - if disk_version != *version { + if disk_version != version { println!("PersistentCache:: Invalid version {path}: cleaning"); Self::clear_file(path); return None; @@ -298,21 +297,21 @@ mod tests { let f = move |_key| value1; { PCache::clear_file(path); - let disk_cache = PCache::new(path, "v0", f); + let disk_cache = PCache::new(path, 0, f); let mut mem_cache = disk_cache.cache(); let res = mem_cache.get(key1); assert_eq!(res, &value1); disk_cache.backport(mem_cache); } { - let cache_later = PCache::new(path, "v0", f); + let cache_later = PCache::new(path, 0, f); let mut mem_cache = cache_later.cache(); let res = mem_cache.get(key1); assert_eq!(res, &value1); assert!(CacheHashMap::own_new_entries(mem_cache).is_empty()); } { - let cache_later = PCache::new(path, "v1", f); + let cache_later = PCache::new(path, 1, f); let mut mem_cache = cache_later.cache(); let res = mem_cache.get(key1); assert_eq!(res, &value1);