perf(ai-proxy): optimize SSE decoder - remove PCRE, add decode_buf, fix comment lines#13391
Open
AlinsRan wants to merge 1 commit into
Open
perf(ai-proxy): optimize SSE decoder - remove PCRE, add decode_buf, fix comment lines#13391AlinsRan wants to merge 1 commit into
AlinsRan wants to merge 1 commit into
Conversation
…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>
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
1. Remove PCRE dependency from SSE decoder
Replace
ngx_re.split(PCRE) insse.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.findreduces per-chunk CPU cost and memory allocations in high-throughput SSE streams.2. Add
decode_buf(buf)- single-pass buffer decodingNew function that combines
split_buf+decodeinto one O(n) forward pass, returning(events, remainder).Benefit: The current hot path calls
split_bufthendecode, scanning the buffer twice.decode_bufdoes 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 previousdecode()used a regex that skipped lines without a named field but the new implementation correctly identifiescolon == 1as a comment and skips it without settinghas_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 bydecode_buf.