SSE parser byte-level optimization, chunked decoding, auto-reconnect#9
Draft
SSE parser byte-level optimization, chunked decoding, auto-reconnect#9
Conversation
…edDecoder, auto-reconnect with Last-Event-ID, thread safety docs 1. SSEParser byte-level optimization: Rewrite to accumulate raw Data bytes, scan for line endings at byte level (0x0A/0x0D), and defer String conversion until a complete event boundary is reached. Reduces ARC overhead and CPU usage. 2. ChunkedDecoder: New incremental decoder for HTTP Transfer-Encoding: chunked byte streams. Strips hex-length + CRLF framing so SSE parser receives clean data. Handles partial chunks across TCP segments. 3. Auto-reconnect with Last-Event-ID: NWAsyncSocket now supports enableSSEAutoReconnect() which auto-reconnects on error disconnect, preserves lastEventId across reconnections, and notifies the delegate via willAutoReconnectWithLastEventId for seamless SSE resumption. 4. Thread safety documentation: Enhanced doc comments clarifying that socketQueue handles all I/O and parsing off the main thread, while delegateQueue delivers callbacks to the UI thread. Co-authored-by: dustturtle <2305214+dustturtle@users.noreply.github.com>
Co-authored-by: dustturtle <2305214+dustturtle@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize SSE parser implementation for iOS framework
SSE parser byte-level optimization, chunked decoding, auto-reconnect
Mar 10, 2026
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.
Implements the four iOS-specific optimizations discussed in the issue: reducing ARC/CPU overhead in the SSE parser, handling proxied chunked streams, seamless reconnection on network switches, and thread-safety documentation.
SSEParser: byte-level accumulation
lineBufferchanged fromStringtoData; line-ending scan uses raw bytes (0x0A/0x0D)[UInt8]arrays — no String allocation per lineString(data:encoding:.utf8)deferred to event dispatch boundary (\n\n)ChunkedDecoder (new)
Transfer-Encoding: chunkedframing before SSE parsingenableChunkedDecoding()onNWAsyncSocketSSE auto-reconnect with Last-Event-ID
enableSSEAutoReconnect(retryInterval:)— on error disconnect, schedules reconnect preservinglastEventIdretry:field for backoff intervalsocket(_:willAutoReconnectWithLastEventId:afterDelay:)lets the app re-send the HTTP request withLast-Event-IDheaderdisconnect()cancels auto-reconnectThread safety docs
socketQueue/delegateQueueseparation in class-level and method-level doc comments (behavior was already correct)87 tests pass (71 existing + 16 new ChunkedDecoder tests). All existing SSEParser/StreamBuffer/ReadRequest tests unchanged.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.