Skip to content

perf(ai-proxy): optimize SSE decoder - remove PCRE, add decode_buf, fix comment lines#13391

Open
AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:perf/ai-sse-optimize
Open

perf(ai-proxy): optimize SSE decoder - remove PCRE, add decode_buf, fix comment lines#13391
AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:perf/ai-sse-optimize

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

Summary

1. Remove PCRE dependency from SSE decoder

Replace ngx_re.split (PCRE) in sse.decode() with a pure Lua forward scan (string.find).

Benefit: Eliminates regex compilation overhead on each streaming chunk. PCRE calls are expensive because they allocate JIT memory; replacing them with string.find reduces per-chunk CPU cost and memory allocations in high-throughput SSE streams.

2. Add decode_buf(buf) - single-pass buffer decoding

New function that combines split_buf + decode into one O(n) forward pass, returning (events, remainder).

Benefit: The current hot path calls split_buf then decode, scanning the buffer twice. decode_buf does both in one pass, halving scan work per chunk. Callers that only need the combined result can use this directly.

3. Fix SSE comment lines being treated as fields

SSE lines starting with : are comment lines per the SSE spec. The previous decode() used a regex that skipped lines without a named field but the new implementation correctly identifies colon == 1 as a comment and skips it without setting has_field = true, which previously caused comment-only blocks to be emitted as empty events.

Benefit: Correct spec compliance. Some providers (e.g., keep-alive pings sent as : keep-alive) would produce spurious empty events before this fix.

Test

Added TEST 29 in t/plugin/ai-proxy-protocol-conversion.t: SSE comment lines interleaved with real events are correctly discarded by decode_buf.

…ix comment lines

- Replace ngx_re.split (PCRE) in sse.decode() with a pure Lua forward scan.
  Avoids regex compilation and PCRE overhead on every streaming chunk.
- Add sse.decode_buf(buf) that combines split_buf + decode into a single
  O(n) forward pass, halving the number of scans per chunk in hot paths.
- Fix SSE comment lines (lines starting with ':') being incorrectly treated
  as fields. Per the SSE spec, comment lines must be silently ignored; the
  previous code set has_field=true for them, producing spurious empty events.
- Add test: SSE comment lines are correctly discarded by decode_buf.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. performance generate flamegraph for the current PR labels May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance generate flamegraph for the current PR size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant