refactor(app): zero-alloc process-name diff for lsof debug log#329
Open
Pablosinyores wants to merge 1 commit into
Open
refactor(app): zero-alloc process-name diff for lsof debug log#329Pablosinyores wants to merge 1 commit into
Pablosinyores wants to merge 1 commit into
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
No behavior change (debug-log gating is unchanged, branch condition is observationally equivalent).