summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-05-24 10:15:46 -0400
committerGitHub <noreply@github.com>2024-05-24 10:15:46 -0400
commitb21004b1d16ad7b67c7b1cd235abf792bf9d9777 (patch)
tree2a89a04d2d87d8e30a34beb97cbccf320dbef0ac /runtime
parent92a8d09e498712aec2ba0e54a1ad85194ebd83af (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 'runtime')
-rw-r--r--runtime/Cargo.toml1
-rw-r--r--runtime/code_cache.rs10
-rw-r--r--runtime/fs_util.rs11
-rw-r--r--runtime/worker.rs86
4 files changed, 40 insertions, 68 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index 80dd6a3e0..f66dfb840 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -116,6 +116,7 @@ signal-hook = "0.3.17"
signal-hook-registry = "1.4.0"
tokio.workspace = true
tokio-metrics.workspace = true
+twox-hash.workspace = true
uuid.workspace = true
which = "4.2.5"
diff --git a/runtime/code_cache.rs b/runtime/code_cache.rs
index ccc070365..2a56543a4 100644
--- a/runtime/code_cache.rs
+++ b/runtime/code_cache.rs
@@ -1,5 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+use deno_core::ModuleSpecifier;
+
pub enum CodeCacheType {
EsModule,
Script,
@@ -17,15 +19,15 @@ impl CodeCacheType {
pub trait CodeCache: Send + Sync {
fn get_sync(
&self,
- specifier: &str,
+ specifier: &ModuleSpecifier,
code_cache_type: CodeCacheType,
- source_hash: &str,
+ source_hash: u64,
) -> Option<Vec<u8>>;
fn set_sync(
&self,
- specifier: &str,
+ specifier: ModuleSpecifier,
code_cache_type: CodeCacheType,
- source_hash: &str,
+ source_hash: u64,
data: &[u8],
);
}
diff --git a/runtime/fs_util.rs b/runtime/fs_util.rs
index 09b107300..fe9736038 100644
--- a/runtime/fs_util.rs
+++ b/runtime/fs_util.rs
@@ -63,17 +63,6 @@ pub fn specifier_to_file_path(
}
}
-pub fn code_timestamp(specifier: &str) -> Result<u64, AnyError> {
- let specifier = ModuleSpecifier::parse(specifier)?;
- let path = specifier_to_file_path(&specifier)?;
- #[allow(clippy::disallowed_methods)]
- let timestamp = std::fs::metadata(path)?
- .modified()?
- .duration_since(std::time::UNIX_EPOCH)?
- .as_millis() as u64;
- Ok(timestamp)
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/runtime/worker.rs b/runtime/worker.rs
index 1c291c641..09faa6e08 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -32,6 +32,7 @@ use deno_core::OpMetricsSummaryTracker;
use deno_core::PollEventLoopOptions;
use deno_core::RuntimeOptions;
use deno_core::SharedArrayBufferStore;
+use deno_core::SourceCodeCacheInfo;
use deno_core::SourceMapGetter;
use deno_cron::local::LocalCronHandler;
use deno_fs::FileSystem;
@@ -45,7 +46,6 @@ use log::debug;
use crate::code_cache::CodeCache;
use crate::code_cache::CodeCacheType;
-use crate::fs_util::code_timestamp;
use crate::inspector_server::InspectorServer;
use crate::ops;
use crate::permissions::PermissionsContainer;
@@ -306,51 +306,6 @@ pub fn create_op_metrics(
(op_summary_metrics, op_metrics_factory_fn)
}
-fn get_code_cache(
- code_cache: Arc<dyn CodeCache>,
- specifier: &str,
-) -> Option<Vec<u8>> {
- // Code hashes are not maintained for op_eval_context scripts. Instead we use
- // the modified timestamp from the local file system.
- if let Ok(code_timestamp) = code_timestamp(specifier) {
- code_cache
- .get_sync(
- specifier,
- CodeCacheType::Script,
- code_timestamp.to_string().as_str(),
- )
- .inspect(|_| {
- // This log line is also used by tests.
- log::debug!(
- "V8 code cache hit for script: {specifier}, [{code_timestamp}]"
- );
- })
- } else {
- None
- }
-}
-
-fn set_code_cache(
- code_cache: Arc<dyn CodeCache>,
- specifier: &str,
- data: &[u8],
-) {
- // Code hashes are not maintained for op_eval_context scripts. Instead we use
- // the modified timestamp from the local file system.
- if let Ok(code_timestamp) = code_timestamp(specifier) {
- // This log line is also used by tests.
- log::debug!(
- "Updating V8 code cache for script: {specifier}, [{code_timestamp}]",
- );
- code_cache.set_sync(
- specifier,
- CodeCacheType::Script,
- code_timestamp.to_string().as_str(),
- data,
- );
- }
-}
-
impl MainWorker {
pub fn bootstrap_from_options(
main_module: ModuleSpecifier,
@@ -550,16 +505,41 @@ impl MainWorker {
validate_import_attributes_cb: Some(Box::new(
validate_import_attributes_callback,
)),
- enable_code_cache: options.v8_code_cache.is_some(),
eval_context_code_cache_cbs: options.v8_code_cache.map(|cache| {
let cache_clone = cache.clone();
(
- Box::new(move |specifier: &str| {
- Ok(get_code_cache(cache.clone(), specifier).map(Cow::Owned))
- }) as Box<dyn Fn(&_) -> _>,
- Box::new(move |specifier: &str, data: &[u8]| {
- set_code_cache(cache_clone.clone(), specifier, data);
- }) as Box<dyn Fn(&_, &_)>,
+ Box::new(move |specifier: &ModuleSpecifier, code: &v8::String| {
+ let source_hash = {
+ use std::hash::Hash;
+ use std::hash::Hasher;
+ let mut hasher = twox_hash::XxHash64::default();
+ code.hash(&mut hasher);
+ hasher.finish()
+ };
+ let data = cache
+ .get_sync(specifier, CodeCacheType::Script, source_hash)
+ .inspect(|_| {
+ // This log line is also used by tests.
+ log::debug!("V8 code cache hit for script: {specifier}, [{source_hash}]");
+ })
+ .map(Cow::Owned);
+ Ok(SourceCodeCacheInfo {
+ data,
+ hash: source_hash,
+ })
+ }) as Box<dyn Fn(&_, &_) -> _>,
+ Box::new(
+ move |specifier: ModuleSpecifier, source_hash: u64, data: &[u8]| {
+ // This log line is also used by tests.
+ log::debug!("Updating V8 code cache for script: {specifier}, [{source_hash}]");
+ cache_clone.set_sync(
+ specifier,
+ CodeCacheType::Script,
+ source_hash,
+ data,
+ );
+ },
+ ) as Box<dyn Fn(_, _, &_)>,
)
}),
..Default::default()