Repository navigation
perf: relay untouched net.fetch responses from protocol.handle without pumping them through JS - #52953
Conversation
MarshallOfSound
left a comment
There was a problem hiding this comment.
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
|
agreed that the short-circuit is the generally useful part - reworking this so an untouched |
…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).
fba3ea3 to
90d5669
Compare
deepak1556
left a comment
There was a problem hiding this comment.
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.
|
@deepak1556 reworked along those lines in the last commit: the stream consumer is now a separate one difference from the InProgressRequest shape: |
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.
|
@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
|
Release Notes Persisted
|
|
I was unable to backport this PR to "43-x-y" cleanly; |
|
I was unable to backport this PR to "42-x-y" cleanly; |
|
I have automatically backported this PR to "44-x-y", please check out #53378 |
|
I have automatically backported this PR to "45-x-y", please check out #53379 |
…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]>
Description of Change
A
protocol.handlehandler that returns whatnet.fetchgave 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.fetch()ing throughprotocol.handle('app', req => net.fetch(...)); Linux x64 release build, medians of interleaved fresh-process runs, p < 0.01 unless markedfile://, 16 MB / 1 MB / 64 KB (15 runs a side)cache: 'no-store', 16 MB / 1 MB / 64 KB (8 a side){ path },Buffer, stream and plainfile://responses (controls)Why: every byte of such a response crossed the browser main thread twice.
SimpleURLLoader's chunks were copied intoArrayBuffers for anIncomingMessage, wrapped into a webReadableStreamfor theResponse, then unwrapped again withReadable.fromWeband written back into a mojo pipe byNodeStreamLoaderin 64 KB slices.What changes: when a handler returns the
Responsenet.fetch produced with its body still untouched - the exact stream net.fetch created, not read, locked, cloned or replaced -protocol.handletells the underlyingSimpleURLLoaderWrapperto 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 returnedResponse(including any the handler set) are used as before; a re-wrapped or re-createdResponse, a body the handler started reading, andResponse.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-speccases 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
npm testpassesRelease Notes
Notes: Improved throughput of responses that
protocol.handlehandlers return straight fromnet.fetch.