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): avoid allocating single-document mutation groups
ldm007276a671fb1
diff --git a/moli-renderer-v8/src/style_engine/runtime_invalidation.rs b/moli-renderer-v8/src/style_engine/runtime_invalidation.rsindex 46eb30302..ee642f675 100644--- a/moli-renderer-v8/src/style_engine/runtime_invalidation.rs+++ b/moli-renderer-v8/src/style_engine/runtime_invalidation.rs@@ -1,7 +1,8 @@-use std::{collections::HashMap, sync::Arc};+use std::{borrow::Cow, collections::HashMap, sync::Arc};use dom::ElementState as StyloElementState;use moli_selector::StyloStyleSourceScope as StyleSourceScope;+use smallvec::SmallVec;use crate::{document_runtime::DomHandle, dom::native::DomHost, protocol_types::EmulatedMediaOverrides,@@ -28,6 +29,8 @@ use super::{const IMMEDIATE_STRUCTURAL_MUTATION_EFFECT_LIMIT: usize = 256;+type StyleMutationGroups<'a> = SmallVec<[(DomHandle, Cow<'a, [StyleMutationEffect]>); 1]>;+impl MoliStyleEngine {pub(crate) fn document_has_style_state(&self, document: DomHandle) -> bool {let Some(world) = self.document_worlds.active_world(document) else {@@ -70,11 +73,12 @@ impl MoliStyleEngine {let mut deferred_us = 0u128;let mut detached_cleanup_us = 0u128;let mut queue_us = 0u128;- for (document, document_effects) in grouped_effects {+ for (document, document_effects) in &grouped_effects {+ let document = *document;+ let document_effects = document_effects.as_ref();let world = self.world_for_document(document);let no_cached_style_started = profile_enabled.then(std::time::Instant::now);- if self.style_invalidation_without_cached_styles_is_complete(&document_effects, &world)- {+ if self.style_invalidation_without_cached_styles_is_complete(document_effects, &world) {no_cached_style_us += no_cached_style_started.map(|started| started.elapsed().as_micros()).unwrap_or_default();@@ -83,21 +87,19 @@ impl MoliStyleEngine {no_cached_style_us += no_cached_style_started.map(|started| started.elapsed().as_micros()).unwrap_or_default();- if should_defer_structural_mutations(&document_effects) {+ if should_defer_structural_mutations(document_effects) {let deferred_started = profile_enabled.then(std::time::Instant::now);world.document_state.bump_target_context_epoch();- world.pending_structural_mutations.push(- &document_effects,- emulated_media,- viewport,- );+ world+ .pending_structural_mutations+ .push(document_effects, emulated_media, viewport);deferred_us += deferred_started.map(|started| started.elapsed().as_micros()).unwrap_or_default();continue;}let detached_cleanup_started = profile_enabled.then(std::time::Instant::now);- self.invalidate_detached_style_subtrees_for_mutations(host, &document_effects);+ self.invalidate_detached_style_subtrees_for_mutations(host, document_effects);detached_cleanup_us += detached_cleanup_started.map(|started| started.elapsed().as_micros()).unwrap_or_default();@@ -106,7 +108,7 @@ impl MoliStyleEngine {document,&world,host,- &document_effects,+ document_effects,emulated_media,viewport,);@@ -614,11 +616,33 @@ fn should_defer_structural_mutations(effects: &[StyleMutationEffect]) -> bool {super::mutation_effect::style_mutation_effects_are_child_list_structural(effects)}-fn style_mutation_effects_by_owner_document(+#[inline]+fn style_mutation_effects_by_owner_document<'a>(+ host: &DomHost,+ effects: &'a [StyleMutationEffect],+) -> StyleMutationGroups<'a> {+ // A single-node effect already belongs to one document. Subtree effects+ // have no single owner here and continue through the batch grouping path.+ if let [effect] = effects+ && let Some(document) = owner_document_for_mutation_effect(host, effect)+ {+ // Borrowing also avoids the inner effect buffer and effect clones;+ // the outer SmallVec only avoids the document-group allocation.+ // A/B on fixed local WeChat HTML (2026-10-06, release, JS/CSS/subframes+ // disabled, 200 balanced randomized pairs, identical outer SmallVec):+ // owned Vec -> borrowed: 161.87 -> 156.24 ms; owned inner SmallVec ->+ // borrowed: 159.61 -> 157.12 ms. Paired-saving 95% bootstrap CIs were+ // [3.15, 6.58] and [0.19, 3.14] ms, respectively, for this workload.+ return SmallVec::from_buf([(document, Cow::Borrowed(effects))]);+ }+ group_batched_style_mutation_effects_by_owner_document(host, effects)+}++fn group_batched_style_mutation_effects_by_owner_document<'a>(host: &DomHost,- effects: &[StyleMutationEffect],-) -> Vec<(DomHandle, Vec<StyleMutationEffect>)> {- let mut groups = Vec::<(DomHandle, Vec<StyleMutationEffect>)>::new();+ effects: &'a [StyleMutationEffect],+) -> StyleMutationGroups<'a> {+ let mut groups = SmallVec::new();for effect in effects {match effect {StyleMutationEffect::ConnectedSubtrees { roots } => {@@ -645,7 +669,7 @@ fn style_mutation_effects_by_owner_document(fn push_subtree_effects_by_owner_document(host: &DomHost,- groups: &mut Vec<(DomHandle, Vec<StyleMutationEffect>)>,+ groups: &mut StyleMutationGroups<'_>,roots: &Arc<[DomHandle]>,make_effect: impl Fn(Arc<[DomHandle]>) -> StyleMutationEffect,) {@@ -681,7 +705,7 @@ fn push_subtree_effects_by_owner_document(}fn push_document_effect(- groups: &mut Vec<(DomHandle, Vec<StyleMutationEffect>)>,+ groups: &mut StyleMutationGroups<'_>,document: DomHandle,effect: StyleMutationEffect,) {@@ -689,9 +713,9 @@ fn push_document_effect(.iter_mut().find(|(group_document, _)| *group_document == document){- group_effects.push(effect);+ group_effects.to_mut().push(effect);} else {- groups.push((document, vec![effect]));+ groups.push((document, Cow::Owned(vec![effect])));}}@@ -868,7 +892,7 @@ mod tests {.find(|(candidate, _)| *candidate == expected_document).expect("each owner document should have a mutation group");assert!(matches!(- effects.as_slice(),+ effects.as_ref(),[StyleMutationEffect::ConnectedSubtrees { roots }]if roots.as_ref() == [expected_root]));