Repository navigation
perf: build the application user agent once - #54206
Conversation
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.
|
@claude review |
There was a problem hiding this comment.
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.
|
Release Notes Persisted
|
|
I have automatically backported this PR to "44-x-y", please check out #54244 |
|
I have automatically backported this PR to "45-x-y", please check out #54245 |
Description of Change
session.getUserAgent()(and everyContentBrowserClient::GetUserAgent()call) rebuilt the full UA string each time: read the app name (a.desktopfile read on Linux), the version, format the product token, thenBuildUserAgentFromProduct. Cache it;app.setName,app.setVersionandapp.setDesktopNameinvalidate it.Testing build, ns/call:
session.getUserAgent()Checklist
npm testpassesRelease Notes
Notes: Improved the performance of
session.getUserAgent().