Skip to content

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

Merged
codebytere merged 1 commit into
44-x-yfrom
bp/44-x-y/53115
Aug 25, 2026
Merged

codebytere merged 1 commit into
44-x-yfrom
bp/44-x-y/53115

Conversation

@codebytere

Copy link
Copy Markdown
Member

Backport of #53115

See that PR for details. Only a member-list context conflict in electron_api_web_contents.h (44-x-y lacks the caret-browsing refcount); resolved by hand.

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 backport This is a backport PR semver/patch backwards-compatible bug fixes 44-x-y labels Aug 24, 2026

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

needs rebase

@codebytere
codebytere enabled auto-merge (squash) August 24, 2026 20:01
…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
codebytere merged commit 433e8d9 into 44-x-y Aug 25, 2026
70 checks passed
@codebytere
codebytere deleted the bp/44-x-y/53115 branch August 25, 2026 05:03
@release-clerk

release-clerk Bot commented Aug 25, 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.

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

Labels

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

Projects

No open projects
Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants