Use GenericChannel for script_chan (#38645)

Motivation: 
Using our GenericChannel abstraction allows us to optimize IPC in
single-process mode to just use cross-beam channel.
To keep the diff low, and get early feedback, this PR only tackles a
single channel, but the intention is to port all ipc channels to the
generic channel, which allows us to skip serializing and deserializing
messages in single process mode.

Based on: 
- https://github.com/servo/servo/pull/38638
- https://github.com/servo/servo/pull/38636

Testing: Covered by existing tests

---------

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-08-19 11:59:20 +02:00 committed by GitHub
parent 73e0f2f7e6
commit 8587536755
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 36 additions and 22 deletions

View file

@ -20,6 +20,7 @@ malloc_size_of = { workspace = true }
malloc_size_of_derive = { workspace = true }
parking_lot = { workspace = true }
serde = { workspace = true }
servo_config = { path = "../../config" }
time = { workspace = true }
webrender_api = { workspace = true }

View file

@ -7,6 +7,7 @@
use std::fmt;
use ipc_channel::router::ROUTER;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
static GENERIC_CHANNEL_USAGE_ERROR_PANIC_MSG: &str = "May not send a crossbeam channel over an IPC channel. \
@ -65,6 +66,12 @@ impl<T: Serialize> GenericSender<T> {
}
}
impl<T: Serialize> MallocSizeOf for GenericSender<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
}
}
#[derive(Debug)]
pub struct SendError;
pub type SendResult = Result<(), SendError>;
@ -145,11 +152,11 @@ where
/// Creates a Servo channel that can select different channel implementations based on multiprocess
/// mode or not. If the scenario doesn't require message to pass process boundary, a simple
/// crossbeam channel is preferred.
pub fn channel<T>(multiprocess: bool) -> Option<(GenericSender<T>, GenericReceiver<T>)>
pub fn channel<T>() -> Option<(GenericSender<T>, GenericReceiver<T>)>
where
T: for<'de> Deserialize<'de> + Serialize,
{
if multiprocess {
if servo_config::opts::get().multiprocess {
ipc_channel::ipc::channel()
.map(|(tx, rx)| (GenericSender::Ipc(tx), GenericReceiver::Ipc(rx)))
.ok()

View file

@ -33,7 +33,7 @@ pub fn webgl_channel<T>() -> Option<(WebGLSender<T>, WebGLReceiver<T>)>
where
T: for<'de> Deserialize<'de> + Serialize,
{
base::generic_channel::channel(servo_config::opts::get().multiprocess)
base::generic_channel::channel()
}
/// Entry point channel type used for sending WebGLMsg messages to the WebGL renderer.

View file

@ -20,6 +20,7 @@ use std::thread::JoinHandle;
use app_units::Au;
use atomic_refcell::AtomicRefCell;
use base::Epoch;
use base::generic_channel::GenericSender;
use base::id::{BrowsingContextId, PipelineId, WebViewId};
use bitflags::bitflags;
use compositing_traits::CrossProcessCompositorApi;
@ -30,7 +31,6 @@ use euclid::default::{Point2D as UntypedPoint2D, Rect};
use fnv::FnvHashMap;
use fonts::{FontContext, SystemFontServiceProxy};
use fxhash::FxHashMap;
use ipc_channel::ipc::IpcSender;
pub use layout_damage::LayoutDamage;
use libc::c_void;
use malloc_size_of::{MallocSizeOf as MallocSizeOfTrait, MallocSizeOfOps, malloc_size_of_is_0};
@ -197,7 +197,7 @@ pub struct LayoutConfig {
pub webview_id: WebViewId,
pub url: ServoUrl,
pub is_iframe: bool,
pub script_chan: IpcSender<ScriptThreadMessage>,
pub script_chan: GenericSender<ScriptThreadMessage>,
pub image_cache: Arc<dyn ImageCache>,
pub font_context: Arc<FontContext>,
pub time_profiler_chan: time::ProfilerChan,

View file

@ -15,6 +15,7 @@ use std::sync::Arc;
use background_hang_monitor_api::BackgroundHangMonitorRegister;
use base::cross_process_instant::CrossProcessInstant;
use base::generic_channel::{GenericReceiver, GenericSender};
use base::id::{BrowsingContextId, HistoryStateId, PipelineId, PipelineNamespaceId, WebViewId};
#[cfg(feature = "bluetooth")]
use bluetooth_traits::BluetoothRequest;
@ -307,9 +308,9 @@ pub struct InitialScriptState {
/// Loading into a Secure Context
pub inherited_secure_context: Option<bool>,
/// A channel with which messages can be sent to us (the script thread).
pub constellation_sender: IpcSender<ScriptThreadMessage>,
pub constellation_sender: GenericSender<ScriptThreadMessage>,
/// A port on which messages sent by the constellation to script can be received.
pub constellation_receiver: IpcReceiver<ScriptThreadMessage>,
pub constellation_receiver: GenericReceiver<ScriptThreadMessage>,
/// A channel on which messages can be sent to the constellation from script.
pub pipeline_to_constellation_sender: ScriptToConstellationChan,
/// A handle to register script-(and associated layout-)threads for hang monitoring.