Skip to content

Commit

Permalink
cache rhai function parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Apr 8, 2024
1 parent ddea9b2 commit 489f00f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ merge = "0.1"
once_cell = "1.19"
self_update = { version = "0.39", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] }
toml = "0.8"
cached = "0.49.3"

[dev-dependencies]
abscissa_core = { version = "0.7.0", default-features = false, features = ["testing"] }
Expand Down
37 changes: 24 additions & 13 deletions src/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use log::warn;
use rustic_core::{repofile::SnapshotFile, StringList};
use std::{error::Error, str::FromStr};

use cached::proc_macro::cached;
use rhai::{serde::to_dynamic, Dynamic, Engine, FnPtr, AST};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
Expand All @@ -24,6 +25,17 @@ impl FromStr for SnapshotFn {
}
}

#[cached(key = "String", convert = r#"{ s.to_string() }"#, size = 1)]
fn string_to_fn(s: &str) -> Option<SnapshotFn> {
match SnapshotFn::from_str(s) {
Ok(filter_fn) => Some(filter_fn),
Err(err) => {
warn!("Error evaluating filter-fn {s}: {err}",);
None
}
}
}

impl SnapshotFn {
/// Call the function with a [`SnapshotFile`]
///
Expand Down Expand Up @@ -87,20 +99,19 @@ impl SnapshotFilter {
#[must_use]
pub fn matches(&self, snapshot: &SnapshotFile) -> bool {
if let Some(filter_fn) = &self.filter_fn {
match SnapshotFn::from_str(filter_fn)
.unwrap()
.call::<bool>(snapshot)
{
Ok(result) => {
if !result {
return false;
if let Some(func) = string_to_fn(filter_fn) {
match func.call::<bool>(snapshot) {
Ok(result) => {
if !result {
return false;
}
}
Err(err) => {
warn!(
"Error evaluating filter-fn for snapshot {}: {err}",
snapshot.id
);
}
}
Err(err) => {
warn!(
"Error evaluating filter-fn for snapshot {}: {err}",
snapshot.id
);
}
}
}
Expand Down

0 comments on commit 489f00f

Please sign in to comment.