Skip to content

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

Merged
codebytere merged 5 commits into
mainfrom
perf/protocol-file-fast-path
Sep 1, 2026
Merged

codebytere merged 5 commits into
mainfrom
perf/protocol-file-fast-path

Conversation

@codebytere

@codebytere codebytere commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Description of Change

A protocol.handle handler that returns what net.fetch gave it now gets that response relayed to the client natively, for any scheme: forwarding a 16 MB file 40.0 -> 24.1 ms, proxying 16 MB from a local http server 48.2 -> 24.2 ms, 1 MB -13..-17%, small responses unchanged.

sandboxed page fetch()ing through protocol.handle('app', req => net.fetch(...)); Linux x64 release build, medians of interleaved fresh-process runs, p < 0.01 unless marked before after
handler forwards to file://, 16 MB / 1 MB / 64 KB (15 runs a side) 40.0 / 4.5 / 2.0 ms 24.1 / 3.8 / 1.9 ms
handler proxies a local http server, cache: 'no-store', 16 MB / 1 MB / 64 KB (8 a side) 48.2 / 4.0 / 1.75 ms 24.2 / 3.3 / 1.75 ms (64 KB n.s.)
same proxy with the HTTP cache writing the body through ~480 ms ~485 ms (cache write dominates)
{ path }, Buffer, stream and plain file:// responses (controls) unchanged

Why: 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, then unwrapped again with Readable.fromWeb and written back into a mojo pipe by NodeStreamLoader in 64 KB slices.

What changes: 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 tells the underlying SimpleURLLoaderWrapper to stop handing chunks to JS, collects whatever JS had already pulled as a prefix, and hands 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 any the handler set) are used as before; a re-wrapped or re-created Response, a body the handler started reading, and Response.error() all keep the existing path. The response pipe grows to the upstream's declared length (capped at the network service default) and never shrinks below mojo's default.

Testing: seven new api-protocol-spec cases through a local http server and a file (small/large/404 bodies byte for byte with status and headers, gzip upstream decoded, slow chunked stream to the end, file relay, header mutation without touching the body, JS fallback for read/cloned/re-wrapped bodies, client abort mid-body); protocol, net, session and webRequest suites pass.

Checklist

Release Notes

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

@codebytere codebytere added the semver/patch backwards-compatible bug fixes label Aug 18, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 18, 2026

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like an ultra specific hack 🤔 Can we make this work generically for "any" loader (not just file). Allow the user to somehow say "return the result of this fetch" rather than "fetch this in the main proc and restream it". That short-circuit seems generically valuable

@codebytere
codebytere marked this pull request as draft August 18, 2026 10:21
@codebytere

Copy link
Copy Markdown
Member Author

agreed that the short-circuit is the generally useful part - reworking this so an untouched net.fetch response of any scheme hands its loader and body pipe straight to the protocol client instead of being restreamed in the main process; drafting until that is up.

…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).
@codebytere
codebytere force-pushed the perf/protocol-file-fast-path branch from fba3ea3 to 90d5669 Compare August 18, 2026 11:11
@codebytere codebytere changed the title perf: answer large untouched net.fetch file responses from protocol.handle with the file perf: relay untouched net.fetch responses from protocol.handle without pumping them through JS Aug 18, 2026
@codebytere
codebytere marked this pull request as ready for review August 18, 2026 12:59
@codebytere codebytere added target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. labels Aug 18, 2026

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The perf improvement looks great but not a fan of the state tracking introduced, makes the SimpleURLLoaderWrapper implementation harder to reason. There is an existing model that we follow to forward URLLoaderClient, ex: InProgressRequest. Please create an intermediate client that holds onto the body pipe from SimpleURLLoaderWrapper, which can then be instructed to stream the response to JS or transfer the pipe to a destination client.

…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.
@codebytere

codebytere commented Aug 18, 2026 •

Copy link
Copy Markdown
Member Author

@deepak1556 reworked along those lines in the last commit: the stream consumer is now a separate ResponseBody that owns the body end to end - it hands chunks to the wrapper for JS until held, and once RelayTo() gives it a destination it writes the held and all further chunks into that client's pipe and completes it when the request completes. SimpleURLLoaderWrapper is back to JS events plus request lifetime (three delegate methods, no relay state of its own).

one difference from the InProgressRequest shape: network::SimpleURLLoader only exposes the body through the stream-consumer callbacks, not the pipe itself, so ResponseBody forwards chunks into the destination pipe rather than transferring a handle. numbers are unchanged from the previous revision (release build, medians of 8 fresh processes): 16 MB through a handler that returns net.fetch(file:) untouched 43.8 -> 26.5 ms, proxied http with cache: no-store 48.0 -> 24.5 ms, 1 MB 4.6 -> 3.2 ms; small bodies flat.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 19, 2026
@codebytere
codebytere requested a review from deepak1556 August 19, 2026 14:33
@github-actions github-actions Bot added the target/45-x-y PR should also be added to the "45-x-y" branch. label Aug 28, 2026
Comment thread lib/browser/api/protocol.ts Resolved
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.
@deepak1556

Copy link
Copy Markdown
Member

@codebytere feel free to land this, it will take a while for me to address the review feedback in #53263

…st-path

# Conflicts:
#	shell/common/api/electron_api_url_loader.cc
#	shell/common/api/electron_api_url_loader.h
@codebytere
codebytere merged commit e5f5e3d into main Sep 1, 2026
79 checks passed
@release-clerk

release-clerk Bot commented Sep 1, 2026

Copy link
Copy Markdown

Release Notes Persisted

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

@codebytere
codebytere deleted the perf/protocol-file-fast-path branch September 1, 2026 19:45
@github-actions github-actions Bot added the 46-x-y label Sep 1, 2026
@trop

trop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "43-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot removed the target/43-x-y PR should also be added to the "43-x-y" branch. label Sep 1, 2026
@trop

trop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "44-x-y", please check out #53378

@trop trop Bot added in-flight/44-x-y and removed target/44-x-y PR should also be added to the "44-x-y" branch. labels Sep 1, 2026
@trop

trop Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #53379

@trop trop Bot added in-flight/45-x-y and removed target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 1, 2026
MarshallOfSound pushed a commit that referenced this pull request Sep 3, 2026
…t pumping them through JS (#53378)

perf: relay untouched net.fetch responses from protocol.handle without pumping them through JS (#52953)

* perf: relay untouched net.fetch responses from protocol.handle without 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).

* refactor: move response body holding/relaying out of SimpleURLLoaderWrapper

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.

* fix: complete relayed responses whose fetch finished before hold()

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: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. and removed in-flight/44-x-y in-flight/45-x-y labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants