Skip to content

Commit 1464bec

Browse files
perf: cache username syscalls, precompute filter paths, eliminate hot-loop allocations
- Cache username via OnceCell on Process (syscall runs once per instance) - Precompute slash-suffixed file/dir paths in Filter at construction - Fix filter exclusion loop to call username() once, not per-excluded-name - FD name comparison: parse filter string to int instead of int to string - Precompute parsed IpAddr for host matching (structural equality vs to_string) - FdName::as_display returns Cow<str> (zero-alloc for static variants) - Add Process::new() constructor, keep username_cache private
1 parent 884cf28 commit 1464bec

19 files changed

Lines changed: 221 additions & 300 deletions

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lsofrs"
3-
version = "4.7.1"
3+
version = "4.8.0"
44
edition = "2024"
55
authors = ["Jacob Menke"]
66
description = "Modern, high-performance lsof implementation in Rust"

src/csv_out.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,7 @@ mod tests {
4646
use super::*;
4747

4848
fn make_proc(pid: i32, cmd: &str, files: Vec<OpenFile>) -> Process {
49-
Process {
50-
pid,
51-
ppid: 1,
52-
pgid: pid,
53-
uid: 0,
54-
command: cmd.to_string(),
55-
files,
56-
sel_flags: 0,
57-
sel_state: 0,
58-
}
49+
Process::new(pid, 1, pid, 0, cmd.to_string(), files)
5950
}
6051

6152
fn make_file(fd: i32, ft: FileType, name: &str) -> OpenFile {

src/darwin.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -968,16 +968,14 @@ fn process_pid(pid: pid_t) -> Option<Process> {
968968
}
969969
}
970970

971-
Some(Process {
971+
Some(Process::new(
972972
pid,
973-
ppid: tai.pbsd.pbi_ppid as i32,
974-
pgid: tai.pbsd.pbi_pgid as i32,
975-
uid: tai.pbsd.pbi_uid,
976-
command: cmd,
973+
tai.pbsd.pbi_ppid as i32,
974+
tai.pbsd.pbi_pgid as i32,
975+
tai.pbsd.pbi_uid,
976+
cmd,
977977
files,
978-
sel_flags: 0,
979-
sel_state: 0,
980-
})
978+
))
981979
}
982980

983981
/// Gather all process information from the system

src/delta.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ mod tests {
134134
use super::*;
135135

136136
fn make_proc(pid: i32, cmd: &str, files: Vec<(&str, &str)>) -> Process {
137-
Process {
137+
Process::new(
138138
pid,
139-
ppid: 1,
140-
pgid: 1,
141-
uid: 501,
142-
command: cmd.to_string(),
143-
files: files
139+
1,
140+
1,
141+
501,
142+
cmd.to_string(),
143+
files
144144
.into_iter()
145145
.map(|(fd, name)| OpenFile {
146146
fd: FdName::Number(fd.parse().unwrap()),
@@ -150,9 +150,7 @@ mod tests {
150150
..Default::default()
151151
})
152152
.collect(),
153-
sel_flags: 0,
154-
sel_state: 0,
155-
}
153+
)
156154
}
157155

158156
#[test]
@@ -514,16 +512,7 @@ mod tests {
514512
}
515513

516514
fn proc_with_open_files(pid: i32, cmd: &str, files: Vec<OpenFile>) -> Process {
517-
Process {
518-
pid,
519-
ppid: 1,
520-
pgid: 1,
521-
uid: 501,
522-
command: cmd.to_string(),
523-
files,
524-
sel_flags: 0,
525-
sel_state: 0,
526-
}
515+
Process::new(pid, 1, 1, 501, cmd.to_string(), files)
527516
}
528517

529518
#[test]

0 commit comments

Comments
 (0)