Skip to content

refactor(app): zero-alloc process-name diff for lsof debug log#329

Open
Pablosinyores wants to merge 1 commit into
domcyrus:mainfrom
Pablosinyores:refactor/process-name-diff-zero-alloc
Open

refactor(app): zero-alloc process-name diff for lsof debug log#329
Pablosinyores wants to merge 1 commit into
domcyrus:mainfrom
Pablosinyores:refactor/process-name-diff-zero-alloc

Conversation

@Pablosinyores

Copy link
Copy Markdown

Summary

`enrich_with_lsof` (`src/app.rs:1177`) walks every connection that already has a `process_name` set and, when lsof returns a new candidate name, compares the two to log a debug-level mismatch:

```rust
let existing_normalized = existing_name
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ");
let new_normalized =
name.split_whitespace().collect::<Vec<&str>>().join(" ");

if existing_normalized != new_normalized {
debug!("...");
}
```

That's a `Vec<&str>` + a heap `String` per side, both dropped on the next statement, every connection, every enrichment pass — even when debug logging is off.

Patch

Use `Iterator::ne` on the `split_whitespace` token iterators directly:

```rust
if existing_name
.split_whitespace()
.ne(name.split_whitespace())
{
debug!("...");
}
```

`Iterator::ne` walks both iterators in lockstep and short-circuits on the first divergence, so the whitespace-collapsed comparison is equivalent to the old join-then-compare but allocates nothing.

Test plan

  • `cargo fmt` clean
  • `cargo clippy --all-targets -- -D warnings` clean
  • `cargo test --lib` 365/365

No behavior change (debug-log gating is unchanged, branch condition is observationally equivalent).

`enrich_with_lsof` walks every connection that already has a
`process_name` set and, when lsof returns a new candidate name, compares
the two to log a debug-level mismatch:

```rust
let existing_normalized = existing_name
    .split_whitespace()
    .collect::<Vec<&str>>()
    .join(" ");
let new_normalized =
    name.split_whitespace().collect::<Vec<&str>>().join(" ");

if existing_normalized != new_normalized {
    debug!(...);
}
```

That's a `Vec<&str>` + a heap `String` for each side, both dropped on
the next statement, every connection, every enrichment pass — even when
debug logging is off.

Use `Iterator::ne` on the `split_whitespace` token iterators directly:

```rust
if existing_name
    .split_whitespace()
    .ne(name.split_whitespace())
{
    debug!(...);
}
```

Iterator::ne walks both iterators in lockstep and short-circuits on the
first divergence, so the whitespace-collapsed comparison is equivalent
to the old join-then-compare but allocates nothing.

cargo fmt clean, cargo clippy --all-targets -- -D warnings clean,
cargo test --lib 365/365.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant