Add direct script to embedder channel (#39039)

This PR **removes** `ScriptToConstellationMessage::ForwardToEmbedder`,
and replaces it with an explicit `ScriptToEmbedderChannel`. This new
channel is based on `GenericCallback` and in single-process mode will
directly send the message the to the embedder and wake it. In
multi-process mode, the message is routed via the ROUTER, since waking
is only possible from the same process currently. This means in
multi-process mode there are likely no direct perf benefits, since we
still need to hop the message over the ROUTER (instead of over the
constellation).
In single-process mode we can directly send the message to the embedder,
which should provide a noticable latency improvement in all cases where
script is blocked waiting on the embedder to reply.

This does not change the way the embedder receives messages - the
receiving end is unchanged.

## How was sending messages to the embedder working before?

1. Script wraps it's message to the embedder in
`ScriptToConstellationMessage::ForwardToEmbedder` and sends it to
constellation.
2. The [constellation event loop] receives the message in
[handle_request]
3. If deserialization fails, [an error is logged and the message is
ignored]
4. Since our message came from script, it is handle in
[handle_request_from_script]
5. The message is logged with trace log level
6. If the pipeline is closed, [a warning is logged and the message
ignored]
7. The wrapped `EmbedderMsg` [is forwarded to the embedder]. Sending the
message also invokes `wake()` on the embedder eventloop waker.

[constellation event loop]:
2e1b2e7260/components/constellation/constellation.rs (L755)

[handle request]:
2e1b2e7260/components/constellation/constellation.rs (L1182)

[an error is logged and the message is ignored]:
2e1b2e7260/components/constellation/constellation.rs (L1252)

[handle_request_from_script]:
https://github.com/servo/servo/blob/main/components/constellation/constellation.rs#L1590
 
[a warning is logged and the message ignored]:
2e1b2e7260/components/constellation/constellation.rs (L1599)

[is forwarded to the embedder]:
2e1b2e7260/components/constellation/constellation.rs (L1701)

Testing: Communication between Script and Embedder is extensive, so this
should be covered by existing tests.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-09-02 08:33:44 +02:00 committed by GitHub
parent 97c8c83cbb
commit f4dd2960b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 120 additions and 113 deletions

View file

@ -26,7 +26,7 @@ use content_security_policy::CspList;
use crossbeam_channel::Sender;
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg};
use dom_struct::dom_struct;
use embedder_traits::{EmbedderMsg, JavaScriptEvaluationError};
use embedder_traits::{EmbedderMsg, JavaScriptEvaluationError, ScriptToEmbedderChan};
use fonts::FontContext;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
@ -254,6 +254,11 @@ pub(crate) struct GlobalScope {
#[no_trace]
script_to_constellation_chan: ScriptToConstellationChan,
/// A handle for communicating messages to the Embedder.
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
script_to_embedder_chan: ScriptToEmbedderChan,
/// <https://html.spec.whatwg.org/multipage/#in-error-reporting-mode>
in_error_reporting_mode: Cell<bool>,
@ -741,6 +746,7 @@ impl GlobalScope {
mem_profiler_chan: profile_mem::ProfilerChan,
time_profiler_chan: profile_time::ProfilerChan,
script_to_constellation_chan: ScriptToConstellationChan,
script_to_embedder_chan: ScriptToEmbedderChan,
resource_threads: ResourceThreads,
origin: MutableOrigin,
creation_url: ServoUrl,
@ -770,6 +776,7 @@ impl GlobalScope {
mem_profiler_chan,
time_profiler_chan,
script_to_constellation_chan,
script_to_embedder_chan,
in_error_reporting_mode: Default::default(),
resource_threads,
timers: OnceCell::default(),
@ -2477,8 +2484,12 @@ impl GlobalScope {
&self.script_to_constellation_chan
}
pub(crate) fn script_to_embedder_chan(&self) -> &ScriptToEmbedderChan {
&self.script_to_embedder_chan
}
pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
self.send_to_constellation(ScriptToConstellationMessage::ForwardToEmbedder(msg));
self.script_to_embedder_chan().send(msg).unwrap();
}
pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {