perf(dpi): drop redundant lines().collect::<Vec<&str>>() in HTTP + SSH parsers#339
Open
obchain wants to merge 1 commit into
Open
perf(dpi): drop redundant lines().collect::<Vec<&str>>() in HTTP + SSH parsers#339obchain wants to merge 1 commit into
lines().collect::<Vec<&str>>() in HTTP + SSH parsers#339obchain wants to merge 1 commit into
Conversation
…SSH parsers
Both `analyze_http` and `analyze_ssh` were materializing the full line
iterator into a `Vec<&str>` and then walking it sequentially:
let lines: Vec<&str> = text.lines().collect();
if lines.is_empty() { return None; }
// …then index `lines[0]` and iterate `lines.iter().skip(1)`,
// or just `for line in lines { … }`.
Neither path needs random access. Drive the `Lines` iterator directly:
- `http.rs` pulls the request/status line via `next()?` (which also
subsumes the old `is_empty()` guard) and iterates the remainder for
headers.
- `ssh.rs` iterates `text.lines()` straight into the banner loop; the
`is_empty()` guard is dead because `is_likely_ssh` already requires
`payload.len() >= 4` plus a banner / packet-structure check upstream.
Per-packet on the DPI hot path, this saves one heap allocation per
parse (one `Vec<&str>` plus the slice it points into). An HTTP request
with N headers used to allocate a Vec of N+1 entries; now it allocates
zero. SSH banner parsing was a smaller win but the same shape of fix.
No behavior change — verified by the existing HTTP and SSH unit tests
(5 + 17, all passing) plus the full lib sweep (371 passed).
Closes domcyrus#338
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
analyze_httpandanalyze_sshboth collectedtext.lines()into aVec<&str>and then walked it sequentially. Neither path needed random access —http.rsonly indexed[0]andskip(1)'d the rest, andssh.rsjust iterated.Drive the
Linesiterator directly instead:src/network/dpi/http.rs—lines.next()?for the request/status line (which also subsumes the oldis_empty()guard); the remaining iterator drives the header loop.src/network/dpi/ssh.rs— iteratetext.lines()straight into the banner loop. Theis_empty()guard was dead:is_likely_sshalready requirespayload.len() >= 4plus a banner/packet-structure check upstream.Per-packet on the DPI hot path, this drops one heap allocation per parse. An HTTP request with N headers used to allocate a Vec of N+1 entries; now zero. SSH banner parsing is a smaller win but identical shape.
Closes #338.
Behavior
No change. The first-line
next()?returns identically to indexing[0]after the oldis_empty()guard, and the header/banner iterators yield the same&stritems in the same order. Verified by the existing tests:Test plan
cargo test --libcargo fmt --checkcargo clippy --all-targets -- -D warnings