Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep only one generation of process cache #41

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions kurobako_core/src/epi/problem/external_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{Error, ErrorKind, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
use std::sync::atomic::{self, AtomicU64};
Expand All @@ -17,8 +16,8 @@ use std::thread_local;
use structopt::StructOpt;

thread_local! {
static FACTORIES: RefCell<HashMap<Vec<u8>, ExternalProgramProblemFactory>> =
RefCell::new(HashMap::new());
static FACTORY_CACHE : RefCell<Option<(Vec<u8>, ExternalProgramProblemFactory)>> =
RefCell::new(None);
}

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

fn create_factory(&self, registry: &FactoryRegistry) -> Result<Self::Factory> {
FACTORIES.with(|f| {
FACTORY_CACHE.with(|f| {
let mut f = f.borrow_mut();
let key = self.cache_key();
if !f.contains_key(&key) {
f.insert(key.clone(), track!(self.create_new_factory(registry))?);

if let Some((k, factory)) = f.as_ref() {
if k == &key {
return Ok(factory.clone());
}
}
Ok(f[&key].clone())
*f = Some((key, track!(self.create_new_factory(registry))?));
return Ok(f.as_ref().unwrap().1.clone());
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/study.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl StudiesRecipe {
/// Returns a iterator that iterates over the study recipes specified by this recipe.
pub fn studies(&self) -> impl Iterator<Item = StudyRecipe> {
let mut studies = Vec::new();
for i in 0..self.repeats {
for problem in &self.problems {
for problem in &self.problems {
for i in 0..self.repeats {
for solver in &self.solvers {
let seed = self.seed.map(|s| s + i as u64);
let study = StudyRecipe {
Expand Down