Skip to content

armgit / moli

Public Imported from GitHub
0

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

perf(style): make custom property inspection linear

ldm035d295d1aa2f
diff --git a/moli-cdp-smoke/perf/computed_style_custom_properties.py b/moli-cdp-smoke/perf/computed_style_custom_properties.py
new file mode 100644
index 000000000..5b77e28ca
--- /dev/null
+++ b/moli-cdp-smoke/perf/computed_style_custom_properties.py
@@ -0,0 +1,196 @@
+"""Reproduce CDP computed-style scaling with inherited custom properties.
+
+The page contains one target element. The only variable is the number of
+custom properties inherited from ``:root``. Each measurement sends one
+``CSS.getComputedStyleForNode`` command, so DOM size, network activity, layout,
+and paint do not contribute to the result.
+
+Usage:
+ uv run --project moli-cdp-smoke python \
+ moli-cdp-smoke/perf/computed_style_custom_properties.py
+"""
+
+from __future__ import annotations
+
+import argparse
+import asyncio
+import json
+import os
+import socket
+import subprocess
+import time
+import urllib.parse
+import urllib.request
+from contextlib import closing
+from pathlib import Path
+from typing import Any
+
+import websockets # type: ignore
+
+REPO_ROOT = Path(__file__).resolve().parents[2]
+
+
+def free_port() -> int:
+ with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
+ sock.bind(("127.0.0.1", 0))
+ return sock.getsockname()[1]
+
+
+def resolve_moli_bin() -> str:
+ return os.environ.get("MOLI_BIN", str(REPO_ROOT / "target/release/moli"))
+
+
+def local_json(url: str) -> Any:
+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
+ with opener.open(url, timeout=0.5) as response:
+ return json.loads(response.read())
+
+
+async def wait_page_websocket(port: int, timeout_s: float = 15.0) -> str:
+ deadline = time.monotonic() + timeout_s
+ last_error: Exception | None = None
+ while time.monotonic() < deadline:
+ try:
+ targets = await asyncio.to_thread(
+ local_json, f"http://127.0.0.1:{port}/json/list"
+ )
+ for target in targets:
+ if target.get("type") == "page" and target.get("webSocketDebuggerUrl"):
+ return target["webSocketDebuggerUrl"]
+ except Exception as error: # The process may still be binding its socket.
+ last_error = error
+ await asyncio.sleep(0.05)
+ raise RuntimeError(f"CDP page target did not become ready: {last_error}")
+
+
+class CdpClient:
+ def __init__(self, websocket: Any) -> None:
+ self.websocket = websocket
+ self.next_id = 1
+
+ async def command(
+ self, method: str, params: dict[str, Any] | None = None
+ ) -> dict[str, Any]:
+ request_id = self.next_id
+ self.next_id += 1
+ message: dict[str, Any] = {"id": request_id, "method": method}
+ if params is not None:
+ message["params"] = params
+ await self.websocket.send(json.dumps(message, separators=(",", ":")))
+ while True:
+ response = json.loads(await self.websocket.recv())
+ if response.get("id") != request_id:
+ continue
+ if "error" in response:
+ raise RuntimeError(f"{method} failed: {response['error']}")
+ return response.get("result", {})
+
+
+def fixture_url(custom_property_count: int) -> str:
+ declarations = "".join(
+ f"--property-{index:04d}:{index};" for index in range(custom_property_count)
+ )
+ html = (
+ "<!doctype html><meta charset=utf-8>"
+ f"<style>:root{{{declarations}}}</style>"
+ '<div id="target">target</div>'
+ )
+ return "data:text/html;charset=utf-8," + urllib.parse.quote(html, safe="")
+
+
+async def wait_until_loaded(client: CdpClient) -> None:
+ for _ in range(200):
+ result = await client.command(
+ "Runtime.evaluate",
+ {"expression": "document.readyState", "returnByValue": True},
+ )
+ if result.get("result", {}).get("value") == "complete":
+ return
+ await asyncio.sleep(0.01)
+ raise RuntimeError("fixture navigation did not complete")
+
+
+async def measure(port: int, counts: list[int], runs: int) -> None:
+ websocket_url = await wait_page_websocket(port)
+ async with websockets.connect(
+ websocket_url, max_size=192 * 1024 * 1024, proxy=None
+ ) as ws:
+ client = CdpClient(ws)
+ await client.command("Page.enable")
+ await client.command("DOM.enable")
+ await client.command("CSS.enable")
+
+ print(
+ "custom_properties "
+ + " ".join(f"run_{run + 1}_ms" for run in range(runs))
+ )
+ for count in counts:
+ await client.command("Page.navigate", {"url": fixture_url(count)})
+ await wait_until_loaded(client)
+ document = await client.command("DOM.getDocument", {"depth": 1})
+ target = await client.command(
+ "DOM.querySelector",
+ {"nodeId": document["root"]["nodeId"], "selector": "#target"},
+ )
+
+ samples: list[float] = []
+ returned_custom_count = 0
+ for _ in range(runs):
+ started = time.perf_counter()
+ result = await client.command(
+ "CSS.getComputedStyleForNode", {"nodeId": target["nodeId"]}
+ )
+ samples.append((time.perf_counter() - started) * 1000.0)
+ returned_custom_count = sum(
+ property_.get("name", "").startswith("--")
+ for property_ in result.get("computedStyle", [])
+ )
+ if returned_custom_count != count:
+ raise RuntimeError(
+ f"requested {count} custom properties, got {returned_custom_count}"
+ )
+ print(f"{count:17d} " + " ".join(f"{sample:8.2f}" for sample in samples))
+
+
+async def main() -> None:
+ parser = argparse.ArgumentParser()
+ parser.add_argument(
+ "--counts", type=int, nargs="+", default=[0, 250, 500, 1000, 2000]
+ )
+ parser.add_argument("--runs", type=int, default=2)
+ args = parser.parse_args()
+
+ port = free_port()
+ environment = {
+ key: value
+ for key, value in os.environ.items()
+ if "PROXY" not in key.upper()
+ }
+ environment["NO_PROXY"] = "127.0.0.1,localhost"
+ process = subprocess.Popen(
+ [
+ resolve_moli_bin(),
+ "serve",
+ "-lr",
+ "--host",
+ "127.0.0.1",
+ "--port",
+ str(port),
+ ],
+ env=environment,
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ )
+ try:
+ await measure(port, args.counts, args.runs)
+ finally:
+ process.terminate()
+ try:
+ process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index ea9eb99a1..c8d62e920 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -7775,6 +7775,67 @@ unrelatedHost.attachShadow({ mode: 'open' }).innerHTML =
);
}
+#[test]
+fn inspector_computed_style_bulk_read_handles_large_inherited_custom_property_sets() {
+ const CUSTOM_PROPERTY_COUNT: usize = 512;
+ let mut vm = new_storage_test_vm("https://computed-style-inspector-many-custom.test/");
+ vm.eval(&format!(
+ r#"
+CSS.registerProperty({{
+ name: '--non-inherited-bulk',
+ syntax: '*',
+ inherits: false,
+ initialValue: 'registered-initial'
+}});
+const style = document.createElement('style');
+style.textContent = `:root {{${{Array.from(
+ {{ length: {CUSTOM_PROPERTY_COUNT} }},
+ (_, index) => `--bulk-${{String(index).padStart(4, '0')}}: value-${{index}};`
+).join('')}}--removed-bulk: ancestor;}}
+#target {{ --removed-bulk: initial; --non-inherited-bulk: registered-local; }}`;
+(document.head || document.documentElement || document).appendChild(style);
+const target = document.createElement('div');
+target.id = 'target';
+(document.body || document.documentElement || document).appendChild(target);
+"#,
+ ))
+ .expect("large custom-property fixture should initialize");
+ let target = element_handle_by_id(&vm, "target");
+
+ for observation in 0..2 {
+ let properties = vm
+ .computed_style_properties_for_inspector_handle(target)
+ .expect("live element should resolve");
+ assert_eq!(
+ properties
+ .iter()
+ .filter(|(name, _)| name.starts_with("--bulk-"))
+ .count(),
+ CUSTOM_PROPERTY_COUNT,
+ "observation {observation} lost custom properties",
+ );
+ let properties = properties
+ .into_iter()
+ .collect::<std::collections::HashMap<_, _>>();
+ for (name, expected) in [
+ ("--bulk-0000", "value-0"),
+ ("--bulk-0256", "value-256"),
+ ("--bulk-0511", "value-511"),
+ ("--non-inherited-bulk", "registered-local"),
+ ] {
+ assert_eq!(
+ properties.get(name).map(String::as_str),
+ Some(expected),
+ "observation {observation} returned the wrong value for {name}",
+ );
+ }
+ assert!(
+ !properties.contains_key("--removed-bulk"),
+ "observation {observation} exposed a tombstoned inherited value",
+ );
+ }
+}
+
#[test]
fn inspector_computed_style_bulk_read_uses_child_document_scope_and_viewport() {
let mut vm = new_storage_test_vm("https://computed-style-inspector-child.test/");
diff --git a/moli-renderer-v8/src/style_engine/computed.rs b/moli-renderer-v8/src/style_engine/computed.rs
index e5e305b8a..5c5d8453c 100644
--- a/moli-renderer-v8/src/style_engine/computed.rs
+++ b/moli-renderer-v8/src/style_engine/computed.rs
@@ -1,3 +1,5 @@
+use std::collections::HashSet;
+
use dom::ElementState as StyloElementState;
use style::{
Atom,
@@ -692,13 +694,26 @@ fn computed_style_snapshot_after_style_update_with_key(
fn computed_custom_property_names_for_style(style: &ComputedValues) -> Vec<String> {
let custom_properties = style.custom_properties();
- let mut names = Vec::new();
- let mut index = 0;
- while let Some((name, value)) = custom_properties.property_at(index) {
- if value.is_some() {
+ let property_count = custom_properties.inherited.len() + custom_properties.non_inherited.len();
+ let mut seen = HashSet::with_capacity(property_count);
+ let mut names = Vec::with_capacity(property_count);
+ for (name, _) in custom_properties
+ .inherited
+ .iter()
+ .chain(custom_properties.non_inherited.iter())
+ {
+ // Stylo's maps are copy-on-write chains. Walk each chain once, but
+ // publish a name only when its effective entry is live; an older
+ // ancestor entry may be shadowed by a tombstone in the child map.
+ if seen.insert(name)
+ && custom_properties
+ .inherited
+ .get(name)
+ .or_else(|| custom_properties.non_inherited.get(name))
+ .is_some()
+ {
names.push(format!("--{name}"));
}
- index += 1;
}
names
}
@@ -1470,33 +1485,30 @@ fn serialize_resolved_computed_property(
}
fn serialize_computed_custom_property(style: &ComputedValues, property: &str) -> Option<String> {
+ // `property_at(index)` is backed by an O(index) COW-chain walk. Parse the
+ // custom name once and use the map's keyed lookup instead.
+ let PropertyId::Custom(name) = PropertyId::parse_enabled_for_all_content(property).ok()? else {
+ return None;
+ };
let custom_properties = style.custom_properties();
- let mut index = 0;
- while let Some((name, value)) = custom_properties.property_at(index) {
- if format!("--{name}") == property {
- value.as_ref()?;
- let property_id = PropertyId::Custom(name.clone());
- let mut context = resolved::Context {
- style,
- for_property: property_id.clone(),
- current_longhand: None,
- };
- let mut serialized = String::new();
- style
- .computed_or_resolved_property_value(
- property_id,
- Some(&mut context),
- &mut serialized,
- )
- .ok()?;
- if serialized.is_empty() {
- return Some(" ".to_owned());
- }
- return Some(serialized);
- }
- index += 1;
+ custom_properties
+ .inherited
+ .get(&name)
+ .or_else(|| custom_properties.non_inherited.get(&name))?;
+ let property_id = PropertyId::Custom(name);
+ let mut context = resolved::Context {
+ style,
+ for_property: property_id.clone(),
+ current_longhand: None,
+ };
+ let mut serialized = String::new();
+ style
+ .computed_or_resolved_property_value(property_id, Some(&mut context), &mut serialized)
+ .ok()?;
+ if serialized.is_empty() {
+ return Some(" ".to_owned());
}
- None
+ Some(serialized)
}
fn inherited_style_parent(host: &DomHost, handle: DomHandle) -> Option<DomHandle> {