Actions
Optimization #9468
open
JY
http2: avoid O(N) per-frame linear transaction lookup in HTTP2State::find_tx_index()
Optimization #9468:
http2: avoid O(N) per-frame linear transaction lookup in HTTP2State::find_tx_index()
Effort:
medium
Difficulty:
Label:
Description
- Summary
In Suricata's Rust HTTP/2 parser (`rust/src/http2/http2.rs`, verified on `main` commit `820fa845c7cd53fa286f570d79791ead602d4049` / `8.0.8` line), `HTTP2State::find_tx_index()` (lines 913–922) performs an unindexed reverse linear scan over `VecDeque<HTTP2Transaction>` on nearly every parsed HTTP/2 frame (`HTTP2State::parse_frames()` L1531 -> `find_or_create_tx()` L990).
While the default `HTTP2_MAX_STREAMS = 256` cap bounds worst-case overhead, traffic with many concurrent open streams where frames target older streams incurs O(N) CPU cost per frame (~3.1x CPU overhead under default 256 streams, scaling linearly if `app-layer.protocols.http2.max-streams` is raised).
- Measured Benchmark (Release build, `820fa845c7`)
Paired replay of 3,840,000 byte-identical RFC 9113 HTTP/2 frames under default `max-streams: 256` (newest open stream vs. oldest open stream):
- Container CPU: `0.556 s` (newest stream) vs. `1.764 s` (oldest stream) — ~3.03x–3.17x CPU (`4.42x–4.49x` vs. single-stream baseline `0.395 s`).
- Callgrind instructions in `find_or_create_tx`: `10.3M (2.6%)`> `297.1M (43.1%)` (28.8x).Scaling test (64 / 128 / 240 open streams): `+0.501 s` / `+1.009 s` / `+1.510 s` (strictly linear).
- Live TCP `h2c` capture (150k frames/s): `2.22%` -> `6.49%` of one core (~2.9x).
- Suggested Optimization
Maintain a `HashMap<u32, usize>` side-index (`stream_id -> VecDeque index`) on `HTTP2State` (`rust/src/http2/http2.rs:792`) to reduce per-frame lookup to O(1):
1. Overwriting the map entry on `push_back` preserves existing reverse-scan highest-index semantics (including `stream_id == 0` in `create_global_tx()` and stream-ID reuse).
2. Synchronize the index at the four mutation sites of `self.transactions`: `clear()` (L860), `remove(index)` (L898, re-indexing on infrequent eviction), and `push_back` (L952, L1037).
3. Update the sibling reverse scan in `HTTP2State::find_child_stream_id()` (L924–L935) in the same change.
Prototype validation with a `HashMap<u32, usize>` side-index on `820fa845c7` reduced the paired CPU ratio from 3.03x–3.17x down to 1.00x (`+0.003 s` delta) with identical packet/tx counters and zero anomalies.
(Reported following triage with OISF security team; credit: MiniMax Cyber.)
No data to display
Actions