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
@@ -241,7 +241,9 @@ pub async fn get_device(name: &str) -> DbSection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sync_db() {
|
pub fn sync_db() {
|
||||||
DB.save();
|
if let Err(err) = DB.save() {
|
||||||
|
tracing::error!("db sync failure: {err}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dev_root() -> PathBuf {
|
fn dev_root() -> PathBuf {
|
||||||
|
|||||||
+39
-21
@@ -118,9 +118,9 @@ pub struct UdevDevice {
|
|||||||
udev: *mut Udev,
|
udev: *mut Udev,
|
||||||
properties: UdevListEntry,
|
properties: UdevListEntry,
|
||||||
syspath: Option<*const libc::c_char>,
|
syspath: Option<*const libc::c_char>,
|
||||||
devlinks: UdevListEntry,
|
devlinks: Option<UdevListEntry>,
|
||||||
tags: UdevListEntry,
|
tags: Option<UdevListEntry>,
|
||||||
sysattr: UdevListEntry,
|
sysattr: Option<UdevListEntry>,
|
||||||
seqnum: libc::c_ulonglong,
|
seqnum: libc::c_ulonglong,
|
||||||
strings: FfiStringPool,
|
strings: FfiStringPool,
|
||||||
}
|
}
|
||||||
@@ -160,9 +160,9 @@ impl UdevDevice {
|
|||||||
udev: unsafe { udev_ref(udev) },
|
udev: unsafe { udev_ref(udev) },
|
||||||
properties,
|
properties,
|
||||||
syspath: syspath.map(|x| strings.insert_rust(x).as_ptr()),
|
syspath: syspath.map(|x| strings.insert_rust(x).as_ptr()),
|
||||||
devlinks: *devlinks,
|
devlinks: devlinks.map(|x| *x),
|
||||||
tags: *tags,
|
tags: tags.map(|x| *x),
|
||||||
sysattr: *sysattr,
|
sysattr: sysattr.map(|x| *x),
|
||||||
seqnum: 0,
|
seqnum: 0,
|
||||||
strings,
|
strings,
|
||||||
}
|
}
|
||||||
@@ -367,7 +367,13 @@ pub unsafe extern "C" fn udev_device_get_is_initialized(_: *const UdevDevice) ->
|
|||||||
pub unsafe extern "C" fn udev_device_get_devlinks_list_entry(
|
pub unsafe extern "C" fn udev_device_get_devlinks_list_entry(
|
||||||
dev: *const UdevDevice,
|
dev: *const UdevDevice,
|
||||||
) -> *const UdevListEntry {
|
) -> *const UdevListEntry {
|
||||||
unsafe { &raw const (*dev).devlinks }
|
unsafe {
|
||||||
|
(*dev)
|
||||||
|
.devlinks
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| x as *const _)
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
@@ -381,21 +387,39 @@ pub unsafe extern "C" fn udev_device_get_properties_list_entry(
|
|||||||
pub unsafe extern "C" fn udev_device_get_tags_list_entry(
|
pub unsafe extern "C" fn udev_device_get_tags_list_entry(
|
||||||
dev: *const UdevDevice,
|
dev: *const UdevDevice,
|
||||||
) -> *const UdevListEntry {
|
) -> *const UdevListEntry {
|
||||||
unsafe { &raw const (*dev).tags }
|
unsafe {
|
||||||
|
(*dev)
|
||||||
|
.tags
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| x as *const _)
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub unsafe extern "C" fn udev_device_get_current_tags_list_entry(
|
pub unsafe extern "C" fn udev_device_get_current_tags_list_entry(
|
||||||
dev: *const UdevDevice,
|
dev: *const UdevDevice,
|
||||||
) -> *const UdevListEntry {
|
) -> *const UdevListEntry {
|
||||||
unsafe { &raw const (*dev).tags }
|
unsafe {
|
||||||
|
(*dev)
|
||||||
|
.tags
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| x as *const _)
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub unsafe extern "C" fn udev_device_get_sysattr_list_entry(
|
pub unsafe extern "C" fn udev_device_get_sysattr_list_entry(
|
||||||
dev: *const UdevDevice,
|
dev: *const UdevDevice,
|
||||||
) -> *const UdevListEntry {
|
) -> *const UdevListEntry {
|
||||||
unsafe { &raw const (*dev).sysattr }
|
unsafe {
|
||||||
|
(*dev)
|
||||||
|
.sysattr
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| x as *const _)
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
@@ -872,7 +896,7 @@ pub unsafe extern "C" fn udev_enumerate_scan_devices(
|
|||||||
for i in &mut result {
|
for i in &mut result {
|
||||||
i.insert_str(0, "/sys");
|
i.insert_str(0, "/sys");
|
||||||
}
|
}
|
||||||
(*udev_enumerate).result = Some(*vec_to_list(&mut (*udev_enumerate).strings, &result));
|
(*udev_enumerate).result = vec_to_list(&mut (*udev_enumerate).strings, &result).map(|x| *x);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1023,25 +1047,19 @@ fn hashmap_to_list(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vec_to_list(strings: &mut FfiStringPool, vec: &Vec<String>) -> Box<UdevListEntry> {
|
fn vec_to_list(strings: &mut FfiStringPool, vec: &[String]) -> Option<Box<UdevListEntry>> {
|
||||||
let mut head = None;
|
let mut head: Option<Box<UdevListEntry>> = None;
|
||||||
let mut tail = &mut head;
|
let mut tail = &mut head;
|
||||||
|
|
||||||
for s in vec {
|
for s in vec {
|
||||||
let entry = Box::new(UdevListEntry {
|
let entry = Box::new(UdevListEntry {
|
||||||
key: strings.insert_rust(s.clone()).as_ptr(),
|
key: strings.insert_rust(s.clone()).as_ptr(),
|
||||||
value: strings.insert_rust("".into()).as_ptr(),
|
value: strings.insert_rust(String::new()).as_ptr(),
|
||||||
next: None,
|
next: None,
|
||||||
});
|
});
|
||||||
*tail = Some(entry);
|
*tail = Some(entry);
|
||||||
tail = &mut tail.as_mut().unwrap().next;
|
tail = &mut tail.as_mut().unwrap().next;
|
||||||
}
|
}
|
||||||
|
|
||||||
head.unwrap_or_else(|| {
|
head
|
||||||
Box::new(UdevListEntry {
|
|
||||||
key: std::ptr::null(),
|
|
||||||
value: std::ptr::null(),
|
|
||||||
next: None,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
+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 {
|
if let Some(path) = &self.path {
|
||||||
let text = serde_json::to_string(&*self.map.read().unwrap()).unwrap();
|
let mut fd = std::fs::File::options()
|
||||||
_ = std::fs::write(&path, text);
|
.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]
|
[dependencies]
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
moka = { version = "0.12", features = ["sync"] }
|
||||||
regex = "1"
|
regex = "1"
|
||||||
ron = "0.12"
|
ron = "0.12"
|
||||||
rustc-hash = "2"
|
rustc-hash = "2"
|
||||||
|
|||||||
+19
-8
@@ -3,11 +3,13 @@ use crate::{
|
|||||||
parser::{Action, OnError, Rule, RuleFile, Str, When},
|
parser::{Action, OnError, Rule, RuleFile, Str, When},
|
||||||
};
|
};
|
||||||
use db::DbSection;
|
use db::DbSection;
|
||||||
|
use moka::sync::Cache;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
path::Path,
|
path::Path,
|
||||||
process::{ExitStatus, Stdio},
|
process::{ExitStatus, Stdio},
|
||||||
sync::Arc,
|
sync::{Arc, LazyLock},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tokio::{io::AsyncReadExt, process::Child};
|
use tokio::{io::AsyncReadExt, process::Child};
|
||||||
@@ -135,20 +137,18 @@ impl Engine {
|
|||||||
let key = self.bake_str(key)?;
|
let key = self.bake_str(key)?;
|
||||||
let regex = self.bake_str(regex)?;
|
let regex = self.bake_str(regex)?;
|
||||||
let regex = format!("^{regex}$");
|
let regex = format!("^{regex}$");
|
||||||
let regex = Regex::new(®ex).map_err(Error::Regex)?;
|
|
||||||
let Some(val) = self.properties.get(&key) else {
|
let Some(val) = self.properties.get(&key) else {
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
};
|
};
|
||||||
Ok(regex.is_match(&val))
|
regex_match(®ex, &val).map_err(Error::Regex)
|
||||||
}
|
}
|
||||||
When::PartMatch(key, regex) => {
|
When::PartMatch(key, regex) => {
|
||||||
let key = self.bake_str(key)?;
|
let key = self.bake_str(key)?;
|
||||||
let regex = self.bake_str(regex)?;
|
let regex = self.bake_str(regex)?;
|
||||||
let regex = Regex::new(®ex).map_err(Error::Regex)?;
|
|
||||||
let Some(val) = self.properties.get(&key) else {
|
let Some(val) = self.properties.get(&key) else {
|
||||||
return Ok(false);
|
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) => {
|
Action::Set(key, val) => {
|
||||||
let key = self.bake_str(key)?;
|
let key = self.bake_str(key)?;
|
||||||
let val = self.bake_str(val)?;
|
let val = self.bake_str(val)?;
|
||||||
self.properties.insert(&key, val);
|
self.properties.insert(&key, val.into());
|
||||||
}
|
}
|
||||||
Action::Add(key, val) => {
|
Action::Add(key, val) => {
|
||||||
let key = self.bake_str(key)?;
|
let key = self.bake_str(key)?;
|
||||||
@@ -182,7 +182,7 @@ impl Engine {
|
|||||||
Ok(())
|
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 {
|
match s {
|
||||||
Str::Direct(x) => Ok(x.into()),
|
Str::Direct(x) => Ok(x.into()),
|
||||||
Str::Format(template) => {
|
Str::Format(template) => {
|
||||||
@@ -222,7 +222,7 @@ impl Engine {
|
|||||||
if current_property.is_some() || right_bracket {
|
if current_property.is_some() || right_bracket {
|
||||||
return Err(Error::InvalidStringTemplate);
|
return Err(Error::InvalidStringTemplate);
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -340,3 +340,14 @@ impl CommandTimeoutBehavior {
|
|||||||
Self::default()
|
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))
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ clear_vars = true
|
|||||||
max-attempts = -1
|
max-attempts = -1
|
||||||
|
|
||||||
[env.vars]
|
[env.vars]
|
||||||
PATH = "/usr/bin"
|
PATH = "/bin:/usr/bin:/sbin:/usr/sbin"
|
||||||
LXDEVICED_HAS_DEVTMPFS = "1"
|
LXDEVICED_HAS_DEVTMPFS = "1"
|
||||||
LXDEVICED_DEV_ROOT = "/dev"
|
LXDEVICED_DEV_ROOT = "/dev"
|
||||||
LXDEVICED_SYSFS_ROOT = "/sys"
|
LXDEVICED_SYSFS_ROOT = "/sys"
|
||||||
Reference in new issue
Block a user