diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-10-01 06:15:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-01 12:15:56 +0200 |
commit | ecfafda9d8ead19cb35708f310e49176db2ec2b5 (patch) | |
tree | 222a52e0e5c06cc946d45240a6194c57b46a8724 /cli/node/mod.rs | |
parent | 1058d1868fcc28ce115d1cc231e66016308b76ce (diff) |
perf: node cjs & esm analysis cache (#16097)
This commit adds a cache for CJS and ESM analysis that is backed by an
SQLite file.
The connection to the DB is lazily created on first use, so shouldn't
have impact on the startup time.
Benched with running Vite
Deno v1.26:
```
$ deno task dev
Warning deno task is unstable and may drastically change in the future
Task dev deno run -A --unstable --node-modules-dir npm:vite
VITE v3.1.4 ready in 961 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
```
This branch:
```
../deno/target/release/deno task dev
Warning deno task is unstable and may drastically change in the future
Task dev deno run -A --unstable --node-modules-dir npm:vite
VITE v3.1.4 ready in 330 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
```
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/node/mod.rs')
-rw-r--r-- | cli/node/mod.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 3ea263b04..2e431eaa0 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -5,6 +5,7 @@ use std::collections::VecDeque; use std::path::Path; use std::path::PathBuf; +use crate::cache::NodeAnalysisCache; use crate::deno_std::CURRENT_STD_URL; use deno_ast::CjsAnalysis; use deno_ast::MediaType; @@ -734,12 +735,20 @@ pub fn translate_cjs_to_esm( code: String, media_type: MediaType, npm_resolver: &NpmPackageResolver, + node_analysis_cache: &NodeAnalysisCache, ) -> Result<String, AnyError> { fn perform_cjs_analysis( + analysis_cache: &NodeAnalysisCache, specifier: &str, media_type: MediaType, code: String, ) -> Result<CjsAnalysis, AnyError> { + let source_hash = NodeAnalysisCache::compute_source_hash(&code); + if let Some(analysis) = + analysis_cache.get_cjs_analysis(specifier, &source_hash) + { + return Ok(analysis); + } let parsed_source = deno_ast::parse_script(deno_ast::ParseParams { specifier: specifier.to_string(), text_info: deno_ast::SourceTextInfo::new(code.into()), @@ -748,7 +757,10 @@ pub fn translate_cjs_to_esm( scope_analysis: false, maybe_syntax: None, })?; - Ok(parsed_source.analyze_cjs()) + let analysis = parsed_source.analyze_cjs(); + analysis_cache.set_cjs_analysis(specifier, &source_hash, &analysis); + + Ok(analysis) } let mut temp_var_count = 0; @@ -758,7 +770,12 @@ pub fn translate_cjs_to_esm( r#"const require = Deno[Deno.internal].require.Module.createRequire(import.meta.url);"#.to_string(), ]; - let analysis = perform_cjs_analysis(specifier.as_str(), media_type, code)?; + let analysis = perform_cjs_analysis( + node_analysis_cache, + specifier.as_str(), + media_type, + code, + )?; let mut all_exports = analysis .exports @@ -804,6 +821,7 @@ pub fn translate_cjs_to_esm( { let analysis = perform_cjs_analysis( + node_analysis_cache, reexport_specifier.as_str(), reexport_file.media_type, reexport_file.source.to_string(), |