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:
@@ -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)]
|
||||||
|
|||||||
Reference in New Issue
Block a user