Fix O(n²) performance in ServerEventParser.parse()#49
Merged
Recouse merged 1 commit intoRecouse:mainfrom May 7, 2026
Merged
Conversation
The parse() method had quadratic performance when receiving large SSE events delivered in small chunks (e.g., URLSession's ~8KB didReceiveData callbacks): 1. `buffer + data` created a new Data allocation on every call, copying the entire accumulated buffer each time. 2. `splitBuffer` scanned the entire buffer for separators on every chunk, even when no separator could possibly be present in the new data. For a 1.5MB SSE event arriving in ~180 chunks, total work was ~135MB of data copying/scanning, causing 15-20s delays that triggered timeouts. Fix: - Use `buffer.append(data)` for amortized O(1) in-place append - Only scan the tail region (new data + separator overlap) for separators - Only call splitBuffer when a separator is actually detected This reduces per-chunk work from O(buffer_size) to O(chunk_size), making the overall complexity O(n) instead of O(n²). Fixes Recouse#48
Recouse
approved these changes
May 7, 2026
Owner
Recouse
left a comment
There was a problem hiding this comment.
Looks like a solid change, thank you
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
ServerEventParser.parse()that causes severe delays (15-20s) for large SSE events delivered in small chunksFixes #48
Problem
When receiving a large SSE event (e.g., 1.5MB) via URLSession's ~8KB
didReceiveDatacallbacks:buffer + datacreates a new Data allocation every call, copying the entire buffersplitBufferscans the entire buffer for\n\non every chunk, even when no separator is presentFor ~180 chunks: total work = 8KB + 16KB + 24KB + ... + 1.5MB ≈ 135MB of copying/scanning.
Fix
buffer.append(data)— in-place append, amortized O(1)\n\nsplitBufferentirely when no separator is detected (vast majority of calls)Impact
Testing
Verified in production iOS app with SSE flight search responses containing 200-400 inventories (~1.5MB payloads). Events that previously caused 30s timeout failures now parse in under 100ms.