Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

perf(fonts): skip pristine style materialization without sources

ldm0b8fb80345e06
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index b9c728120..d7a56a2ee 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -884,6 +884,15 @@ impl JsContextHost {
.adopted_style_sheet_sources_for_document(document)
}
+ pub(crate) fn document_has_adopted_style_sheet_sources(&self, document: DomHandle) -> bool {
+ self.style_engine
+ .document_has_adopted_style_sheet_sources(document)
+ }
+
+ pub(crate) fn document_has_style_state(&self, document: DomHandle) -> bool {
+ self.style_engine.document_has_style_state(document)
+ }
+
/// Initialize sources when taking ownership of an already constructed DOM.
/// Ordinary reads and synchronization only install lifecycle-prepared sources.
pub(crate) fn initialize_style_element_sources_for_document(&mut self, document: DomHandle) {
@@ -1262,6 +1271,11 @@ impl JsContextHost {
.shadow_root_adopted_style_sheet_sources_with_host(unsafe { &*dom_host }, root)
}
+ pub(crate) fn shadow_root_has_adopted_style_sheet_sources(&self, root: DomHandle) -> bool {
+ self.style_engine
+ .shadow_root_has_adopted_style_sheet_sources(self.dom_host(), root)
+ }
+
pub(crate) fn register_css_custom_property_for_document(
&mut self,
document: DomHandle,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/layout.rs b/moli-renderer-v8/src/native_bridge/context_host/layout.rs
index 585ba3f31..83978dc18 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/layout.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/layout.rs
@@ -522,6 +522,12 @@ impl JsContextHost {
.web_font_resources_are_current(generation)
}
+ pub(crate) fn document_web_font_sidecar_is_pristine(&self) -> bool {
+ self.document_layout_state
+ .borrow()
+ .web_font_sidecar_is_pristine()
+ }
+
pub(crate) fn publish_document_web_font_resource_generation(
&self,
generation: crate::style_engine::StylesheetResourceGeneration,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/layout_state.rs b/moli-renderer-v8/src/native_bridge/context_host/layout_state.rs
index 574eebcd2..dee9842b8 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/layout_state.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/layout_state.rs
@@ -59,6 +59,13 @@ pub(super) struct DocumentLayoutState {
}
impl DocumentLayoutState {
+ /// Both conditions are required: a request can own a slot before its
+ /// stylesheet generation is published, while an empty published manifest
+ /// still records reconciliation state that later mutations must advance.
+ pub(super) fn web_font_sidecar_is_pristine(&self) -> bool {
+ self.web_font_resource_generation.is_none() && self.web_fonts.is_empty()
+ }
+
pub(super) fn web_font_resources_are_current(
&self,
generation: StylesheetResourceGeneration,
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
index d4b340ed4..0311354a3 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
@@ -229,6 +229,28 @@ fn connected_shadow_roots(host: &DomHost, document: DomHandle) -> Vec<DomHandle>
roots
}
+impl JsContextHost {
+ pub(crate) fn document_has_active_author_stylesheet_sources(
+ &self,
+ document: DomHandle,
+ ) -> bool {
+ // Keep detached-source handling aligned with the ordinary document
+ // style observation used by stylesheet resource reconciliation.
+ if !active_stylesheet_handles(self, document, false).is_empty()
+ || self.document_has_adopted_style_sheet_sources(document)
+ {
+ return true;
+ }
+
+ connected_shadow_roots(self.dom_host(), document)
+ .into_iter()
+ .any(|root| {
+ !active_stylesheet_handles(self, root, false).is_empty()
+ || self.shadow_root_has_adopted_style_sheet_sources(root)
+ })
+ }
+}
+
fn document_stylesheet_sources(
runtime: &JsContextHost,
source_document: Option<DomHandle>,
diff --git a/moli-renderer-v8/src/script_vm/layout.rs b/moli-renderer-v8/src/script_vm/layout.rs
index be0404f4b..d09f99f5f 100644
--- a/moli-renderer-v8/src/script_vm/layout.rs
+++ b/moli-renderer-v8/src/script_vm/layout.rs
@@ -257,6 +257,16 @@ impl ScriptVm {
let Some(root) = root else {
return;
};
+ let skip_pristine_document = {
+ let host = self._context_host.borrow();
+ let document = host.document_handle();
+ host.document_web_font_sidecar_is_pristine()
+ && !host.document_has_style_state(document)
+ && !host.document_has_active_author_stylesheet_sources(document)
+ };
+ if skip_pristine_document {
+ return;
+ }
let Some(resources) = crate::layout_renderer::current_native_stylesheet_resources(
&self._context_host.borrow(),
root,
diff --git a/moli-renderer-v8/src/script_vm/tests/extracted/main_document_lifecycle.rs b/moli-renderer-v8/src/script_vm/tests/extracted/main_document_lifecycle.rs
index 66bd05dd2..4858c6b32 100644
--- a/moli-renderer-v8/src/script_vm/tests/extracted/main_document_lifecycle.rs
+++ b/moli-renderer-v8/src/script_vm/tests/extracted/main_document_lifecycle.rs
@@ -2241,3 +2241,116 @@ async fn main_document_replacement_retires_pending_media_and_text_track_sequence
);
assert_eq!(vm.current_main_document_task_owner(), Some(current_owner));
}
+
+#[tokio::test(flavor = "current_thread")]
+async fn web_font_reconciliation_keeps_style_world_lazy_without_active_author_sources() {
+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
+ loader.set_optional_resource_fetch_mask(crate::protocol_types::OptionalResourceFetchMask::FONT);
+ let (mut vm, _resource_completions) =
+ new_parsed_test_vm_with_loader_and_resource_completion_queue(
+ "https://font-reconciliation-fast-path.test/",
+ concat!(
+ "<!doctype html><html><head>",
+ "<link rel='author' href='/not-a-stylesheet.css'>",
+ "</head><body></body></html>",
+ ),
+ &loader,
+ );
+ let document = vm._context_host.borrow().document_handle();
+
+ let rebuilds = vm.retained_style_system_rebuild_count_for_document_for_test(document);
+ let materializations = vm
+ ._context_host
+ .borrow()
+ .style_world_update_materializations_for_test();
+ assert_eq!(vm.document_web_font_counts_for_test(), (0, 0, 0));
+ vm.reconcile_document_web_fonts_for_layout();
+ assert_eq!(
+ vm.retained_style_system_rebuild_count_for_document_for_test(document),
+ rebuilds,
+ "font reconciliation must not build retained styles without an active author source",
+ );
+ assert_eq!(
+ vm._context_host
+ .borrow()
+ .style_world_update_materializations_for_test(),
+ materializations,
+ "font reconciliation must not materialize a style-world update without an active author source",
+ );
+ assert_eq!(vm.document_web_font_counts_for_test(), (0, 0, 0));
+
+ assert_eq!(
+ vm.eval(
+ r#"
+const host = document.createElement('div');
+host.attachShadow({mode: 'open'}).innerHTML = '<style>div { color: red }</style><div></div>';
+document.body.appendChild(host);
+const target = host.shadowRoot.querySelector('div');
+const initial = getComputedStyle(target).color;
+host.remove();
+host.shadowRoot.querySelector('style').remove();
+globalThis.__fontReconciliationDetachedShadow = {host, target};
+initial;
+"#,
+ )
+ .expect("detached shadow style fixture should evaluate"),
+ "rgb(255, 0, 0)",
+ );
+ let materializations = vm
+ ._context_host
+ .borrow()
+ .style_world_update_materializations_for_test();
+ vm.reconcile_document_web_fonts_for_layout();
+ assert!(
+ vm._context_host
+ .borrow()
+ .style_world_update_materializations_for_test()
+ > materializations,
+ "an existing style world must still reconcile after its active sources disappear",
+ );
+ assert_ne!(
+ vm.eval(
+ r#"
+document.body.appendChild(__fontReconciliationDetachedShadow.host);
+getComputedStyle(__fontReconciliationDetachedShadow.target).color;
+"#,
+ )
+ .expect("reconnected shadow style should evaluate"),
+ "rgb(255, 0, 0)",
+ );
+}
+
+#[test]
+fn web_font_active_author_stylesheet_probe_includes_connected_shadow_adopted_sheets() {
+ let mut vm = new_parsed_test_vm(
+ "https://shadow-adopted-font-source.test/",
+ "<!doctype html><html><head></head><body></body></html>",
+ );
+ let document = vm._context_host.borrow().document_handle();
+ vm.eval(
+ r#"
+const host = document.createElement('div');
+const shadow = host.attachShadow({mode: 'open'});
+const sheet = new CSSStyleSheet();
+sheet.replaceSync(':host { color: red; }');
+shadow.adoptedStyleSheets = [sheet];
+globalThis.__detachedShadowStylesheetHost = host;
+"#,
+ )
+ .expect("detached shadow adopted stylesheet fixture should evaluate");
+
+ assert!(
+ !vm._context_host
+ .borrow()
+ .document_has_active_author_stylesheet_sources(document),
+ "a detached shadow scope must not count as an active document source",
+ );
+ vm.eval("document.body.appendChild(__detachedShadowStylesheetHost)")
+ .expect("shadow stylesheet host should connect");
+ assert!(
+ vm._context_host
+ .borrow()
+ .document_has_active_author_stylesheet_sources(document),
+ "a connected shadow adopted stylesheet must prevent the font fast path",
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/web_fonts.rs b/moli-renderer-v8/src/script_vm/web_fonts.rs
index 0fb445315..6e979fadf 100644
--- a/moli-renderer-v8/src/script_vm/web_fonts.rs
+++ b/moli-renderer-v8/src/script_vm/web_fonts.rs
@@ -44,6 +44,10 @@ pub(crate) enum DocumentWebFontCompletion {
}
impl DocumentWebFontState {
+ pub(crate) fn is_empty(&self) -> bool {
+ self.slots.is_empty()
+ }
+
/// Removes registrations whose declarations are no longer in the current
/// Stylo source set. Pending network work may still finish, but its exact
/// request identity will no longer be accepted by [`Self::complete`].
diff --git a/moli-renderer-v8/src/style_engine/mod.rs b/moli-renderer-v8/src/style_engine/mod.rs
index c6aabb1ae..3ab9ff00b 100644
--- a/moli-renderer-v8/src/style_engine/mod.rs
+++ b/moli-renderer-v8/src/style_engine/mod.rs
@@ -204,6 +204,33 @@ impl MoliStyleEngine {
self.document_worlds.documents_with_adopted_style_sheets()
}
+ pub(crate) fn document_has_adopted_style_sheet_sources(&self, document: DomHandle) -> bool {
+ self.document_worlds
+ .active_world(document)
+ .is_some_and(|world| {
+ world
+ .adopted_style_sheet_sources
+ .borrow()
+ .document_source_count(document)
+ != 0
+ })
+ }
+
+ pub(crate) fn shadow_root_has_adopted_style_sheet_sources(
+ &self,
+ host: &DomHost,
+ root: DomHandle,
+ ) -> bool {
+ self.active_owner_document_world(host, root)
+ .is_some_and(|world| {
+ world
+ .adopted_style_sheet_sources
+ .borrow()
+ .shadow_root_source_count(root)
+ != 0
+ })
+ }
+
pub(in crate::style_engine) fn world_for_document(
&self,
document: DomHandle,
diff --git a/moli-renderer-v8/src/style_engine/runtime_invalidation.rs b/moli-renderer-v8/src/style_engine/runtime_invalidation.rs
index 43ff0e2a7..46eb30302 100644
--- a/moli-renderer-v8/src/style_engine/runtime_invalidation.rs
+++ b/moli-renderer-v8/src/style_engine/runtime_invalidation.rs
@@ -29,6 +29,13 @@ use super::{
const IMMEDIATE_STRUCTURAL_MUTATION_EFFECT_LIMIT: usize = 256;
impl MoliStyleEngine {
+ pub(crate) fn document_has_style_state(&self, document: DomHandle) -> bool {
+ let Some(world) = self.document_worlds.active_world(document) else {
+ return false;
+ };
+ world_has_style_state(self, &world)
+ }
+
#[cfg(test)]
pub(crate) fn invalidate_for_mutations(
&mut self,
diff --git a/moli-renderer-v8/src/style_engine/source/adopted.rs b/moli-renderer-v8/src/style_engine/source/adopted.rs
index e179dc4b9..909e1e33f 100644
--- a/moli-renderer-v8/src/style_engine/source/adopted.rs
+++ b/moli-renderer-v8/src/style_engine/source/adopted.rs
@@ -269,6 +269,11 @@ impl AdoptedStyleSheetSources {
.len()
}
+ pub(in crate::style_engine) fn shadow_root_source_count(&self, root: DomHandle) -> usize {
+ self.sources_or_empty(AdoptedStyleSheetOwner::ShadowRoot(root))
+ .len()
+ }
+
pub(in crate::style_engine) fn document_source_ids(
&self,
document: DomHandle,