Skip to content

Commit 85094ea

Browse files
authored
Merge pull request #41 from contramundum53/single-cache
Keep only one generation of process cache
2 parents 788dd4c + 525d765 commit 85094ea

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

kurobako_core/src/epi/problem/external_program.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{Error, ErrorKind, Result};
88
use serde::{Deserialize, Serialize};
99
use sha2::{Digest, Sha256};
1010
use std::cell::RefCell;
11-
use std::collections::HashMap;
1211
use std::path::PathBuf;
1312
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
1413
use std::sync::atomic::{self, AtomicU64};
@@ -17,8 +16,8 @@ use std::thread_local;
1716
use structopt::StructOpt;
1817

1918
thread_local! {
20-
static FACTORIES: RefCell<HashMap<Vec<u8>, ExternalProgramProblemFactory>> =
21-
RefCell::new(HashMap::new());
19+
static FACTORY_CACHE : RefCell<Option<(Vec<u8>, ExternalProgramProblemFactory)>> =
20+
RefCell::new(None);
2221
}
2322

2423
/// Recipe for the problem implemented by an external program.
@@ -78,13 +77,17 @@ impl ProblemRecipe for ExternalProgramProblemRecipe {
7877
type Factory = ExternalProgramProblemFactory;
7978

8079
fn create_factory(&self, registry: &FactoryRegistry) -> Result<Self::Factory> {
81-
FACTORIES.with(|f| {
80+
FACTORY_CACHE.with(|f| {
8281
let mut f = f.borrow_mut();
8382
let key = self.cache_key();
84-
if !f.contains_key(&key) {
85-
f.insert(key.clone(), track!(self.create_new_factory(registry))?);
83+
84+
if let Some((k, factory)) = f.as_ref() {
85+
if k == &key {
86+
return Ok(factory.clone());
87+
}
8688
}
87-
Ok(f[&key].clone())
89+
*f = Some((key, track!(self.create_new_factory(registry))?));
90+
return Ok(f.as_ref().unwrap().1.clone());
8891
})
8992
}
9093
}

src/study.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ impl StudiesRecipe {
106106
/// Returns a iterator that iterates over the study recipes specified by this recipe.
107107
pub fn studies(&self) -> impl Iterator<Item = StudyRecipe> {
108108
let mut studies = Vec::new();
109-
for i in 0..self.repeats {
110-
for problem in &self.problems {
109+
for problem in &self.problems {
110+
for i in 0..self.repeats {
111111
for solver in &self.solvers {
112112
let seed = self.seed.map(|s| s + i as u64);
113113
let study = StudyRecipe {

0 commit comments

Comments
 (0)