Skip to content

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

Merged
jkleinsc merged 4 commits into
mainfrom
perf/webrequest-typed-observers
Aug 27, 2026
Merged

jkleinsc merged 4 commits into
mainfrom
perf/webrequest-typed-observers

Conversation

@codebytere

Copy link
Copy Markdown
Member

Description of Change

Renderer requests no longer wait for the main process when the only webRequest listeners that could match them are observers (onCompleted, onErrorOccurred, onSendHeaders, onBeforeRedirect, onResponseStarted) or are filtered to other resource types. Follow-up to #53115, whose IO-thread gate sent every request through the UI-thread ProxyingURLLoaderFactory as soon as a session had any listener, because a listener may modify the request and re-runs on each redirect leg, so its URL filter cannot be applied up front. Two things can be decided up front:

  • A request's resource type never changes across redirects, so the listeners' existing types filter is honored before the request reaches the UI thread, in the gate and in the proxy's pass-through path. A blocking listener registered with types: ['mainFrame', 'subFrame'] no longer pulls images, scripts and fetch() calls through the main process.
  • Observer listeners cannot change anything. When only observers 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. Listeners receive the same details (including one onSendHeaders per redirect leg); the renderer never waits for them.

InterceptState keeps two resource-type masks (blocking / observing) instead of a boolean, recomputed whenever a listener is added or removed.

Linux x64 release, page with 150 small subresources from a local server in a separate process, median of 3 warm loads; "busy" = the main process runs 100 ms of JavaScript right after the navigation commits. Before -> after:

listeners on the session page, idle page, busy fetch() round trip main-process CPU per request
none 53 -> 50 ms 152 -> 54 ms 0.46 -> 0.40 ms 146 -> 129 µs
onCompleted + onErrorOccurred + onSendHeaders 56 -> 46 ms 160 -> 55 ms 0.63 -> 0.46 ms 342 -> 244 µs
onBeforeRequest + onHeadersReceived, types: ['mainFrame', 'subFrame'] 57 -> 49 ms 161 -> 108 ms 0.60 -> 0.40 ms 309 -> 129 µs
both of the above 57 -> 48 ms 155 -> 108 ms 0.61 -> 0.47 ms 325 -> 236 µs
onBeforeSendHeaders, no filter 58 -> 54 ms 159 -> 163 ms 0.61 -> 0.65 ms 320 -> 342 µs

In the 108 ms rows the subresources are on the direct path but the document request itself matches the frame-typed blocking listeners, so its completion still goes through the main process; an unfiltered blocking listener is unchanged by design.

Checklist

Release Notes

Notes: Renderer network requests no longer waited on the main process when the session's webRequest listeners only observe requests (onCompleted, onErrorOccurred, onSendHeaders, onBeforeRedirect, onResponseStarted) or are filtered to other resource types.

@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 26, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 26, 2026
@codebytere
codebytere requested a review from deepak1556 August 26, 2026 13:56
…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.
@codebytere
codebytere force-pushed the perf/webrequest-typed-observers branch from e0daace to e96aa51 Compare August 26, 2026 17:45
@deepak1556

Copy link
Copy Markdown
Member

Proposed a cleanup in #53216

@codebytere

Copy link
Copy Markdown
Member Author

thanks, pulled it in as 6ce8ae3. reran the table with it: observer-only under the busy main process 55 -> 50 ms, the frame-typed and unfiltered rows unchanged, and the new specs pass here too.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 27, 2026
@jkleinsc
jkleinsc merged commit e151d54 into main Aug 27, 2026
84 checks passed
@release-clerk

release-clerk Bot commented Aug 27, 2026

Copy link
Copy Markdown

Release Notes Persisted

Renderer network requests no longer waited on the main process when the session's webRequest listeners only observe requests (onCompleted, onErrorOccurred, onSendHeaders, onBeforeRedirect, onResponseStarted) or are filtered to other resource types.

@jkleinsc
jkleinsc deleted the perf/webrequest-typed-observers branch August 27, 2026 21:21
@trop

trop Bot commented Aug 27, 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 removed the target/42-x-y PR should also be added to the "42-x-y" branch. label Aug 27, 2026
@trop

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

Copy link
Copy Markdown
Contributor

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

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

Labels

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants