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): update registry associations without global scans

ldm04c56f704a3fc
diff --git a/Cargo.lock b/Cargo.lock
index afe2035d6..b97da0dcc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3083,6 +3083,7 @@ dependencies = [
"fastrand",
"flate2",
"futures-util",
+ "hashlink",
"html-escape",
"html5ever",
"http",
diff --git a/moli-renderer-v8/Cargo.toml b/moli-renderer-v8/Cargo.toml
index acb282e69..83b885235 100644
--- a/moli-renderer-v8/Cargo.toml
+++ b/moli-renderer-v8/Cargo.toml
@@ -97,6 +97,7 @@ http = "1"
httpdate = "1.0.3"
html-escape = "0.2.13"
html5ever = "0.39.0"
+hashlink = "0.10"
indexmap = "2.6"
parking_lot = "0.12"
markup5ever = "0.39.0"
diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs
index 4cf33aed5..fd6434590 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/core.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs
@@ -232,7 +232,7 @@ impl JsContextHost {
parser_defined_autonomous_custom_elements: VecDeque::new(),
parser_custom_element_handoff_replacements: HashMap::new(),
scoped_custom_element_registry_wrappers: HashMap::new(),
- custom_element_registry_associations: IndexMap::new(),
+ custom_element_registry_associations: LinkedHashMap::new(),
next_scoped_custom_elements_registry_id: 1,
observers: ObserverStore::default(),
text_codecs: TextCodecStore::default(),
@@ -1753,17 +1753,8 @@ impl JsContextHost {
handle: DomHandle,
association: CustomElementRegistryAssociation,
) {
- match self
- .custom_element_registry_associations
- .get(&handle)
- .copied()
- {
- Some(current) if current == association => return,
- Some(_) => {
- self.custom_element_registry_associations
- .shift_remove(&handle);
- }
- None => {}
+ if self.custom_element_registry_associations.get(&handle) == Some(&association) {
+ return;
}
self.custom_element_registry_associations
.insert(handle, association);
@@ -1773,27 +1764,12 @@ impl JsContextHost {
&mut self,
retargets: &[RegistryAssociationRetarget],
) {
- if retargets.is_empty() {
- return;
- }
-
// Re-association is observable even when the registry itself does not
// change: adopting a scoped-registry tree into another document moves
- // that document to the end of the registry's association order. Build
- // the final touched-handle order once so a large adopted subtree does
- // not repeatedly shift the IndexMap.
- let mut touched = HashSet::with_capacity(retargets.len());
- let mut ordered_retargets = Vec::with_capacity(retargets.len());
- for retarget in retargets.iter().rev() {
- if touched.insert(retarget.handle) {
- ordered_retargets.push(*retarget);
- }
- }
- ordered_retargets.reverse();
-
- self.custom_element_registry_associations
- .retain(|handle, _| !touched.contains(handle));
- for retarget in ordered_retargets {
+ // that document to the end of the registry's association order.
+ // LinkedHashMap::insert moves existing entries to the back in O(1),
+ // preserving last-retarget order without scanning unrelated entries.
+ for retarget in retargets {
self.custom_element_registry_associations
.insert(retarget.handle, retarget.association);
}
@@ -1850,8 +1826,7 @@ impl JsContextHost {
})
.collect::<Vec<_>>();
for handle in stale_handles {
- self.custom_element_registry_associations
- .shift_remove(&handle);
+ self.custom_element_registry_associations.remove(&handle);
}
}
@@ -1870,8 +1845,7 @@ impl JsContextHost {
})
.collect::<Vec<_>>();
for handle in stale_handles {
- self.custom_element_registry_associations
- .shift_remove(&handle);
+ self.custom_element_registry_associations.remove(&handle);
}
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
index 2895814c4..025003655 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
@@ -57,6 +57,7 @@ use crate::{
},
types::{PendingSubresourceContinueEvent, PendingSubresourceFetchInfo},
};
+use hashlink::LinkedHashMap;
use indexmap::IndexMap;
use moli_shared_worker::SharedWorkerClientOwnerId;
use std::{
@@ -837,7 +838,8 @@ pub(crate) struct JsContextHost {
// that result before the next script observes the DOM.
parser_custom_element_handoff_replacements: HashMap<DomHandle, DomHandle>,
scoped_custom_element_registry_wrappers: HashMap<u64, v8::Weak<v8::Object>>,
- custom_element_registry_associations: IndexMap<DomHandle, CustomElementRegistryAssociation>,
+ custom_element_registry_associations:
+ LinkedHashMap<DomHandle, CustomElementRegistryAssociation>,
next_scoped_custom_elements_registry_id: u64,
observers: ObserverStore,
text_codecs: TextCodecStore,