mirror of
https://github.com/loot/loot-condition-interpreter.git
synced 2026-07-27 14:16:09 -07:00
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:
+21
-6
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user