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
+21 -6
View File
@@ -193,9 +193,11 @@ fn evaluate_version(
impl Function {
pub fn eval(&self, state: &State) -> Result<bool, Error> {
if let Ok(reader) = state.condition_cache.read() {
if let Some(cached_result) = reader.get(self) {
return Ok(*cached_result);
if self.is_slow() {
if let Ok(reader) = state.condition_cache.read() {
if let Some(cached_result) = reader.get(self) {
return Ok(*cached_result);
}
}
}
@@ -210,14 +212,27 @@ impl Function {
Function::Version(ref p, ref v, ref c) => evaluate_version(state, p, v, *c),
};
if let Ok(function_result) = result {
if let Ok(mut writer) = state.condition_cache.write() {
writer.insert(self.clone(), function_result);
if self.is_slow() {
if let Ok(function_result) = result {
if let Ok(mut writer) = state.condition_cache.write() {
writer.insert(self.clone(), function_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)]