-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
ScriptThread access via TLS is slow #37969
Copy link
Copy link
Open
Labels
A-content/scriptRelated to the script threadRelated to the script threadI-perf-slowUnnecessary performance degredation.Unnecessary performance degredation.
Description
All script thread execution starts in the ScriptThread event loop method, but anytime some code runs that needs to interact with the ScriptThread public APIs we end up fetching a singleton pointer that's stored in thread-local storage (
servo/components/script/script_thread.rs
Lines 162 to 168 in d5d131c
| fn with_optional_script_thread<R>(f: impl FnOnce(Option<&ScriptThread>) -> R) -> R { | |
| SCRIPT_THREAD_ROOT.with(|root| { | |
| f(root | |
| .get() | |
| .and_then(|script_thread| unsafe { script_thread.as_ref() })) | |
| }) | |
| } |
Per #36609 (comment), we know that TLS accesses in hot code paths show up in profiles, so it would be great to find a way to avoid this particular footgun, even if this particular use is not yet causing us troubles.
Some assorted ideas I have had:
- store a pointer to the ScriptThread in Window
- share subsets of data contained in ScriptThread behind
Rc<RefCell<...>>so DOM objects can interact with the data directly instead of calling ScriptThread APIs (Rendering loop: replace theArc<AtomicBool>with aCell<bool>forhas_pending_animation_tickinScriptThread#37946 proposes moving some existing code away from this model) - store a static mutable vector of ScriptThread pointers and store indexes into that vector in Window/other relevant code
- pass
&ScriptThreadarguments into common entrypoints (eg. tasks) so they can be passed down to the code that requires ScriptThread access
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-content/scriptRelated to the script threadRelated to the script threadI-perf-slowUnnecessary performance degredation.Unnecessary performance degredation.