summaryrefslogtreecommitdiff
path: root/cli/cache/module_info.rs
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/cache/module_info.rs
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/cache/module_info.rs')
-rw-r--r--cli/cache/module_info.rs130
1 files changed, 102 insertions, 28 deletions
diff --git a/cli/cache/module_info.rs b/cli/cache/module_info.rs
index 4dbb01c37..060a6f4f0 100644
--- a/cli/cache/module_info.rs
+++ b/cli/cache/module_info.rs
@@ -44,18 +44,32 @@ pub static MODULE_INFO_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration {
/// A cache of `deno_graph::ModuleInfo` objects. Using this leads to a considerable
/// performance improvement because when it exists we can skip parsing a module for
/// deno_graph.
+#[derive(Debug)]
pub struct ModuleInfoCache {
conn: CacheDB,
+ parsed_source_cache: Arc<ParsedSourceCache>,
}
impl ModuleInfoCache {
#[cfg(test)]
- pub fn new_in_memory(version: &'static str) -> Self {
- Self::new(CacheDB::in_memory(&MODULE_INFO_CACHE_DB, version))
+ pub fn new_in_memory(
+ version: &'static str,
+ parsed_source_cache: Arc<ParsedSourceCache>,
+ ) -> Self {
+ Self::new(
+ CacheDB::in_memory(&MODULE_INFO_CACHE_DB, version),
+ parsed_source_cache,
+ )
}
- pub fn new(conn: CacheDB) -> Self {
- Self { conn }
+ pub fn new(
+ conn: CacheDB,
+ parsed_source_cache: Arc<ParsedSourceCache>,
+ ) -> Self {
+ Self {
+ conn,
+ parsed_source_cache,
+ }
}
/// Useful for testing: re-create this cache DB with a different current version.
@@ -63,6 +77,7 @@ impl ModuleInfoCache {
pub(crate) fn recreate_with_version(self, version: &'static str) -> Self {
Self {
conn: self.conn.recreate_with_version(version),
+ parsed_source_cache: self.parsed_source_cache,
}
}
@@ -113,13 +128,10 @@ impl ModuleInfoCache {
Ok(())
}
- pub fn as_module_analyzer<'a>(
- &'a self,
- parsed_source_cache: &'a Arc<ParsedSourceCache>,
- ) -> ModuleInfoCacheModuleAnalyzer<'a> {
+ pub fn as_module_analyzer(&self) -> ModuleInfoCacheModuleAnalyzer {
ModuleInfoCacheModuleAnalyzer {
module_info_cache: self,
- parsed_source_cache,
+ parsed_source_cache: &self.parsed_source_cache,
}
}
}
@@ -129,31 +141,99 @@ pub struct ModuleInfoCacheModuleAnalyzer<'a> {
parsed_source_cache: &'a Arc<ParsedSourceCache>,
}
-#[async_trait::async_trait(?Send)]
-impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> {
- async fn analyze(
+impl<'a> ModuleInfoCacheModuleAnalyzer<'a> {
+ fn load_cached_module_info(
&self,
specifier: &ModuleSpecifier,
- source: Arc<str>,
media_type: MediaType,
- ) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> {
- // attempt to load from the cache
- let source_hash = CacheDBHash::from_source(&source);
+ source_hash: CacheDBHash,
+ ) -> Option<ModuleInfo> {
match self.module_info_cache.get_module_info(
specifier,
media_type,
source_hash,
) {
- Ok(Some(info)) => return Ok(info),
- Ok(None) => {}
+ Ok(Some(info)) => Some(info),
+ Ok(None) => None,
Err(err) => {
log::debug!(
"Error loading module cache info for {}. {:#}",
specifier,
err
);
+ None
}
}
+ }
+
+ fn save_module_info_to_cache(
+ &self,
+ specifier: &ModuleSpecifier,
+ media_type: MediaType,
+ source_hash: CacheDBHash,
+ module_info: &ModuleInfo,
+ ) {
+ if let Err(err) = self.module_info_cache.set_module_info(
+ specifier,
+ media_type,
+ source_hash,
+ module_info,
+ ) {
+ log::debug!(
+ "Error saving module cache info for {}. {:#}",
+ specifier,
+ err
+ );
+ }
+ }
+
+ pub fn analyze_sync(
+ &self,
+ specifier: &ModuleSpecifier,
+ media_type: MediaType,
+ source: &Arc<str>,
+ ) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> {
+ // attempt to load from the cache
+ let source_hash = CacheDBHash::from_source(source);
+ if let Some(info) =
+ self.load_cached_module_info(specifier, media_type, source_hash)
+ {
+ return Ok(info);
+ }
+
+ // otherwise, get the module info from the parsed source cache
+ let parser = self.parsed_source_cache.as_capturing_parser();
+ let analyzer = ParserModuleAnalyzer::new(&parser);
+ let module_info =
+ analyzer.analyze_sync(specifier, source.clone(), media_type)?;
+
+ // then attempt to cache it
+ self.save_module_info_to_cache(
+ specifier,
+ media_type,
+ source_hash,
+ &module_info,
+ );
+
+ Ok(module_info)
+ }
+}
+
+#[async_trait::async_trait(?Send)]
+impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> {
+ async fn analyze(
+ &self,
+ specifier: &ModuleSpecifier,
+ source: Arc<str>,
+ media_type: MediaType,
+ ) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> {
+ // attempt to load from the cache
+ let source_hash = CacheDBHash::from_source(&source);
+ if let Some(info) =
+ self.load_cached_module_info(specifier, media_type, source_hash)
+ {
+ return Ok(info);
+ }
// otherwise, get the module info from the parsed source cache
let module_info = deno_core::unsync::spawn_blocking({
@@ -169,18 +249,12 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> {
.unwrap()?;
// then attempt to cache it
- if let Err(err) = self.module_info_cache.set_module_info(
+ self.save_module_info_to_cache(
specifier,
media_type,
source_hash,
&module_info,
- ) {
- log::debug!(
- "Error saving module cache info for {}. {:#}",
- specifier,
- err
- );
- }
+ );
Ok(module_info)
}
@@ -202,7 +276,7 @@ fn serialize_media_type(media_type: MediaType) -> i64 {
Tsx => 11,
Json => 12,
Wasm => 13,
- TsBuildInfo => 14,
+ Css => 14,
SourceMap => 15,
Unknown => 16,
}
@@ -217,7 +291,7 @@ mod test {
#[test]
pub fn module_info_cache_general_use() {
- let cache = ModuleInfoCache::new_in_memory("1.0.0");
+ let cache = ModuleInfoCache::new_in_memory("1.0.0", Default::default());
let specifier1 =
ModuleSpecifier::parse("https://localhost/mod.ts").unwrap();
let specifier2 =