Skip to content

perf: relay untouched net.fetch responses from protocol.handle without pumping them through JS - #53379

Merged
MarshallOfSound merged 4 commits into
45-x-yfrom
trop/45-x-y-bp-perf-relay-untouched-net-fetch-responses-from-protocol-handle-without-pumping-them-through-js-1788291929547
Sep 4, 2026
Merged

MarshallOfSound merged 4 commits into
45-x-yfrom
trop/45-x-y-bp-perf-relay-untouched-net-fetch-responses-from-protocol-handle-without-pumping-them-through-js-1788291929547

Conversation

@trop

@trop trop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Backport of #52953

See that PR for details.

Notes: Improved throughput of responses that protocol.handle handlers return straight from net.fetch.

trop Bot and others added 3 commits September 1, 2026 19:45
…t pumping them through JS

protocol.handle(scheme, req => net.fetch(...)) is the documented way to
serve an app's own files from a custom scheme and to proxy a remote origin
through one, and every byte of such a response crossed the browser main
thread twice: SimpleURLLoader's chunks were copied into ArrayBuffers for
an IncomingMessage, wrapped into a web ReadableStream for the Response,
unwrapped again with Readable.fromWeb and written back into a mojo pipe by
NodeStreamLoader in 64 KB slices.

When a handler returns the Response net.fetch produced with its body still
untouched (the exact stream net.fetch created, not read, locked, cloned or
replaced), protocol.handle now tells the underlying SimpleURLLoaderWrapper
to stop handing chunks to JS, collects whatever JS had already pulled as a
prefix, and passes the client end to the wrapper, which writes prefix and
remainder straight into the response pipe and completes the client with
the request's outcome. Status and headers of the returned Response
(including ones the handler set) are used as before; anything else - a
re-wrapped or re-created Response, a body the handler started reading,
Response.error() - keeps the existing path. The response pipe grows to the
upstream's declared length (up to the network service default) but never
below mojo's default. Works for any scheme net.fetch can load.

Renderer fetch() through such a handler, release build, medians of
interleaved fresh-process runs: forwarding to file:// 16 MB 40.0 -> 24.1
ms, 1 MB 4.5 -> 3.8 ms, 64 KB 2.0 -> 1.9 ms; forwarding to a local http
server (cache: 'no-store') 16 MB 48.2 -> 24.2 ms, 1 MB 4.0 -> 3.3 ms,
64 KB unchanged. With the HTTP cache writing the proxied body through, the
cache write dominates either way (16 MB ~480 ms on this disk).

Co-authored-by: Shelley Vohr <[email protected]>
…rapper

Review follow-up: SimpleURLLoaderWrapper no longer tracks relay state itself.
A ResponseBody object is the SimpleURLLoader stream consumer; it hands chunks
to its delegate (the wrapper, which emits them to JS) until held, and once
RelayTo() gives it a destination it writes the held and all further chunks
into that client's data pipe and completes the client when the request
completes. The wrapper keeps only the JS-facing events and request lifetime.

Co-authored-by: Shelley Vohr <[email protected]>
ResponseBody::OnComplete() returned before recording the result when the
body had not been held yet, so a handler that awaited between net.fetch()
and returning the response handed RelayTo() a finished request with no
result to complete the client with, and the load hung. Record the result
unconditionally, and when JS has already drained the whole body before
the relay decision, answer with that data instead of a native relay.

Co-authored-by: Shelley Vohr <[email protected]>
@trop
trop Bot requested a review from codebytere September 1, 2026 19:46
@trop trop Bot added 45-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Sep 1, 2026
…tch-responses-from-protocol-handle-without-pumping-them-through-js-1788291929547

Co-authored-by: electron-claude[bot] <279139701+electron-claude[bot]@users.noreply.github.com>

# Conflicts:
#	shell/common/api/electron_api_url_loader.cc
#	shell/common/api/electron_api_url_loader.h
@electron-claude

Copy link
Copy Markdown
Contributor

Claude PR Fix (run) added 1 GitHub-signed commit(s) to this branch through the Git Data API, fast-forwarding it 6ff53fe..b9d71df; no existing history was rewritten.

  • merged 45-x-y @ 61a1eb4 into this branch; conflicts resolved by Claude: yes
  • patches re-applied by Claude after the conflict resolution
  • Linux build (e build, testing config): passed

The new commits were produced by an automated agent (Claude) and re-created by the Electron Claude App so they carry GitHub's signature; each keeps its original author as a Co-authored-by trailer. Review them like any other contribution.

@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 3, 2026 17:57
@MarshallOfSound
MarshallOfSound merged commit 44c55cc into 45-x-y Sep 4, 2026
75 checks passed
@MarshallOfSound
MarshallOfSound deleted the trop/45-x-y-bp-perf-relay-untouched-net-fetch-responses-from-protocol-handle-without-pumping-them-through-js-1788291929547 branch September 4, 2026 06:37
@release-clerk

release-clerk Bot commented Sep 4, 2026

Copy link
Copy Markdown

Release Notes Persisted

Improved throughput of responses that protocol.handle handlers return straight from net.fetch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants