Skip to content

armgit / moli

Public Imported from GitHub
0

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

perf(modules): share immutable binary sources across compilation

ldm04cd59fc906e0
diff --git a/moli-module-script-tree/src/types.rs b/moli-module-script-tree/src/types.rs
index d2164ef19..714b8e01a 100644
--- a/moli-module-script-tree/src/types.rs
+++ b/moli-module-script-tree/src/types.rs
@@ -519,7 +519,8 @@ impl FetchedModuleSource {
pub enum ModuleSource {
/// Share decoded text across fetch clients and the renderer's compiled record.
Text(Arc<str>),
- Binary(Vec<u8>),
+ /// Share immutable Wasm bytes across fetch clients and compilation.
+ Binary(Arc<[u8]>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
diff --git a/moli-renderer-v8/src/document_module_graph/record.rs b/moli-renderer-v8/src/document_module_graph/record.rs
index f26f779c5..eeded6910 100644
--- a/moli-renderer-v8/src/document_module_graph/record.rs
+++ b/moli-renderer-v8/src/document_module_graph/record.rs
@@ -8,7 +8,8 @@ use super::{ModuleAttributesKey, ModuleKind, ModuleMapKey};
pub(crate) enum ModuleSource {
/// The fetch pipeline and synthetic evaluation share this immutable allocation.
Text(Arc<str>),
- Binary(Vec<u8>),
+ /// Retained Wasm bytes share the fetch allocation across clones.
+ Binary(Arc<[u8]>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -89,8 +90,8 @@ impl ModuleSource {
Self::Text(source.into())
}
- pub(crate) fn binary(bytes: Vec<u8>) -> Self {
- Self::Binary(bytes)
+ pub(crate) fn binary(bytes: impl Into<Arc<[u8]>>) -> Self {
+ Self::Binary(bytes.into())
}
pub(crate) fn text_source(&self) -> Option<&str> {
diff --git a/moli-renderer-v8/src/module_runtime/graph.rs b/moli-renderer-v8/src/module_runtime/graph.rs
index 0a8ed57bb..a2e0cd30b 100644
--- a/moli-renderer-v8/src/module_runtime/graph.rs
+++ b/moli-renderer-v8/src/module_runtime/graph.rs
@@ -2706,6 +2706,59 @@ mod tests {
}
}
+ #[test]
+ fn wasm_module_compilation_shares_fetched_bytes_and_preserves_map_source() {
+ let bytes = b"\0asm\x01\0\0\0";
+ for phase in [
+ module_tree::ModuleImportPhase::Evaluation,
+ module_tree::ModuleImportPhase::Source,
+ ] {
+ let mut vm = new_test_vm("https://app.example.test/page");
+ let source_url = url("https://app.example.test/source.wasm");
+ let key = ModuleMapKey::webassembly(source_url.clone());
+ let shared_bytes: std::sync::Arc<[u8]> = bytes.as_slice().into();
+ let weak_source = std::sync::Arc::downgrade(&shared_bytes);
+ let source = ModuleSource::binary(shared_bytes);
+ let source_pointer = source.binary_source().unwrap().as_ptr();
+ let fetched = module_tree::FetchedModuleSource::new(
+ chromium_module_key(&key),
+ chromium_module_key(&key),
+ source_url.clone(),
+ source_url,
+ chromium_source(source.clone()),
+ module_tree::ModuleFetchMetadata::default(),
+ );
+ let snapshot = {
+ let mut host =
+ RendererModuleScriptTreeHost::new(NativeModuleTreeDocumentOwner::new(&mut vm));
+ module_tree::ModuleScriptTreeHost::compile_module_source(
+ &mut host,
+ fetched.clone(),
+ phase,
+ )
+ .expect("Wasm module should compile")
+ };
+ let entry = local_entry_id(snapshot.entry);
+ let retained_source = vm.document_runtime.native_module_source(entry).unwrap();
+ assert_eq!(retained_source.binary_source(), Some(bytes.as_slice()));
+ assert_eq!(
+ retained_source.binary_source().unwrap().as_ptr(),
+ source_pointer,
+ "{phase:?}: compilation must share the original fetched byte allocation",
+ );
+ drop(retained_source);
+ drop(fetched);
+ drop(source);
+ assert_eq!(
+ weak_source.strong_count(),
+ 1,
+ "{phase:?}: the module map should retain the original byte allocation",
+ );
+ drop(vm);
+ assert!(weak_source.upgrade().is_none());
+ }
+ }
+
#[test]
fn native_json_module_fetch_request_uses_json_destination_metadata() {
let fetch_request = NativeModuleGraphFetchRequest::new_for_test(