Skip to content

perf: build the application user agent once - #54206

Merged
MarshallOfSound merged 1 commit into
mainfrom
perf/user-agent-cache
Sep 23, 2026
Merged

MarshallOfSound merged 1 commit into
mainfrom
perf/user-agent-cache

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Member

Description of Change

session.getUserAgent() (and every ContentBrowserClient::GetUserAgent() call) rebuilt the full UA string each time: read the app name (a .desktop file read on Linux), the version, format the product token, then BuildUserAgentFromProduct. Cache it; app.setName, app.setVersion and app.setDesktopName invalidate it.

Testing build, ns/call:

call before after
session.getUserAgent() 1399 562

Checklist

Release Notes

Notes: Improved the performance of session.getUserAgent().

session.getUserAgent(), and every request that asks the browser client
for a user agent, rebuilt the whole string on every call: read the
application name (which on Linux opens the .desktop file), read the
version, format the product string, then build the Mozilla/5.0 header
around it.

Cache it, and drop the cache when the name, version or desktop name it
was built from changes. A value built while an invalidation lands is
returned but not stored.

Testing build: session.getUserAgent() 1399 ns -> 562 ns.
@MarshallOfSound MarshallOfSound added semver/none target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 22, 2026
@MarshallOfSound MarshallOfSound self-assigned this Sep 22, 2026
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 22, 2026 18:18
@MarshallOfSound

Copy link
Copy Markdown
Member Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed this PR and didn't find any bugs. Because it introduces a new lock-and-generation-counter caching scheme for a value read from multiple call sites, a human look would still be worthwhile.

What was reviewed: the double-checked-locking pattern in GetApplicationUserAgent()/InvalidateApplicationUserAgent() in shell/common/application_info.cc, and all three invalidation call sites (Browser::SetVersion, Browser::SetName in shell/browser/browser.cc, and App::SetDesktopName in shell/browser/api/electron_api_app.cc). Traced the generation-counter race: a build that starts before an invalidation but finishes after it is detected via the generation mismatch and returned without being cached, so a stale value is never persisted. Confirmed the invalidation calls cover every current input to the user agent (name, version, and the Linux desktop-name env var that GetApplicationName() reads).

Extended reasoning...

Small, self-contained C++ change (4 files) adding a thread-safe lazy cache with a generation counter for the application user-agent string, invalidated from three call sites; no security-sensitive surface (no user input, no IPC/renderer boundary). I traced the locking/generation logic by hand and found it correct, including the stale-build-discarded-not-cached edge case, but it's new concurrency code guarding a value read on potentially different threads (ContentBrowserClient::GetUserAgent), which is the kind of thing worth a second pair of eyes even without a concrete bug found; this is a standalone checkout so I can't verify the threading contract of the upstream ContentBrowserClient::GetUserAgent() call site.

@MarshallOfSound
MarshallOfSound merged commit 65b4061 into main Sep 23, 2026
93 checks passed
@MarshallOfSound
MarshallOfSound deleted the perf/user-agent-cache branch September 23, 2026 20:01
@release-clerk

release-clerk Bot commented Sep 23, 2026

Copy link
Copy Markdown

Release Notes Persisted

Improved the performance of session.getUserAgent().

@trop

trop Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

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

@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 23, 2026
@trop

trop Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/45-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. and removed target/45-x-y PR should also be added to the "45-x-y" branch. in-flight/44-x-y in-flight/45-x-y labels Sep 23, 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/none

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants