feat: optimize regex match to reduce memory consumption
Signed-off-by: sisungo <[email protected]>
This commit is contained in:
1 parent
bbf4250677
commit
42f3f40755
6 files changed
+71
-34
No files matched your search
+8
-3
@@ -36,11 +36,16 @@ impl Db {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
pub fn save(&self) -> std::io::Result<()> {
|
||||
if let Some(path) = &self.path {
|
||||
let text = serde_json::to_string(&*self.map.read().unwrap()).unwrap();
|
||||
_ = std::fs::write(&path, text);
|
||||
let mut fd = std::fs::File::options()
|
||||
.truncate(true)
|
||||
.create(true)
|
||||
.write(true)
|
||||
.open(path)?;
|
||||
serde_json::to_writer(&mut fd, &*self.map.read().unwrap())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
moka = { version = "0.12", features = ["sync"] }
|
||||
regex = "1"
|
||||
ron = "0.12"
|
||||
rustc-hash = "2"
|
||||
|
||||
+19
-8
@@ -3,11 +3,13 @@ use crate::{
|
||||
parser::{Action, OnError, Rule, RuleFile, Str, When},
|
||||
};
|
||||
use db::DbSection;
|
||||
use moka::sync::Cache;
|
||||
use regex::Regex;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
path::Path,
|
||||
process::{ExitStatus, Stdio},
|
||||
sync::Arc,
|
||||
sync::{Arc, LazyLock},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::{io::AsyncReadExt, process::Child};
|
||||
@@ -135,20 +137,18 @@ impl Engine {
|
||||
let key = self.bake_str(key)?;
|
||||
let regex = self.bake_str(regex)?;
|
||||
let regex = format!("^{regex}$");
|
||||
let regex = Regex::new(®ex).map_err(Error::Regex)?;
|
||||
let Some(val) = self.properties.get(&key) else {
|
||||
return Ok(false);
|
||||
};
|
||||
Ok(regex.is_match(&val))
|
||||
regex_match(®ex, &val).map_err(Error::Regex)
|
||||
}
|
||||
When::PartMatch(key, regex) => {
|
||||
let key = self.bake_str(key)?;
|
||||
let regex = self.bake_str(regex)?;
|
||||
let regex = Regex::new(®ex).map_err(Error::Regex)?;
|
||||
let Some(val) = self.properties.get(&key) else {
|
||||
return Ok(false);
|
||||
};
|
||||
Ok(regex.is_match(&val))
|
||||
regex_match(®ex, &val).map_err(Error::Regex)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,7 +158,7 @@ impl Engine {
|
||||
Action::Set(key, val) => {
|
||||
let key = self.bake_str(key)?;
|
||||
let val = self.bake_str(val)?;
|
||||
self.properties.insert(&key, val);
|
||||
self.properties.insert(&key, val.into());
|
||||
}
|
||||
Action::Add(key, val) => {
|
||||
let key = self.bake_str(key)?;
|
||||
@@ -182,7 +182,7 @@ impl Engine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn bake_str(&self, s: &Str) -> Result<String, Error> {
|
||||
fn bake_str<'s>(&self, s: &'s Str) -> Result<Cow<'s, str>, Error> {
|
||||
match s {
|
||||
Str::Direct(x) => Ok(x.into()),
|
||||
Str::Format(template) => {
|
||||
@@ -222,7 +222,7 @@ impl Engine {
|
||||
if current_property.is_some() || right_bracket {
|
||||
return Err(Error::InvalidStringTemplate);
|
||||
}
|
||||
Ok(result)
|
||||
Ok(result.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -340,3 +340,14 @@ impl CommandTimeoutBehavior {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn regex_match(re: &str, s: &str) -> Result<bool, regex::Error> {
|
||||
static REGEX_CACHE: LazyLock<Cache<String, Regex>> = LazyLock::new(|| Cache::new(256));
|
||||
|
||||
let it = REGEX_CACHE
|
||||
.entry(re.into())
|
||||
.or_try_insert_with(|| Regex::new(re))
|
||||
.map_err(|err| (*err).clone())?;
|
||||
let re = it.value();
|
||||
Ok(re.is_match(s))
|
||||
}
|
||||
Reference in new issue
Block a user