Skip to content

perf: stop routing renderer requests and file loads through the main thread - #53115

Merged
codebytere merged 4 commits into
mainfrom
perf/main-thread-detours
Aug 24, 2026
Merged

codebytere merged 4 commits into
mainfrom
perf/main-thread-detours

Conversation

@codebytere

@codebytere codebytere commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

Description of Change

A renderer's network requests and file:// loads no longer wait for the main process's JavaScript. Today every URLLoaderFactory Electron hands a renderer is fronted by objects bound on the browser UI thread, so each <script>, stylesheet, fetch() or image request is dispatched by the main thread and sits in its queue while app code runs. Measured on Linux x64 release builds, medians of 3 warm loads, with the main process either idle or running 100 ms of JavaScript right after the navigation commits (which is what app startup looks like from the window's side):

idle main process main process busy 100 ms
page with 150 small subresources from a local server, no webRequest listeners 51 -> 46 ms 152 -> 51 ms
same page, session has a protocol.handle() for another scheme 48 -> 49 ms 152 -> 50 ms
same page, any webRequest listener registered (matching or not) 54 -> 54 ms 152 -> 155 ms (unchanged: still via the main thread)
page with 60 small stylesheets from disk (loadFile) 30 -> 28 ms 130 -> 30 ms (200 ms busy: 230 -> 31)
renderer fetch() round trip, no listeners / with a webRequest listener 0.43 -> 0.39 ms / 0.66 -> 0.65 ms
main-process CPU per renderer request, no listeners / with a listener 145 -> 130 us / 330 -> 335 us
main-process CPU per renderer console.log, no console-message listener 22.6 -> 13.4 us

Four commits, each independent:

  1. Keep renderer network requests off the main thread unless they are observed. WillCreateURLLoaderFactory wrapped every factory in a ProxyingURLLoaderFactory on the UI thread, listeners or not. Renderer-facing factories now get a small URLLoaderFactoryGate on the IO thread in front of the proxy: it forwards straight to the network service unless the session has a webRequest listener, an intercepted scheme matching the request, or an --ignore-connections-limit domain, in which case the request goes to the proxy exactly as before. That state is a few words under a lock that api::WebRequest and ProtocolRegistry update, so a listener added at any time applies to the next request without recreating factories (listeners re-run on redirects, which is why their URL filters are not applied at the gate). With a listener registered nothing changes, within noise.
  2. Bind the file:// URL loader factory off the main thread. AsarURLLoaderFactory only posts loaders to the thread pool, so it is now bound on the IO thread; file:// subresources stop queueing behind main-process work.
  3. Skip the trusted-header round trips for requests proxied without webRequest listeners. The proxy put every request in "extraHeaders" mode (two TrustedHeaderClient round trips per request); that is what lets listeners see and modify network-owned headers, so it stays whenever the session has any webRequest listener, and requests that reach the proxy only for an intercepted protocol or an --ignore-connections-limit domain skip it.
  4. Don't build console-message events while nothing listens for them. OnDidAddMessageToConsole created an event object and emitted into JS for every renderer console call; WebContents now keeps a flag from newListener/removeListener and returns early without it.

Checklist

Release Notes

Notes: Page resources, fetch() calls and file:// loads in renderers no longer wait for the main process to be idle when no webRequest listeners or protocol interceptors are registered.

@codebytere codebytere added semver/patch backwards-compatible bug fixes 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 22, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 22, 2026
@codebytere
codebytere force-pushed the perf/main-thread-detours branch 3 times, most recently from 4ef1e05 to cc22f58 Compare August 22, 2026 16:03
@codebytere
codebytere requested a review from deepak1556 August 22, 2026 16:40
@codebytere
codebytere marked this pull request as ready for review August 22, 2026 16:42
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 23, 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.

Can you also update the benchmark table for the non ideal case when there are listeners.

…are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.
AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.
…t webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.
Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.
@codebytere
codebytere force-pushed the perf/main-thread-detours branch from cc22f58 to 7233c86 Compare August 24, 2026 07:54
@codebytere

Copy link
Copy Markdown
Member Author

added the with-listener and intercepted-protocol rows to the table: with any webRequest listener registered the page load, fetch round trip and per-request main-process CPU are unchanged from main (still routed through the main thread), which is the expected non-ideal case.

@codebytere
codebytere requested a review from deepak1556 August 24, 2026 09:24

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

LGTM

@codebytere
codebytere merged commit adc50a1 into main Aug 24, 2026
75 checks passed
@codebytere
codebytere deleted the perf/main-thread-detours branch August 24, 2026 12:02
@release-clerk

release-clerk Bot commented Aug 24, 2026

Copy link
Copy Markdown

Release Notes Persisted

Page resources, fetch() calls and file:// loads in renderers no longer wait for the main process to be idle when no webRequest listeners or protocol interceptors are registered.

@trop

trop Bot commented Aug 24, 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 Aug 24, 2026
@trop

trop Bot commented Aug 24, 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 Aug 24, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot removed the target/42-x-y PR should also be added to the "42-x-y" branch. label Aug 24, 2026
@trop

trop Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "43-x-y", please check out #53142

@trop

trop Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@codebytere has manually backported this PR to "42-x-y", please check out #53143

codebytere added a commit that referenced this pull request Aug 24, 2026
…thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
codebytere added a commit that referenced this pull request Aug 24, 2026
…thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
codebytere added a commit that referenced this pull request Aug 24, 2026
…thread (#53143)

perf: stop routing renderer requests and file loads through the main thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Aug 24, 2026
codebytere added a commit that referenced this pull request Aug 24, 2026
…thread (#53142)

perf: stop routing renderer requests and file loads through the main thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Aug 24, 2026
codebytere added a commit that referenced this pull request Aug 24, 2026
…thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
codebytere added a commit that referenced this pull request Aug 25, 2026
…thread (#53141)

perf: stop routing renderer requests and file loads through the main thread (#53115)

* perf: keep renderer network requests off the main thread unless they are observed

Every URLLoaderFactory handed to a renderer was wrapped in a
ProxyingURLLoaderFactory living on the UI thread, so each subresource
request took a detour through the browser's main thread even when no
webRequest listener or intercepted protocol existed, and waited there
behind whatever JavaScript the app was running. A page with 150 small
subresources loads in 51 ms with an idle main process and 152 ms when
the main process is busy for 100 ms after commit.

Put a small gate on the IO thread in front of the proxy for
renderer-facing factories: it forwards straight to the network service
unless the session currently has a webRequest listener, an intercepted
scheme matching the request, or an --ignore-connections-limit domain,
in which case the request goes to the proxy exactly as before. The
state is a few words under a lock, written by api::WebRequest and
ProtocolRegistry when listeners or handlers change, so adding a
listener takes effect for the next request without recreating
factories. Same page: 46 ms idle, 51 ms with the busy main process;
fetch() round trip 0.43 -> 0.41 ms.

* perf: bind the file:// URL loader factory off the main thread

AsarURLLoaderFactory (Electron's factory for file://, including paths
inside asar archives) was created and bound on the UI thread, although
all it does per request is post an AsarURLLoader to the thread pool. So
every <script>, <link> or image a file:// page loads was dispatched via
the browser's main thread and waited there behind the app's JavaScript:
a page with 60 small stylesheets on disk loads in 30 ms with an idle
main process, 130 ms when the main process is busy for 100 ms after the
navigation commits, 230 ms for 200 ms.

Bind the factory on the IO thread instead. The same page loads
in 28 / 30 / 31 ms.

* perf: skip the trusted-header round trips for requests proxied without webRequest listeners

ProxyingURLLoaderFactory put every proxied request in "extraHeaders"
mode, which makes the network service call back into this process
(TrustedHeaderClient) before sending headers and after receiving them.
That is what lets webRequest listeners see and modify headers the
network stack owns, so it stays whenever the session has any listener;
requests that reach the proxy only because a protocol is intercepted or
a connection-limit domain is configured have nothing to consult there
and now skip the two round trips.

* perf: don't build console-message events while nothing listens for them

Every console call in a renderer reached OnDidAddMessageToConsole, which
created an Event, filled a dictionary and emitted into JS whether or not
the app had a 'console-message' listener: 22.6 us of main-process CPU
per renderer console.log here. WebContents now keeps a flag from JS
(newListener / removeListener) and returns early without it: 13.4 us
per message, i.e. 20,000 renderer log lines cost the main process
269 ms instead of 452.

(cherry picked from commit adc50a1)
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Aug 25, 2026
@codebytere codebytere removed the 45-x-y label Aug 26, 2026
codebytere added a commit that referenced this pull request Aug 26, 2026
…r-only webRequest listeners

The IO-thread gate from #53115 sent every renderer request through the
UI-thread ProxyingURLLoaderFactory as soon as the session had any
webRequest listener, and the proxy itself wrapped every request in an
InProgressRequest for the same reason: a listener may modify the
request and re-runs on every redirect leg, so its URL filter cannot be
applied up front. Two things can be decided up front though:

- A request's resource type never changes across redirects, so the
  listeners' existing `types` filter is honored before the request
  reaches the UI thread: a blocking listener (onBeforeRequest,
  onBeforeSendHeaders, onHeadersReceived) registered with
  types: ['mainFrame', 'subFrame'] no longer pulls images, scripts and
  fetches through the main process.
- onSendHeaders, onBeforeRedirect, onResponseStarted, onCompleted and
  onErrorOccurred cannot change anything. When only such listeners
  match a request's type, it goes straight to the network service and a
  pass-through URLLoaderClient on the IO thread reports the redirect,
  response and completion to api::WebRequest afterwards; the listeners
  get the same details objects (including onSendHeaders per redirect
  leg), the renderer never waits for them.

InterceptState holds two resource-type masks instead of a boolean,
recomputed whenever a listener is added or removed, and both the gate
and the proxy's pass-through path consult them. Linux x64, page with
150 subresources from a local server while the main process is busy for
100 ms after commit: onCompleted + onErrorOccurred listeners 160 -> 55
ms (no listeners: 54 ms); fetch() round trip 0.59 -> 0.46 ms; main
process CPU per request 335 -> 244 us. With onBeforeRequest /
onHeadersReceived limited to frame types, subresources take the direct
path as well (fetch 0.61 -> 0.42 ms, 321 -> 133 us per request); the
document itself is still intercepted, so that page's load event lands
at 108 ms under the same load.
jkleinsc pushed a commit that referenced this pull request Aug 27, 2026
…r-only webRequest listeners (#53204)

* perf: keep requests off the main thread for type-filtered and observer-only webRequest listeners

The IO-thread gate from #53115 sent every renderer request through the
UI-thread ProxyingURLLoaderFactory as soon as the session had any
webRequest listener, and the proxy itself wrapped every request in an
InProgressRequest for the same reason: a listener may modify the
request and re-runs on every redirect leg, so its URL filter cannot be
applied up front. Two things can be decided up front though:

- A request's resource type never changes across redirects, so the
  listeners' existing `types` filter is honored before the request
  reaches the UI thread: a blocking listener (onBeforeRequest,
  onBeforeSendHeaders, onHeadersReceived) registered with
  types: ['mainFrame', 'subFrame'] no longer pulls images, scripts and
  fetches through the main process.
- onSendHeaders, onBeforeRedirect, onResponseStarted, onCompleted and
  onErrorOccurred cannot change anything. When only such listeners
  match a request's type, it goes straight to the network service and a
  pass-through URLLoaderClient on the IO thread reports the redirect,
  response and completion to api::WebRequest afterwards; the listeners
  get the same details objects (including onSendHeaders per redirect
  leg), the renderer never waits for them.

InterceptState holds two resource-type masks instead of a boolean,
recomputed whenever a listener is added or removed, and both the gate
and the proxy's pass-through path consult them. Linux x64, page with
150 subresources from a local server while the main process is busy for
100 ms after commit: onCompleted + onErrorOccurred listeners 160 -> 55
ms (no listeners: 54 ms); fetch() round trip 0.59 -> 0.46 ms; main
process CPU per request 335 -> 244 us. With onBeforeRequest /
onHeadersReceived limited to frame types, subresources take the direct
path as well (fetch 0.61 -> 0.42 ms, 321 -> 133 us per request); the
document itself is still intercepted, so that page's load event lands
at 108 ms under the same load.

* test: type-filtered and observer-only webRequest listeners leave requests on the direct path

* refactor: align observed webRequest loads with network semantics

---------

Co-authored-by: deepak1556 <[email protected]>
MarshallOfSound pushed a commit that referenced this pull request Aug 28, 2026
…r-only webRequest listeners (#53240)

* perf: keep requests off the main thread for type-filtered and observer-only webRequest listeners

The IO-thread gate from #53115 sent every renderer request through the
UI-thread ProxyingURLLoaderFactory as soon as the session had any
webRequest listener, and the proxy itself wrapped every request in an
InProgressRequest for the same reason: a listener may modify the
request and re-runs on every redirect leg, so its URL filter cannot be
applied up front. Two things can be decided up front though:

- A request's resource type never changes across redirects, so the
  listeners' existing `types` filter is honored before the request
  reaches the UI thread: a blocking listener (onBeforeRequest,
  onBeforeSendHeaders, onHeadersReceived) registered with
  types: ['mainFrame', 'subFrame'] no longer pulls images, scripts and
  fetches through the main process.
- onSendHeaders, onBeforeRedirect, onResponseStarted, onCompleted and
  onErrorOccurred cannot change anything. When only such listeners
  match a request's type, it goes straight to the network service and a
  pass-through URLLoaderClient on the IO thread reports the redirect,
  response and completion to api::WebRequest afterwards; the listeners
  get the same details objects (including onSendHeaders per redirect
  leg), the renderer never waits for them.

InterceptState holds two resource-type masks instead of a boolean,
recomputed whenever a listener is added or removed, and both the gate
and the proxy's pass-through path consult them. Linux x64, page with
150 subresources from a local server while the main process is busy for
100 ms after commit: onCompleted + onErrorOccurred listeners 160 -> 55
ms (no listeners: 54 ms); fetch() round trip 0.59 -> 0.46 ms; main
process CPU per request 335 -> 244 us. With onBeforeRequest /
onHeadersReceived limited to frame types, subresources take the direct
path as well (fetch 0.61 -> 0.42 ms, 321 -> 133 us per request); the
document itself is still intercepted, so that page's load event lands
at 108 ms under the same load.

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

* test: type-filtered and observer-only webRequest listeners leave requests on the direct path

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

* refactor: align observed webRequest loads with network semantics

Co-authored-by: deepak1556 <[email protected]>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
Co-authored-by: deepak1556 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants