diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-05-24 10:15:46 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-24 10:15:46 -0400 |
commit | b21004b1d16ad7b67c7b1cd235abf792bf9d9777 (patch) | |
tree | 2a89a04d2d87d8e30a34beb97cbccf320dbef0ac /cli | |
parent | 92a8d09e498712aec2ba0e54a1ad85194ebd83af (diff) |
fix: use hash of in-memory bytes only for code cache (#23966)
* https://github.com/denoland/deno_core/pull/752
* https://github.com/denoland/deno_core/pull/753
Did benchmarking on this and it's slightly faster (couple ms) or equal
to in performance as main.
Closes #23904
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 2 | ||||
-rw-r--r-- | cli/cache/code_cache.rs | 31 | ||||
-rw-r--r-- | cli/cache/module_info.rs | 17 | ||||
-rw-r--r-- | cli/factory.rs | 1 | ||||
-rw-r--r-- | cli/module_loader.rs | 92 |
5 files changed, 44 insertions, 99 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 8c033f55a..ec02a801c 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -146,7 +146,7 @@ thiserror.workspace = true tokio.workspace = true tokio-util.workspace = true tower-lsp.workspace = true -twox-hash = "=1.6.3" +twox-hash.workspace = true typed-arena = "=2.0.1" uuid = { workspace = true, features = ["serde"] } walkdir = "=2.3.2" diff --git a/cli/cache/code_cache.rs b/cli/cache/code_cache.rs index 5e44c366e..8d96bf3a1 100644 --- a/cli/cache/code_cache.rs +++ b/cli/cache/code_cache.rs @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use deno_ast::ModuleSpecifier; use deno_core::error::AnyError; use deno_runtime::code_cache; use deno_runtime::deno_webstorage::rusqlite::params; @@ -21,7 +22,6 @@ pub static CODE_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration { on_failure: CacheFailure::Blackhole, }; -#[derive(Clone)] pub struct CodeCache { inner: CodeCacheInner, } @@ -52,28 +52,28 @@ impl CodeCache { pub fn get_sync( &self, - specifier: &str, + specifier: &ModuleSpecifier, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: u64, ) -> Option<Vec<u8>> { Self::ensure_ok(self.inner.get_sync( - specifier, + specifier.as_str(), code_cache_type, - source_hash, + &source_hash.to_string(), )) } pub fn set_sync( &self, - specifier: &str, + specifier: &ModuleSpecifier, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: u64, data: &[u8], ) { Self::ensure_ok(self.inner.set_sync( - specifier, + specifier.as_str(), code_cache_type, - source_hash, + &source_hash.to_string(), data, )); } @@ -82,25 +82,24 @@ impl CodeCache { impl code_cache::CodeCache for CodeCache { fn get_sync( &self, - specifier: &str, + specifier: &ModuleSpecifier, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: u64, ) -> Option<Vec<u8>> { self.get_sync(specifier, code_cache_type, source_hash) } fn set_sync( &self, - specifier: &str, + specifier: ModuleSpecifier, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: u64, data: &[u8], ) { - self.set_sync(specifier, code_cache_type, source_hash, data); + self.set_sync(&specifier, code_cache_type, source_hash, data); } } -#[derive(Clone)] struct CodeCacheInner { conn: CacheDB, } @@ -135,7 +134,7 @@ impl CodeCacheInner { &self, specifier: &str, code_cache_type: code_cache::CodeCacheType, - source_hash: &str, + source_hash: &str, // use string because sqlite doesn't have a u64 type data: &[u8], ) -> Result<(), AnyError> { let sql = " diff --git a/cli/cache/module_info.rs b/cli/cache/module_info.rs index 2e9274160..0e7a97678 100644 --- a/cli/cache/module_info.rs +++ b/cli/cache/module_info.rs @@ -87,23 +87,6 @@ impl ModuleInfoCache { } } - pub fn get_module_source_hash( - &self, - specifier: &ModuleSpecifier, - media_type: MediaType, - ) -> Result<Option<ModuleInfoCacheSourceHash>, AnyError> { - let query = "SELECT source_hash FROM moduleinfocache WHERE specifier=?1 AND media_type=?2"; - let res = self.conn.query_row( - query, - params![specifier.as_str(), serialize_media_type(media_type)], - |row| { - let source_hash: String = row.get(0)?; - Ok(ModuleInfoCacheSourceHash(source_hash)) - }, - )?; - Ok(res) - } - pub fn get_module_info( &self, specifier: &ModuleSpecifier, diff --git a/cli/factory.rs b/cli/factory.rs index 43486bca0..6cdbff9bb 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -798,7 +798,6 @@ impl CliFactory { }, self.emitter()?.clone(), self.main_module_graph_container().await?.clone(), - self.module_info_cache()?.clone(), self.module_load_preparer().await?.clone(), cli_node_resolver.clone(), NpmModuleLoader::new( diff --git a/cli/module_loader.rs b/cli/module_loader.rs index 0ee555f38..6d8d3e92b 100644 --- a/cli/module_loader.rs +++ b/cli/module_loader.rs @@ -13,7 +13,7 @@ use crate::args::CliOptions; use crate::args::DenoSubcommand; use crate::args::TsTypeLib; use crate::cache::CodeCache; -use crate::cache::ModuleInfoCache; +use crate::cache::FastInsecureHasher; use crate::cache::ParsedSourceCache; use crate::emit::Emitter; use crate::factory::CliFactory; @@ -55,6 +55,7 @@ use deno_core::ModuleSpecifier; use deno_core::ModuleType; use deno_core::RequestedModuleType; use deno_core::ResolutionKind; +use deno_core::SourceCodeCacheInfo; use deno_core::SourceMapGetter; use deno_graph::source::ResolutionMode; use deno_graph::source::Resolver; @@ -67,7 +68,6 @@ use deno_graph::Resolution; use deno_lockfile::Lockfile; use deno_runtime::code_cache; use deno_runtime::deno_node::NodeResolutionMode; -use deno_runtime::fs_util::code_timestamp; use deno_runtime::permissions::PermissionsContainer; use deno_semver::npm::NpmPackageReqReference; @@ -221,7 +221,6 @@ struct SharedCliModuleLoaderState { code_cache: Option<Arc<CodeCache>>, emitter: Arc<Emitter>, main_module_graph_container: Arc<MainModuleGraphContainer>, - module_info_cache: Arc<ModuleInfoCache>, module_load_preparer: Arc<ModuleLoadPreparer>, node_resolver: Arc<CliNodeResolver>, npm_module_loader: NpmModuleLoader, @@ -240,7 +239,6 @@ impl CliModuleLoaderFactory { code_cache: Option<Arc<CodeCache>>, emitter: Arc<Emitter>, main_module_graph_container: Arc<MainModuleGraphContainer>, - module_info_cache: Arc<ModuleInfoCache>, module_load_preparer: Arc<ModuleLoadPreparer>, node_resolver: Arc<CliNodeResolver>, npm_module_loader: NpmModuleLoader, @@ -261,7 +259,6 @@ impl CliModuleLoaderFactory { code_cache, emitter, main_module_graph_container, - module_info_cache, module_load_preparer, node_resolver, npm_module_loader, @@ -388,27 +385,20 @@ impl<TGraphContainer: ModuleGraphContainer> } let code_cache = if module_type == ModuleType::JavaScript { - self.shared.code_cache.as_ref().and_then(|cache| { - let code_hash = self - .get_code_hash_or_timestamp(specifier, code_source.media_type) - .ok() - .flatten(); - if let Some(code_hash) = code_hash { - cache - .get_sync( - specifier.as_str(), - code_cache::CodeCacheType::EsModule, - &code_hash, - ) - .map(Cow::from) - .inspect(|_| { - // This log line is also used by tests. - log::debug!( - "V8 code cache hit for ES module: {specifier}, [{code_hash:?}]" - ); - }) - } else { - None + self.shared.code_cache.as_ref().map(|cache| { + let code_hash = FastInsecureHasher::hash(&code); + let data = cache + .get_sync(specifier, code_cache::CodeCacheType::EsModule, code_hash) + .map(Cow::from) + .inspect(|_| { + // This log line is also used by tests. + log::debug!( + "V8 code cache hit for ES module: {specifier}, [{code_hash:?}]" + ); + }); + SourceCodeCacheInfo { + hash: code_hash, + data, } }) } else { @@ -589,25 +579,6 @@ impl<TGraphContainer: ModuleGraphContainer> resolution.map_err(|err| err.into()) } - fn get_code_hash_or_timestamp( - &self, - specifier: &ModuleSpecifier, - media_type: MediaType, - ) -> Result<Option<String>, AnyError> { - let hash = self - .shared - .module_info_cache - .get_module_source_hash(specifier, media_type)?; - if let Some(hash) = hash { - return Ok(Some(hash.into())); - } - - // Use the modified timestamp from the local file system if we don't have a hash. - let timestamp = code_timestamp(specifier.as_str()) - .map(|timestamp| timestamp.to_string())?; - Ok(Some(timestamp)) - } - async fn load_prepared_module( &self, specifier: &ModuleSpecifier, @@ -865,28 +836,21 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader fn code_cache_ready( &self, - specifier: &ModuleSpecifier, + specifier: ModuleSpecifier, + source_hash: u64, code_cache: &[u8], ) -> Pin<Box<dyn Future<Output = ()>>> { if let Some(cache) = self.0.shared.code_cache.as_ref() { - let media_type = MediaType::from_specifier(specifier); - let code_hash = self - .0 - .get_code_hash_or_timestamp(specifier, media_type) - .ok() - .flatten(); - if let Some(code_hash) = code_hash { - // This log line is also used by tests. - log::debug!( - "Updating V8 code cache for ES module: {specifier}, [{code_hash:?}]" - ); - cache.set_sync( - specifier.as_str(), - code_cache::CodeCacheType::EsModule, - &code_hash, - code_cache, - ); - } + // This log line is also used by tests. + log::debug!( + "Updating V8 code cache for ES module: {specifier}, [{source_hash:?}]" + ); + cache.set_sync( + &specifier, + code_cache::CodeCacheType::EsModule, + source_hash, + code_cache, + ); } std::future::ready(()).boxed_local() } |