From ec0207e9b1cd61d2c14e36df43f56aa0bea56222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 1 Dec 2023 22:57:52 +0100 Subject: [PATCH] perf(lsp): better op performance logging (#21423) --- cli/build.rs | 8 +--- cli/lsp/performance.rs | 2 +- cli/lsp/tsc.rs | 85 +++++++++++++++++++------------------ cli/tsc/99_main_compiler.js | 2 +- 4 files changed, 46 insertions(+), 51 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 4a481885e6..53ee914c25 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -60,17 +60,11 @@ mod ts { false } - #[derive(Debug, Deserialize, Serialize)] - #[serde(rename_all = "camelCase")] - struct ScriptVersionArgs { - specifier: String, - } - #[op2] #[string] fn op_script_version( _state: &mut OpState, - #[serde] _args: ScriptVersionArgs, + #[string] _arg: &str, ) -> Result, AnyError> { Ok(Some("1".to_string())) } diff --git a/cli/lsp/performance.rs b/cli/lsp/performance.rs index c4e65aa3c9..ffb6ed2170 100644 --- a/cli/lsp/performance.rs +++ b/cli/lsp/performance.rs @@ -191,7 +191,7 @@ impl Performance { "type": "measure", "name": measure.name, "count": measure.count, - "duration": measure.duration.as_millis() as u32, + "duration": measure.duration.as_micros() as f64 / 1000.0, }) ); let duration = measure.duration; diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index a834faea89..b4e02394bf 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -46,6 +46,8 @@ use deno_core::serde::Serialize; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; +use deno_core::serde_v8; +use deno_core::v8; use deno_core::JsRuntime; use deno_core::ModuleSpecifier; use deno_core::OpState; @@ -3868,71 +3870,76 @@ struct LoadResponse { } #[op2] -#[serde] -fn op_load( +fn op_load<'s>( + scope: &'s mut v8::HandleScope, state: &mut OpState, #[serde] args: SpecifierArgs, -) -> Result, AnyError> { +) -> Result, AnyError> { let state = state.borrow_mut::(); let mark = state.performance.mark_with_args("tsc.op.op_load", &args); let specifier = state.specifier_map.normalize(args.specifier)?; - if specifier.as_str() == "internal:///missing_dependency.d.ts" { - return Ok(Some(LoadResponse { - data: Arc::from("declare const __: any;\nexport = __;\n"), - script_kind: crate::tsc::as_ts_script_kind(MediaType::Dts), - version: Some("1".to_string()), - })); - } - let asset_or_document = state.get_asset_or_document(&specifier); + let maybe_load_response = + if specifier.as_str() == "internal:///missing_dependency.d.ts" { + Some(LoadResponse { + data: Arc::from("declare const __: any;\nexport = __;\n"), + script_kind: crate::tsc::as_ts_script_kind(MediaType::Dts), + version: Some("1".to_string()), + }) + } else { + let asset_or_document = state.get_asset_or_document(&specifier); + asset_or_document.map(|doc| LoadResponse { + data: doc.text(), + script_kind: crate::tsc::as_ts_script_kind(doc.media_type()), + version: state.script_version(&specifier), + }) + }; + + let serialized = serde_v8::to_v8(scope, maybe_load_response)?; + state.performance.measure(mark); - Ok(asset_or_document.map(|doc| LoadResponse { - data: doc.text(), - script_kind: crate::tsc::as_ts_script_kind(doc.media_type()), - version: state.script_version(&specifier), - })) + Ok(serialized) } #[op2] -#[serde] -fn op_resolve( +fn op_resolve<'s>( + scope: &'s mut v8::HandleScope, state: &mut OpState, #[serde] args: ResolveArgs, -) -> Result>, AnyError> { +) -> Result, AnyError> { let state = state.borrow_mut::(); let mark = state.performance.mark_with_args("tsc.op.op_resolve", &args); let referrer = state.specifier_map.normalize(&args.base)?; - let result = match state.get_asset_or_document(&referrer) { + let specifiers = match state.get_asset_or_document(&referrer) { Some(referrer_doc) => { let resolved = state.state_snapshot.documents.resolve( args.specifiers, &referrer_doc, state.state_snapshot.npm.as_ref(), ); - Ok( - resolved - .into_iter() - .map(|o| { - o.map(|(s, mt)| { - ( - state.specifier_map.denormalize(&s), - mt.as_ts_extension().to_string(), - ) - }) + resolved + .into_iter() + .map(|o| { + o.map(|(s, mt)| { + ( + state.specifier_map.denormalize(&s), + mt.as_ts_extension().to_string(), + ) }) - .collect(), - ) + }) + .collect() } None => { lsp_warn!( "Error resolving. Referring specifier \"{}\" was not found.", args.base ); - Ok(vec![None; args.specifiers.len()]) + vec![None; args.specifiers.len()] } }; + let response = serde_v8::to_v8(scope, specifiers)?; state.performance.measure(mark); - result + Ok(response) } #[op2] @@ -3993,22 +4000,16 @@ fn op_script_names(state: &mut OpState) -> Vec { .collect() } -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -struct ScriptVersionArgs { - specifier: String, -} - #[op2] #[string] fn op_script_version( state: &mut OpState, - #[serde] args: ScriptVersionArgs, + #[string] specifier: &str, ) -> Result, AnyError> { let state = state.borrow_mut::(); // this op is very "noisy" and measuring its performance is not useful, so we // don't measure it uniquely anymore. - let specifier = state.specifier_map.normalize(args.specifier)?; + let specifier = state.specifier_map.normalize(specifier)?; Ok(state.script_version(&specifier)) } diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 1b2e70dd67..6861e17442 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -722,7 +722,7 @@ delete Object.prototype.__proto__; if (scriptVersionCache.has(specifier)) { return scriptVersionCache.get(specifier); } - const scriptVersion = ops.op_script_version({ specifier }); + const scriptVersion = ops.op_script_version(specifier); scriptVersionCache.set(specifier, scriptVersion); return scriptVersion; },