Only cache some function results

Some functions evaluate more quickly without caching than with caching,
because they use data that is cached in a separate read-only cache
that's faster to check, and don't do anything complex with that data.
This commit is contained in:
Oliver Hamlet
2018-10-06 13:38:55 +01:00
parent f6f51e2ee6
commit 48ec04a95f
+15
View File
@@ -193,11 +193,13 @@ fn evaluate_version(
impl Function { impl Function {
pub fn eval(&self, state: &State) -> Result<bool, Error> { pub fn eval(&self, state: &State) -> Result<bool, Error> {
if self.is_slow() {
if let Ok(reader) = state.condition_cache.read() { if let Ok(reader) = state.condition_cache.read() {
if let Some(cached_result) = reader.get(self) { if let Some(cached_result) = reader.get(self) {
return Ok(*cached_result); return Ok(*cached_result);
} }
} }
}
let result = match *self { let result = match *self {
Function::FilePath(ref f) => evaluate_file_path(state, f), Function::FilePath(ref f) => evaluate_file_path(state, f),
@@ -210,14 +212,27 @@ impl Function {
Function::Version(ref p, ref v, ref c) => evaluate_version(state, p, v, *c), Function::Version(ref p, ref v, ref c) => evaluate_version(state, p, v, *c),
}; };
if self.is_slow() {
if let Ok(function_result) = result { if let Ok(function_result) = result {
if let Ok(mut writer) = state.condition_cache.write() { if let Ok(mut writer) = state.condition_cache.write() {
writer.insert(self.clone(), function_result); writer.insert(self.clone(), function_result);
} }
} }
}
result result
} }
/// Some functions are faster to evaluate than to look their result up in
/// the cache, as the data they operate on are already cached separately and
/// the operation is simple.
fn is_slow(&self) -> bool {
use Function::*;
match self {
ActivePath(_) | ActiveRegex(_) | ManyActive(_) | Checksum(_, _) => false,
_ => true,
}
}
} }
#[cfg(test)] #[cfg(test)]