perf: parse large streamed frames in linear time#16489
Open
Nic-Polumeyv wants to merge 1 commit into
Open
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/ee3bf285877efb7c4ab47bfed2230007c25ca365Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
🦋 Changeset detectedLatest commit: ee3bf28 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
read_streamaccumulates decoded text in one string, re-runsindexOfover the whole buffer after every transport chunk and re-slices the buffer for every emitted record. When one frame spans many chunks, every prefix is rescanned and total work grows quadratically with frame size. This parser sits behind streamed page data and remote functions (NDJSON) andquery.live(SSE), so the cost lands on the client for large payloads.With this change only newly decoded text is searched. Already searched text accumulates in an array and is joined once per completed frame. The last
delimiter.length - 1characters stay in the unsearched tail so a delimiter split across chunk boundaries still matches.One 2 MiB frame, Node 22 x64, median of repeated runs:
Many small records, the common path, get slightly faster (2 MiB of 64 byte records in 16 KiB chunks, ~21 ms to ~15 ms) because the old code re-sliced the buffer once per record.
Output is unchanged. The rewrite matched the previous implementation across 4000 randomized cases covering both delimiters, delimiters split between chunks, multibyte UTF-8 split between chunks and trailing unterminated records. The new unit tests also pass against the previous implementation.
read_streamwas extracted in #15957 and last changed in #16423. No open PR modifiesstream.js,ndjson.jsorsse.js.Before submitting the PR, please make sure you do the following
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.