feat: optimize regex match to reduce memory consumption

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-10-25 03:56:23 +00:00
1 parent bbf4250677
commit 42f3f40755
6 files changed
+71 -34

No files matched your search

+39 -21
View File
@@ -118,9 +118,9 @@ pub struct UdevDevice {
udev: *mut Udev,
properties: UdevListEntry,
syspath: Option<*const libc::c_char>,
devlinks: UdevListEntry,
tags: UdevListEntry,
sysattr: UdevListEntry,
devlinks: Option<UdevListEntry>,
tags: Option<UdevListEntry>,
sysattr: Option<UdevListEntry>,
seqnum: libc::c_ulonglong,
strings: FfiStringPool,
}
@@ -160,9 +160,9 @@ impl UdevDevice {
udev: unsafe { udev_ref(udev) },
properties,
syspath: syspath.map(|x| strings.insert_rust(x).as_ptr()),
devlinks: *devlinks,
tags: *tags,
sysattr: *sysattr,
devlinks: devlinks.map(|x| *x),
tags: tags.map(|x| *x),
sysattr: sysattr.map(|x| *x),
seqnum: 0,
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(
dev: *const UdevDevice,
) -> *const UdevListEntry {
unsafe { &raw const (*dev).devlinks }
unsafe {
(*dev)
.devlinks
.as_ref()
.map(|x| x as *const _)
.unwrap_or_default()
}
}
#[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(
dev: *const UdevDevice,
) -> *const UdevListEntry {
unsafe { &raw const (*dev).tags }
unsafe {
(*dev)
.tags
.as_ref()
.map(|x| x as *const _)
.unwrap_or_default()
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn udev_device_get_current_tags_list_entry(
dev: *const UdevDevice,
) -> *const UdevListEntry {
unsafe { &raw const (*dev).tags }
unsafe {
(*dev)
.tags
.as_ref()
.map(|x| x as *const _)
.unwrap_or_default()
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn udev_device_get_sysattr_list_entry(
dev: *const UdevDevice,
) -> *const UdevListEntry {
unsafe { &raw const (*dev).sysattr }
unsafe {
(*dev)
.sysattr
.as_ref()
.map(|x| x as *const _)
.unwrap_or_default()
}
}
#[unsafe(no_mangle)]
@@ -872,7 +896,7 @@ pub unsafe extern "C" fn udev_enumerate_scan_devices(
for i in &mut result {
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
}
}
@@ -1023,25 +1047,19 @@ fn hashmap_to_list(
}))
}
fn vec_to_list(strings: &mut FfiStringPool, vec: &Vec<String>) -> Box<UdevListEntry> {
let mut head = None;
fn vec_to_list(strings: &mut FfiStringPool, vec: &[String]) -> Option<Box<UdevListEntry>> {
let mut head: Option<Box<UdevListEntry>> = None;
let mut tail = &mut head;
for s in vec {
let entry = Box::new(UdevListEntry {
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,
});
*tail = Some(entry);
tail = &mut tail.as_mut().unwrap().next;
}
head.unwrap_or_else(|| {
Box::new(UdevListEntry {
key: std::ptr::null(),
value: std::ptr::null(),
next: None,
})
})
head
}