Open Bug 2098733 Opened 3 hours ago Updated 2 hours ago

Don't clamp to subject if we pass incumbent global the same global

Categories

(Core :: DOM: Core & HTML, task)

task

Tracking

()

ASSIGNED

People

(Reporter: mgaudet, Assigned: mgaudet)

References

(Blocks 1 open bug)

Details

Attachments

(2 external files)

ClampToSubject either returns the passed in global, or the current global. So if you pass in the current global, the whole thing is overhead when getting the incumbent global for promise jobs where we know the JS global and current js global.

CycleCollectedJSContext::getHostDefinedGlobal only wants the incumbent global's
JS object. SpiderMonkey reaches it through getHostDefinedData for every promise
reaction and thenable job it creates (and directly from FinalizationRegistry).
Getting it through GetIncumbentGlobal() turns the scripted caller's JS global
into its nsIGlobalObject and back again.

Give it an accessor that returns the JS object, and move the part of
GetIncumbentGlobal that works from the scripted caller's global into a helper
both can share, so the next patch can skip the round trip.

The accessor takes the JSContext getHostDefinedGlobal is given rather than
looking it up through nsContentUtils::GetCurrentJSContext, which reads the
script settings stack's thread-local and returns null when no JSAPI is active.
The incumbent global is null in that case anyway: there is no scripted caller
and the script settings stack is empty, so ClampToSubject keeps it null.
No behaviour change.

Assignee: nobody → mgaudet
Status: NEW → ASSIGNED

When the scripted caller runs in the current realm, which is the common case
for promise code, GetIncumbentGlobalJSObject converts its global to an
nsIGlobalObject (a QueryInterface and refcount traffic), runs ClampToSubject
(principal lookups and a virtual SubsumesConsideringDomain call), and converts
back with GetGlobalJSObject, only to return the global it started with.

ClampToSubject either keeps the global it is given or substitutes the current
global, so for the current global it is the identity, and every global's
native returns that global from GetGlobalJSObject. A DEBUG assertion checks
that the fast path agrees with the slow one. The global is exposed to active
JS by the read barrier in Realm::maybeGlobal, which JS::GetScriptedCallerGlobal
goes through, as GetGlobalJSObject would have done.

In-browser doxbee-promise, measured on its own (n=10, interleaved): Average -3.2% (95% CI -5.1% to -1.1%).

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: