@@ -8,7 +8,6 @@ use crate::{Error, ErrorKind, Result};
8
8
use serde:: { Deserialize , Serialize } ;
9
9
use sha2:: { Digest , Sha256 } ;
10
10
use std:: cell:: RefCell ;
11
- use std:: collections:: HashMap ;
12
11
use std:: path:: PathBuf ;
13
12
use std:: process:: { Child , ChildStdin , ChildStdout , Command , Stdio } ;
14
13
use std:: sync:: atomic:: { self , AtomicU64 } ;
@@ -17,8 +16,8 @@ use std::thread_local;
17
16
use structopt:: StructOpt ;
18
17
19
18
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 ) ;
22
21
}
23
22
24
23
/// Recipe for the problem implemented by an external program.
@@ -78,13 +77,17 @@ impl ProblemRecipe for ExternalProgramProblemRecipe {
78
77
type Factory = ExternalProgramProblemFactory ;
79
78
80
79
fn create_factory ( & self , registry : & FactoryRegistry ) -> Result < Self :: Factory > {
81
- FACTORIES . with ( |f| {
80
+ FACTORY_CACHE . with ( |f| {
82
81
let mut f = f. borrow_mut ( ) ;
83
82
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
+ }
86
88
}
87
- Ok ( f[ & key] . clone ( ) )
89
+ * f = Some ( ( key, track ! ( self . create_new_factory( registry) ) ?) ) ;
90
+ return Ok ( f. as_ref ( ) . unwrap ( ) . 1 . clone ( ) ) ;
88
91
} )
89
92
}
90
93
}
0 commit comments