Skip to content

perf: parse large streamed frames in linear time#16489

Open
Nic-Polumeyv wants to merge 1 commit into
sveltejs:version-3from
Nic-Polumeyv:stream-linear-parse
Open

perf: parse large streamed frames in linear time#16489
Nic-Polumeyv wants to merge 1 commit into
sveltejs:version-3from
Nic-Polumeyv:stream-linear-parse

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

read_stream accumulates decoded text in one string, re-runs indexOf over 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) and query.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 - 1 characters 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:

transport chunks before after speedup
2048 × 1 KiB 1720.7 ms 4.0 ms 429×
512 × 4 KiB 431.0 ms 3.2 ms 135×
128 × 16 KiB 111.5 ms 3.1 ms 36×
32 × 64 KiB 31.3 ms 3.1 ms 10×

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_stream was extracted in #15957 and last changed in #16423. No open PR modifies stream.js, ndjson.js or sse.js.


Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from ee3bf28:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/ee3bf285877efb7c4ab47bfed2230007c25ca365

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16489

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ee3bf28

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

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.

1 participant