From 69c0b05f7ab72f957ce7685998d3f424fb7e812c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 21 Feb 2023 21:12:22 +0100 Subject: refactor(core): More efficient serde for ES modules in snapshot (#17856) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of relying on "serde_v8" which is very inefficient in serializing enums, I'm hand rolling serde for "ModuleMap" data that is stored in the V8 snapshot to make ES modules snapshottable. ``` // this branch Benchmark #2: ./target/release/deno run empty.js Time (mean ± σ): 21.4 ms ± 0.9 ms [User: 15.6 ms, System: 6.4 ms] Range (min … max): 20.2 ms … 24.4 ms // main branch Benchmark #2: ./target/release/deno run empty.js Time (mean ± σ): 23.1 ms ± 1.2 ms [User: 17.0 ms, System: 6.2 ms] Range (min … max): 21.0 ms … 26.0 ms ``` --- core/runtime.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'core/runtime.rs') diff --git a/core/runtime.rs b/core/runtime.rs index d098c25b1..9c6b7afea 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -438,7 +438,7 @@ impl JsRuntime { fn get_context_data( scope: &mut v8::HandleScope<()>, context: v8::Local, - ) -> (Vec>, v8::Global) { + ) -> (Vec>, v8::Global) { fn data_error_to_panic(err: v8::DataError) -> ! { match err { v8::DataError::BadType { actual, expected } => { @@ -457,15 +457,11 @@ impl JsRuntime { // The 0th element is the module map itself, followed by X number of module // handles. We need to deserialize the "next_module_id" field from the // map to see how many module handles we expect. - match scope.get_context_data_from_snapshot_once::(0) { + match scope.get_context_data_from_snapshot_once::(0) { Ok(val) => { let next_module_id = { - let info_str = v8::String::new(&mut scope, "info").unwrap(); - let info_data: v8::Local = val - .get(&mut scope, info_str.into()) - .unwrap() - .try_into() - .unwrap(); + let info_data: v8::Local = + val.get_index(&mut scope, 1).unwrap().try_into().unwrap(); info_data.length() }; @@ -3648,7 +3644,7 @@ pub mod tests { main, name: specifier.to_string(), requests: vec![crate::modules::ModuleRequest { - specifier: crate::resolve_url(&format!("file:///{prev}.js")).unwrap(), + specifier: format!("file:///{prev}.js"), asserted_module_type: AssertedModuleType::JavaScriptOrWasm, }], module_type: ModuleType::JavaScript, -- cgit v1.2.3