Skip to content

armgit / moli

Public Imported from GitHub
0

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

perf(dom): share named index values

ldm0bf82c1a10b15
diff --git a/moli-dom/src/native/host/document.rs b/moli-dom/src/native/host/document.rs
index ae5c68f13..14c6aa5f8 100644
--- a/moli-dom/src/native/host/document.rs
+++ b/moli-dom/src/native/host/document.rs
@@ -303,7 +303,7 @@ impl DomHost {
handle: DomHandle,
current_value: Option<&str>,
) {
- if index.value_by_handle.get(&handle).map(String::as_str) == current_value {
+ if index.value_by_handle.get(&handle).map(AsRef::as_ref) == current_value {
return;
}
if let Some(previous_value) = index.value_by_handle.remove(&handle) {
@@ -320,14 +320,13 @@ impl DomHost {
}
}
if let Some(current_value) = current_value {
+ let current_value = std::sync::Arc::<str>::from(current_value);
index
.handles_by_value
- .entry(current_value.to_owned())
+ .entry(current_value.clone())
.or_default()
.insert(handle);
- index
- .value_by_handle
- .insert(handle, current_value.to_owned());
+ index.value_by_handle.insert(handle, current_value);
}
}
diff --git a/moli-dom/src/native/host/query_index.rs b/moli-dom/src/native/host/query_index.rs
index 4edf59601..683e68790 100644
--- a/moli-dom/src/native/host/query_index.rs
+++ b/moli-dom/src/native/host/query_index.rs
@@ -990,6 +990,32 @@ mod tests {
assert_eq!(host.element_handle_by_name("late"), Some(late));
}
+ #[test]
+ fn named_candidate_index_shares_values_between_directions() {
+ let mut host = test_host();
+ let element = host.create_element("div");
+ assert!(host.set_attribute(element, "id", "shared-identifier"));
+ assert!(host.append_child(host.document_handle(), element));
+
+ assert_eq!(
+ host.element_handle_by_id("shared-identifier"),
+ Some(element)
+ );
+
+ let index = host.id_index.borrow();
+ let index = index.as_ref().expect("materialized ID index");
+ let forward = index
+ .handles_by_value
+ .keys()
+ .find(|value| value.as_ref() == "shared-identifier")
+ .expect("forward index key");
+ let reverse = index
+ .value_by_handle
+ .get(&element)
+ .expect("reverse index value");
+ assert!(std::sync::Arc::ptr_eq(forward, reverse));
+ }
+
#[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.rs
index 589cfdd7c..9ccf02308 100644
--- a/moli-dom/src/native/host/types.rs
+++ b/moli-dom/src/native/host/types.rs
@@ -1,4 +1,5 @@
use super::*;
+use std::sync::Arc;
pub type DomHandle = NativeNodeId;
@@ -9,8 +10,8 @@ pub struct HostElementSnapshot {
#[derive(Debug, Clone, Default)]
pub(super) struct NamedElementIndex {
- pub(super) handles_by_value: HashMap<String, IndexSet<DomHandle>>,
- pub(super) value_by_handle: HashMap<DomHandle, String>,
+ pub(super) handles_by_value: HashMap<Arc<str>, IndexSet<DomHandle>>,
+ pub(super) value_by_handle: HashMap<DomHandle, Arc<str>>,
}
#[derive(Debug, Clone, Default)]