script: Reduce ScriptThread TLS usage (#38875)

We store a pointer to the ScriptThread singleton for a thread in
thread-local storage. While we don't have yet have profiles pointing to
this TLS access as a hot spot, we can remove a potential performance
footgun without a lot of effort by passing around small pieces of data
that we otherwise need to fetch from the ScriptThread.

Testing: Existing WPT is sufficient
Fixes: part of #37969

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-08-30 12:51:40 -04:00 committed by GitHub
parent d1da1a995c
commit c97ec1b2fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 129 additions and 68 deletions

View file

@ -124,7 +124,7 @@ use crate::dom::cdatasection::CDATASection;
use crate::dom::comment::Comment;
use crate::dom::compositionevent::CompositionEvent;
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::customelementregistry::CustomElementDefinition;
use crate::dom::customelementregistry::{CustomElementDefinition, CustomElementReactionStack};
use crate::dom::customevent::CustomEvent;
use crate::dom::document_event_handler::DocumentEventHandler;
use crate::dom::documentfragment::DocumentFragment;
@ -565,6 +565,10 @@ pub(crate) struct Document {
/// this `Document` will not perform any more rendering updates.
#[no_trace]
current_canvas_epoch: RefCell<Epoch>,
/// The global custom element reaction stack for this script thread.
#[conditional_malloc_size_of]
custom_element_reaction_stack: Rc<CustomElementReactionStack>,
}
#[allow(non_snake_case)]
@ -3261,6 +3265,7 @@ impl Document {
allow_declarative_shadow_roots: bool,
inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
has_trustworthy_ancestor_origin: bool,
custom_element_reaction_stack: Rc<CustomElementReactionStack>,
) -> Document {
let url = url.unwrap_or_else(|| ServoUrl::parse("about:blank").unwrap());
@ -3434,6 +3439,7 @@ impl Document {
resize_observer_started_observing_target: Cell::new(false),
waiting_on_canvas_image_updates: Cell::new(false),
current_canvas_epoch: RefCell::new(Epoch(0)),
custom_element_reaction_stack,
}
}
@ -3536,6 +3542,7 @@ impl Document {
allow_declarative_shadow_roots: bool,
inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
has_trustworthy_ancestor_origin: bool,
custom_element_reaction_stack: Rc<CustomElementReactionStack>,
can_gc: CanGc,
) -> DomRoot<Document> {
Self::new_with_proto(
@ -3557,6 +3564,7 @@ impl Document {
allow_declarative_shadow_roots,
inherited_insecure_requests_policy,
has_trustworthy_ancestor_origin,
custom_element_reaction_stack,
can_gc,
)
}
@ -3581,6 +3589,7 @@ impl Document {
allow_declarative_shadow_roots: bool,
inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
has_trustworthy_ancestor_origin: bool,
custom_element_reaction_stack: Rc<CustomElementReactionStack>,
can_gc: CanGc,
) -> DomRoot<Document> {
let document = reflect_dom_object_with_proto(
@ -3602,6 +3611,7 @@ impl Document {
allow_declarative_shadow_roots,
inherited_insecure_requests_policy,
has_trustworthy_ancestor_origin,
custom_element_reaction_stack,
)),
window,
proto,
@ -3736,6 +3746,7 @@ impl Document {
self.allow_declarative_shadow_roots(),
Some(self.insecure_requests_policy()),
self.has_trustworthy_ancestor_or_current_origin(),
self.custom_element_reaction_stack.clone(),
can_gc,
);
new_doc
@ -4413,6 +4424,10 @@ impl Document {
pub(crate) fn highlighted_dom_node(&self) -> Option<DomRoot<Node>> {
self.highlighted_dom_node.get()
}
pub(crate) fn custom_element_reaction_stack(&self) -> Rc<CustomElementReactionStack> {
self.custom_element_reaction_stack.clone()
}
}
#[allow(non_snake_case)]
@ -4444,6 +4459,7 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
doc.allow_declarative_shadow_roots(),
Some(doc.insecure_requests_policy()),
doc.has_trustworthy_ancestor_or_current_origin(),
doc.custom_element_reaction_stack(),
can_gc,
))
}