Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
perf(style): narrow nth-of invalidation roots
ldm0dceb3ad3417d
diff --git a/moli-renderer-v8/src/style_engine/target_queries.rs b/moli-renderer-v8/src/style_engine/target_queries.rsindex 74d53e1f2..7f0a0abef 100644--- a/moli-renderer-v8/src/style_engine/target_queries.rs+++ b/moli-renderer-v8/src/style_engine/target_queries.rs@@ -465,6 +465,11 @@ impl PendingStyleInvalidationTargetQueries {self.structural_boundary_cleanup_roots().len()}+ #[cfg(test)]+ pub(super) fn structural_boundary_cleanup_roots_for_test(&self) -> &IndexSet<DomHandle> {+ &self.structural_boundary_cleanup_roots().roots+ }+fn fallback_reasons(&self) -> &IndexSet<StyloSourceInvalidationFallbackReason> {&self.context.fallback_reasons}diff --git a/moli-renderer-v8/src/style_engine/tests/dependency.rs b/moli-renderer-v8/src/style_engine/tests/dependency.rsindex f7a787a2a..a9d6929d0 100644--- a/moli-renderer-v8/src/style_engine/tests/dependency.rs+++ b/moli-renderer-v8/src/style_engine/tests/dependency.rs@@ -673,7 +673,7 @@ fn pending_cause_default_roots_do_not_get_source_scope_reason() {assert!(target_query.fallback_reasons_for_test().is_empty());}#[test]-fn source_dependency_plan_translation_preserves_exact_queries_fallback_roots_and_reason() {+fn source_dependency_plan_translation_preserves_exact_queries_and_nth_structural_roots() {let mut host = test_host();let root = host.document_handle();let parent = host.create_element("section");@@ -734,18 +734,24 @@ fn source_dependency_plan_translation_preserves_exact_queries_fallback_roots_andassert!(target_query.retained_queries_for_test()- .is_some_and(|queries| queries.contains(&exact_query))+ .is_some_and(|queries| queries.contains(&exact_query) && queries.contains(&nth_query)));assert!(target_query- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&nth_root));assert!(target_query- .fallback_reasons_for_test()- .contains(&StyloSourceInvalidationFallbackReason::NthOfDependency)+ .structural_boundary_cleanup_roots_for_test()+ .contains(&later));+ assert!(+ target_query+ .reasoned_fallback_root_set_for_test()+ .is_empty()+ );+ assert!(target_query.fallback_reasons_for_test().is_empty());}#[test]fn source_dependency_plan_translation_preserves_required_source_fallback_reason() {@@ -1142,7 +1148,7 @@ fn source_dependency_request_translation_prefers_cause_fallback_roots() {}#[test]-fn source_dependency_request_translation_uses_context_roots_for_nth_of_dependency() {+fn source_dependency_request_translation_uses_exact_structural_roots_for_nth_of_dependency() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -1188,31 +1194,37 @@ fn source_dependency_request_translation_uses_context_roots_for_nth_of_dependencassert_eq!(target_queries.len(), 1);assert_eq!(target_queries[0].kind(),- StyloRetainedSourceStyleInvalidationKind::ContextFallback+ StyloRetainedSourceStyleInvalidationKind::RetainedQueries);assert!(target_queries[0]- .fallback_reasons_for_test()- .contains(&StyloSourceInvalidationFallbackReason::NthOfDependency)+ .retained_queries_for_test()+ .is_some_and(|queries| queries.contains(&query)));+ assert!(target_queries[0].fallback_reasons_for_test().is_empty());assert!(target_queries[0].reasoned_fallback_root_set_for_test()+ .is_empty()+ );+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test().contains(&middle));assert!(target_queries[0]- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&target));assert!(!target_queries[0]- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&first));assert!(!target_queries[0]- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&document));}@@ -1296,7 +1308,7 @@ fn source_dependency_request_translation_tries_nested_relative_with_context_safe}#[test]-fn source_dependency_request_translation_keeps_nth_fallback_while_trying_nested_relative_exactly() {+fn source_dependency_request_translation_keeps_nth_and_nested_relative_queries_exact() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -1348,10 +1360,10 @@ fn source_dependency_request_translation_keeps_nth_fallback_while_trying_nested_assert!(target_queries[0].retained_queries_for_test()- .is_some_and(|queries| !queries.contains(&nth_query) && queries.contains(&nested_query))+ .is_some_and(|queries| queries.contains(&nth_query) && queries.contains(&nested_query)));let reasons = target_queries[0].fallback_reasons_for_test();- assert!(reasons.contains(&StyloSourceInvalidationFallbackReason::NthOfDependency));+ assert!(!reasons.contains(&StyloSourceInvalidationFallbackReason::NthOfDependency));assert!(!reasons.contains(&StyloSourceInvalidationFallbackReason::NestedRelativeSelectorDependency));@@ -1360,6 +1372,16 @@ fn source_dependency_request_translation_keeps_nth_fallback_while_trying_nested_.reasoned_fallback_root_set_for_test().contains(&document));+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&middle)+ );+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&target)+ );assert!(!target_queries[0].exact_safety_fallback_root_set_for_test()@@ -1368,7 +1390,7 @@ fn source_dependency_request_translation_keeps_nth_fallback_while_trying_nested_}#[test]-fn source_dependency_request_translation_preserves_context_fallback_kind_with_retained_queries() {+fn source_dependency_request_translation_combines_exact_and_nth_structural_roots() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -1434,23 +1456,24 @@ fn source_dependency_request_translation_preserves_context_fallback_kind_with_retarget_queries[0].kind(),StyloRetainedSourceStyleInvalidationKind::RetainedQueries);- assert_eq!(target_queries[0].retained_query_count(), 1);+ assert_eq!(target_queries[0].retained_query_count(), 2);+ assert!(target_queries[0].fallback_reasons_for_test().is_empty());assert!(target_queries[0]- .fallback_reasons_for_test()- .contains(&StyloSourceInvalidationFallbackReason::NthOfDependency)+ .structural_boundary_cleanup_roots_for_test()+ .contains(&middle)+ );+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&target));let retained_source_input = retained_source_input_for_test(target_queries[0].retained_source_invalidation_input().into_stylo_input(None),);- assert_eq!(- retained_source_input.retained_fallback_kind,- Some(Some(- StyloRetainedSourceStyleInvalidationKind::ContextFallback- ))- );+ assert_eq!(retained_source_input.retained_fallback_kind, Some(None));let mut engine = MoliStyleEngine::new();let source = StyloStylesheetSource::new(style_text.into(), base_url.clone())@@ -1477,13 +1500,13 @@ fn source_dependency_request_translation_preserves_context_fallback_kind_with_reassert_eq!(application.diagnostic_target_results().len(), 1);assert_eq!(application.diagnostic_target_results()[0].kind(),- StyleInvalidationDiagnosticTargetResultKind::ContextFallback+ StyleInvalidationDiagnosticTargetResultKind::Exact);assert_eq!(application.diagnostic_target_result_summary().context_fallback_target_count(),- 1+ 0);assert_eq!(application@@ -1494,8 +1517,7 @@ fn source_dependency_request_translation_preserves_context_fallback_kind_with_re}#[test]-fn source_dependency_request_translation_does_not_merge_exact_context_roots_into_context_fallback()-{+fn source_dependency_request_translation_separates_query_safety_from_nth_structural_roots() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -1577,18 +1599,24 @@ fn source_dependency_request_translation_does_not_merge_exact_context_roots_intoassert!(target_query.retained_queries_for_test()- .is_some_and(|queries| queries.contains(&exact_query))+ .is_some_and(|queries| queries.contains(&exact_query) && queries.contains(&nth_query))+ );+ assert!(target_query.fallback_reasons_for_test().is_empty());+ assert!(+ !target_query+ .structural_boundary_cleanup_roots_for_test()+ .contains(&exact_context_only),+ "ordinary query safety roots must not become unconditional structural cleanup roots");assert!(target_query- .fallback_reasons_for_test()- .contains(&StyloSourceInvalidationFallbackReason::NthOfDependency)+ .structural_boundary_cleanup_roots_for_test()+ .contains(&nth_subject));assert!(- !target_query- .reasoned_fallback_root_set_for_test()- .contains(&exact_context_only),- "exact sibling query fallback roots must not be merged into the context fallback roots for another request"+ target_query+ .structural_boundary_cleanup_roots_for_test()+ .contains(&nth_later));assert!(target_query@@ -1599,7 +1627,7 @@ fn source_dependency_request_translation_does_not_merge_exact_context_roots_into}#[test]-fn retained_unavailable_uses_exact_safety_roots_without_broadening_context_fallback() {+fn retained_unavailable_uses_exact_safety_roots_without_broadening_nth_structural_cleanup() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -1694,14 +1722,18 @@ fn retained_unavailable_uses_exact_safety_roots_without_broadening_context_fallb.contains(&StyloSourceInvalidationFallbackReason::MissingRetainedStyleSystem));assert!(- target_result+ !target_result.fallback_reasons().contains(&StyloSourceInvalidationFallbackReason::NthOfDependency));assert!(matches!(application.cleanup_target(),- StyleInvalidationCleanupTarget::SourceFallbackSubtreeRoots(roots)- if roots.contains(&exact_context_only) && !roots.contains(&document)+ StyleInvalidationCleanupTarget::MixedSubtreeRoots(groups)+ if groups.source_fallback_roots().contains(&exact_context_only)+ && groups.structural_boundary_roots().contains(&nth_subject)+ && groups.structural_boundary_roots().contains(&nth_later)+ && !groups.source_fallback_roots().contains(&document)+ && !groups.structural_boundary_roots().contains(&document)));}@@ -2213,7 +2245,7 @@ fn custom_state_source_dependency_request_stays_exact_without_unsupported_fallba}#[test]-fn custom_state_nth_of_source_dependency_request_uses_context_fallback_roots() {+fn custom_state_nth_of_source_dependency_request_uses_exact_structural_roots() {let mut host = test_host();let document = host.document_handle();let parent = host.create_element("section");@@ -2270,36 +2302,46 @@ fn custom_state_nth_of_source_dependency_request_uses_context_fallback_roots() {let target_query = &target_queries[0];assert_eq!(target_query.kind(),- StyloRetainedSourceStyleInvalidationKind::ContextFallback+ StyloRetainedSourceStyleInvalidationKind::RetainedQueries);assert!(target_query- .reasoned_fallback_root_set_for_test()+ .retained_queries_for_test()+ .is_some_and(|queries| queries.contains(&custom_state_query)),+ "{target_query:?}"+ );+ assert!(+ target_query+ .structural_boundary_cleanup_roots_for_test()+ .contains(&root),+ "{target_query:?}"+ );+ assert!(+ target_query+ .structural_boundary_cleanup_roots_for_test().contains(&adjacent),"{target_query:?}");assert!(target_query- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&later),"{target_query:?}");assert!(target_query- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&later_adjacent),"{target_query:?}");assert!(!target_query- .reasoned_fallback_root_set_for_test()+ .structural_boundary_cleanup_roots_for_test().contains(&document),"{target_query:?}");assert!(- target_query- .fallback_reasons_for_test()- .contains(&StyloSourceInvalidationFallbackReason::NthOfDependency),+ target_query.fallback_reasons_for_test().is_empty(),"{target_query:?}");}diff --git a/moli-renderer-v8/src/style_engine/tests/invalidator.rs b/moli-renderer-v8/src/style_engine/tests/invalidator.rsindex 4a05d8eb8..4ea6372fd 100644--- a/moli-renderer-v8/src/style_engine/tests/invalidator.rs+++ b/moli-renderer-v8/src/style_engine/tests/invalidator.rs@@ -3518,6 +3518,242 @@ fn retained_stylo_invalidator_uses_snapshots_for_mixed_attribute_target_batches(2);}+#[test]+fn nth_of_child_list_insertion_invalidates_only_changed_and_following_sibling_subtrees() {+ let mut host = test_host();+ let document = host.document_handle();+ let container = host.create_element("section");+ let first = host.create_element("div");+ let inserted = host.create_element("div");+ let target = host.create_element("div");+ let later = host.create_element("div");+ let unrelated = host.create_element("aside");++ for handle in [first, inserted, target, later] {+ assert!(host.set_attribute(handle, "class", "active"));+ }+ assert!(host.set_attribute(unrelated, "id", "unrelated"));+ assert!(host.append_child(document, container));+ assert!(host.append_child(container, first));+ assert!(host.append_child(container, target));+ assert!(host.append_child(container, later));+ assert!(host.append_child(document, unrelated));++ let mut engine = MoliStyleEngine::new();+ let document_url = url::Url::parse("https://example.test/").unwrap();+ let source = StyloStylesheetSource::new(+ "section > div:nth-child(odd of .active) {+ background-color: rgb(192, 192, 192);+ }+ #unrelated { color: rgb(1, 2, 3); }"+ .into(),+ document_url.clone(),+ );+ engine.set_document_adopted_style_sheet_sources(document, vec![source.clone()]);+ let mut inputs = FullStyleWorldSnapshot::default();+ inputs.document_stylesheet_sources.push(source);++ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ target,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgba(0, 0, 0, 0)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ later,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(192, 192, 192)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ unrelated,+ "color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(1, 2, 3)".into())+ );++ assert!(host.insert_before(container, inserted, Some(target)));+ let media = crate::protocol_types::EmulatedMediaOverrides::default();+ engine.invalidate_for_mutations(+ &host,+ &[StyleMutationEffect::ChildList {+ parent: container,+ added_nodes: vec![inserted],+ removed_nodes: Vec::new(),+ removed_element_snapshots: Vec::new(),+ previous_sibling: Some(first),+ next_sibling: Some(target),+ }],+ &media,+ );+ engine.drain_pending_style_invalidations_for_document_for_test(&host, document);++ assert!(!engine.computed_style_cache_contains_handle_for_document_for_test(document, target));+ assert!(!engine.computed_style_cache_contains_handle_for_document_for_test(document, later));+ assert!(engine.computed_style_cache_contains_handle_for_document_for_test(document, unrelated));+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ target,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(192, 192, 192)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ later,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgba(0, 0, 0, 0)".into())+ );+}++#[test]+fn nth_of_child_list_removal_invalidates_only_following_sibling_subtrees() {+ let mut host = test_host();+ let document = host.document_handle();+ let container = host.create_element("section");+ let first = host.create_element("div");+ let removed = host.create_element("div");+ let target = host.create_element("div");+ let later = host.create_element("div");+ let unrelated = host.create_element("aside");++ for handle in [first, removed, target, later] {+ assert!(host.set_attribute(handle, "class", "active"));+ }+ assert!(host.set_attribute(unrelated, "id", "unrelated"));+ assert!(host.append_child(document, container));+ assert!(host.append_child(container, first));+ assert!(host.append_child(container, removed));+ assert!(host.append_child(container, target));+ assert!(host.append_child(container, later));+ assert!(host.append_child(document, unrelated));++ let mut engine = MoliStyleEngine::new();+ let document_url = url::Url::parse("https://example.test/").unwrap();+ let source = StyloStylesheetSource::new(+ "section > div:nth-child(odd of .active) {+ background-color: rgb(192, 192, 192);+ }+ #unrelated { color: rgb(1, 2, 3); }"+ .into(),+ document_url.clone(),+ );+ engine.set_document_adopted_style_sheet_sources(document, vec![source.clone()]);+ let mut inputs = FullStyleWorldSnapshot::default();+ inputs.document_stylesheet_sources.push(source);++ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ target,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(192, 192, 192)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ later,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgba(0, 0, 0, 0)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ unrelated,+ "color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(1, 2, 3)".into())+ );++ let removed_snapshots = removed_element_dependency_snapshots(&host, &[removed]);+ assert!(host.remove_child(container, removed));+ let media = crate::protocol_types::EmulatedMediaOverrides::default();+ engine.invalidate_for_mutations(+ &host,+ &[StyleMutationEffect::ChildList {+ parent: container,+ added_nodes: Vec::new(),+ removed_nodes: vec![removed],+ removed_element_snapshots: removed_snapshots,+ previous_sibling: Some(first),+ next_sibling: Some(target),+ }],+ &media,+ );+ engine.drain_pending_style_invalidations_for_document_for_test(&host, document);++ assert!(!engine.computed_style_cache_contains_handle_for_document_for_test(document, target));+ assert!(!engine.computed_style_cache_contains_handle_for_document_for_test(document, later));+ assert!(engine.computed_style_cache_contains_handle_for_document_for_test(document, unrelated));+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ target,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgba(0, 0, 0, 0)".into())+ );+ assert_eq!(+ engine.computed_style_property_value(+ &host,+ &document_url,+ later,+ "background-color",+ None,+ &inputs,+ None,+ ),+ Some("rgb(192, 192, 192)".into())+ );+}+#[test]fn retained_stylo_invalidator_narrows_child_list_inserted_sibling_invalidation() {let mut host = test_host();@@ -4929,7 +5165,7 @@ fn retained_stylo_invalidator_refreshes_nth_child_of_selector_list_on_class_chan}#[test]-fn retained_stylo_invalidator_refreshes_nth_child_of_custom_state_with_context_fallback() {+fn retained_stylo_invalidator_refreshes_nth_child_of_custom_state_with_structural_roots() {let mut host = test_host();let document = host.document_handle();let body = host.create_element("body");@@ -5019,9 +5255,24 @@ fn retained_stylo_invalidator_refreshes_nth_child_of_custom_state_with_context_fassert_eq!(target_queries.len(), 1, "{target_queries:#?}");assert_eq!(target_queries[0].kind(),- StyloRetainedSourceStyleInvalidationKind::ContextFallback,+ StyloRetainedSourceStyleInvalidationKind::RetainedQueries,"{target_queries:#?}");+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&middle)+ );+ assert!(+ target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&target)+ );+ assert!(+ !target_queries[0]+ .structural_boundary_cleanup_roots_for_test()+ .contains(&unrelated)+ );let application = engine.with_retained_style_system_for_document_for_test(document, |retained| {retained_source_invalidation_outcome_for_document_for_test(@@ -5037,20 +5288,30 @@ fn retained_stylo_invalidator_refreshes_nth_child_of_custom_state_with_context_f.finalize(&host);assert_eq!(application.cleanup_target_kind(),- StyleInvalidationCleanupTargetKind::SourceFallbackSubtreeRoots+ StyleInvalidationCleanupTargetKind::MixedSubtreeRoots);assert!(matches!(application.cleanup_target(),- StyleInvalidationCleanupTarget::SourceFallbackSubtreeRoots(roots)- if roots.contains(&target) && !roots.contains(&unrelated)+ StyleInvalidationCleanupTarget::MixedSubtreeRoots(groups)+ if groups.structural_boundary_roots().contains(&target)+ && !groups.structural_boundary_roots().contains(&unrelated)+ && !groups.exact_affected_roots().contains(&unrelated)));assert_eq!(application.diagnostic_target_results().len(), 1);- assert!(!application.diagnostic_target_results()[0].exact());+ assert!(application.diagnostic_target_results()[0].exact());assert!(- application.diagnostic_target_results()[0]+ !application.diagnostic_target_results()[0].fallback_reasons().contains(&StyloSourceInvalidationFallbackReason::NthOfDependency));+ let telemetry = application.fallback_telemetry();+ assert_eq!(telemetry.exact_target_result_count(), 1);+ assert_eq!(telemetry.fallback_target_result_count(), 0);+ assert_eq!(+ telemetry+ .fallback_reason_target_count(StyloSourceInvalidationFallbackReason::NthOfDependency,),+ 0+ );engine.invalidate_for_custom_state_change(&host,diff --git a/moli-selector/src/stylo/invalidation.rs b/moli-selector/src/stylo/invalidation.rsindex f3fe91b85..40e7a7087 100644--- a/moli-selector/src/stylo/invalidation.rs+++ b/moli-selector/src/stylo/invalidation.rs@@ -753,9 +753,9 @@ pub fn stylo_source_invalidation_fallback_reason_plan(}StyloSourceInvalidationFallbackReason::NthOfDependency => {StyloSourceInvalidationFallbackReasonPlan {- owner: "Stylo nth-of dependency planner",- missing_fact: "sibling/selector-list roots for :nth-child(... of ...)",- next_work_item: "return exact sibling traversal roots for nth-of dependencies",+ owner: "Moli mutation-context capture",+ missing_fact: "mutation-time sibling context for an nth-of query that reached the planner without it",+ next_work_item: "attach sibling context to the remaining context-free nth-of invalidation entry points",}}StyloSourceInvalidationFallbackReason::NestedRelativeSelectorDependency => {