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(dom): compact singleton named candidates
ldm0dd24293221a5
diff --git a/moli-dom/src/native/host.rs b/moli-dom/src/native/host.rsindex e16ce2661..b83f3adae 100644--- a/moli-dom/src/native/host.rs+++ b/moli-dom/src/native/host.rs@@ -37,8 +37,8 @@ pub use self::mutation::{use self::query_index::ElementQueryIndex;use self::types::{CachedConnectedShadowRoots, CachedLiveCollection, LiveCollectionCacheKey,- LiveCollectionCacheKind, MutationScope, NamedElementIndex, ShadowRootState,- ShadowSlotNameIndex, ThinArcStr,+ LiveCollectionCacheKind, MutationScope, NamedElementHandles, NamedElementIndex,+ ShadowRootState, ShadowSlotNameIndex, ThinArcStr,};pub use self::types::{ConnectedShadowRootSnapshot, DomHandle, DomHost};pub use self::types::{HostElementSnapshot, ShadowRootBindingSnapshot, ShadowRootInit};diff --git a/moli-dom/src/native/host/document.rs b/moli-dom/src/native/host/document.rsindex f3c40393f..61bafdaba 100644--- a/moli-dom/src/native/host/document.rs+++ b/moli-dom/src/native/host/document.rs@@ -307,14 +307,10 @@ impl DomHost {return;}if let Some(previous_value) = index.value_by_handle.remove(&handle) {- let remove_key =- index- .handles_by_value- .get_mut(&previous_value)- .is_some_and(|handles| {- handles.swap_remove(&handle);- handles.is_empty()- });+ let remove_key = index+ .handles_by_value+ .get_mut(&previous_value)+ .is_some_and(|handles| handles.remove(handle));if remove_key {index.handles_by_value.remove(&previous_value);}@@ -324,7 +320,14 @@ impl DomHost {.handles_by_value.entry(ThinArcStr::from(current_value));let canonical_value = entry.key().clone();- entry.or_default().insert(handle);+ match entry {+ std::collections::hash_map::Entry::Occupied(mut entry) => {+ entry.get_mut().insert(handle);+ }+ std::collections::hash_map::Entry::Vacant(entry) => {+ entry.insert(NamedElementHandles::One(handle));+ }+ }index.value_by_handle.insert(handle, canonical_value);}}@@ -464,7 +467,7 @@ impl DomHost {fn first_current_named_candidate(&self,- candidates: &IndexSet<DomHandle>,+ candidates: &NamedElementHandles,key: &str,read_value: impl Fn(&Element) -> Option<&str>,) -> Option<DomHandle> {diff --git a/moli-dom/src/native/host/query_index.rs b/moli-dom/src/native/host/query_index.rsindex 26e234662..ee18e3920 100644--- a/moli-dom/src/native/host/query_index.rs+++ b/moli-dom/src/native/host/query_index.rs@@ -1022,6 +1022,42 @@ mod tests {);}+ #[test]+ fn named_candidate_index_expands_and_compacts_duplicate_values() {+ let mut host = test_host();+ let first = host.create_element("div");+ let second = host.create_element("div");+ assert!(host.set_attribute(first, "id", "duplicate"));+ assert!(host.append_child(host.document_handle(), first));+ assert_eq!(host.element_handle_by_id("duplicate"), Some(first));+ assert!(matches!(+ host.id_index+ .borrow()+ .as_ref()+ .and_then(|index| index.handles_by_value.get("duplicate")),+ Some(NamedElementHandles::One(handle)) if *handle == first+ ));++ assert!(host.set_attribute(second, "id", "duplicate"));+ assert!(host.append_child(host.document_handle(), second));+ assert!(matches!(+ host.id_index+ .borrow()+ .as_ref()+ .and_then(|index| index.handles_by_value.get("duplicate")),+ Some(NamedElementHandles::Many(handles)) if handles.len() == 2+ ));++ assert!(host.remove_attribute(second, "id"));+ assert!(matches!(+ host.id_index+ .borrow()+ .as_ref()+ .and_then(|index| index.handles_by_value.get("duplicate")),+ Some(NamedElementHandles::One(handle)) if *handle == first+ ));+ }+#[test]fn document_root_named_subtree_lookups_use_indexes_without_widening_nested_scope() {let mut host = test_host();diff --git a/moli-dom/src/native/host/types.rs b/moli-dom/src/native/host/types.rsindex 99dc0eb11..658831538 100644--- a/moli-dom/src/native/host/types.rs+++ b/moli-dom/src/native/host/types.rs@@ -15,7 +15,7 @@ pub struct HostElementSnapshot {#[derive(Debug, Clone, Default)]pub(super) struct NamedElementIndex {- pub(super) handles_by_value: HashMap<ThinArcStr, IndexSet<DomHandle>>,+ pub(super) handles_by_value: HashMap<ThinArcStr, NamedElementHandles>,pub(super) value_by_handle: HashMap<DomHandle, ThinArcStr>,}@@ -74,6 +74,70 @@ impl fmt::Debug for ThinArcStr {}}+#[derive(Debug, Clone)]+pub(super) enum NamedElementHandles {+ One(DomHandle),+ Many(IndexSet<DomHandle>),+}++impl NamedElementHandles {+ pub(super) fn insert(&mut self, handle: DomHandle) {+ match self {+ Self::One(current) if *current == handle => {}+ Self::One(current) => {+ let first = *current;+ let mut handles = IndexSet::with_capacity(2);+ handles.insert(first);+ handles.insert(handle);+ *self = Self::Many(handles);+ }+ Self::Many(handles) => {+ handles.insert(handle);+ }+ }+ }++ /// Removes a handle and reports whether the map entry is now empty.+ pub(super) fn remove(&mut self, handle: DomHandle) -> bool {+ match self {+ Self::One(current) => *current == handle,+ Self::Many(handles) => {+ if !handles.swap_remove(&handle) {+ return false;+ }+ if handles.len() == 1 {+ let remaining = *handles.first().expect("one named candidate remains");+ *self = Self::One(remaining);+ }+ false+ }+ }+ }++ pub(super) fn iter(&self) -> NamedElementHandlesIter<'_> {+ match self {+ Self::One(handle) => NamedElementHandlesIter::One(Some(handle)),+ Self::Many(handles) => NamedElementHandlesIter::Many(handles.iter()),+ }+ }+}++pub(super) enum NamedElementHandlesIter<'a> {+ One(Option<&'a DomHandle>),+ Many(indexmap::set::Iter<'a, DomHandle>),+}++impl<'a> Iterator for NamedElementHandlesIter<'a> {+ type Item = &'a DomHandle;++ fn next(&mut self) -> Option<Self::Item> {+ match self {+ Self::One(handle) => handle.take(),+ Self::Many(handles) => handles.next(),+ }+ }+}+#[derive(Debug, Clone, Default)]pub(super) struct ShadowSlotNameIndex {pub(super) slots_in_tree_order: Vec<DomHandle>,