Skip to content

SSE parser byte-level optimization, chunked decoding, auto-reconnect#9

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/optimize-sse-parser-ios
Draft

SSE parser byte-level optimization, chunked decoding, auto-reconnect#9
Copilot wants to merge 3 commits intomainfrom
copilot/optimize-sse-parser-ios

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

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

  • lineBuffer changed from String to Data; line-ending scan uses raw bytes (0x0A/0x0D)
  • Field names compared as [UInt8] arrays — no String allocation per line
  • String(data:encoding:.utf8) deferred to event dispatch boundary (\n\n)

ChunkedDecoder (new)

  • Incremental state-machine that strips Transfer-Encoding: chunked framing before SSE parsing
  • Handles partial chunks split across TCP segments, chunk extensions, hex size variants
  • Integrated via enableChunkedDecoding() on NWAsyncSocket

SSE auto-reconnect with Last-Event-ID

  • enableSSEAutoReconnect(retryInterval:) — on error disconnect, schedules reconnect preserving lastEventId
  • Honors server-sent retry: field for backoff interval
  • Delegate callback socket(_:willAutoReconnectWithLastEventId:afterDelay:) lets the app re-send the HTTP request with Last-Event-ID header
  • Explicit disconnect() cancels auto-reconnect

Thread safety docs

  • Documented socketQueue / delegateQueue separation in class-level and method-level doc comments (behavior was already correct)
let socket = NWAsyncSocket(delegate: self, delegateQueue: .main)
socket.enableSSEParsing()
socket.enableChunkedDecoding()        // behind Nginx/CDN
socket.enableSSEAutoReconnect()       // auto-retry with Last-Event-ID
try socket.connect(toHost: "api.example.com", onPort: 443)

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.

Copilot AI and others added 2 commits March 10, 2026 07:37
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants