Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pass along hash through eval context code cache callbacks #753

Merged
merged 16 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,14 @@ pub type CustomModuleEvaluationCb = Box<
) -> Result<CustomModuleEvaluationKind, AnyError>,
>;

/// A callback to get the code cache for a script.
/// (specifier: &str, code: &str) -> ...
pub type EvalContextGetCodeCacheCb =
Box<dyn Fn(&str) -> Result<Option<Cow<'static, [u8]>>, AnyError>>;
Box<dyn Fn(&Url, &v8::String) -> Result<ModuleSourceCodeCache, AnyError>>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since v8::String is hashable, we can just pass this along.


pub type EvalContextCodeCacheReadyCb = Box<dyn Fn(&str, &[u8])>;
/// Callback when the code cache is ready.
/// (specifier: Url, hash: u64, data: &[u8]) -> ()
pub type EvalContextCodeCacheReadyCb = Box<dyn Fn(Url, u64, &[u8])>;

pub enum CustomModuleEvaluationKind {
/// This evaluation results in a single, "synthetic" module.
Expand Down
34 changes: 21 additions & 13 deletions core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,20 @@ pub fn op_eval_context<'a>(
let tc_scope = &mut v8::TryCatch::new(scope);
let source = v8::Local::<v8::String>::try_from(source)
.map_err(|_| type_error("Invalid source"))?;
let specifier = resolve_url(&specifier)?.to_string();
let specifier_v8 = v8::String::new(tc_scope, &specifier).unwrap();
let specifier = resolve_url(&specifier)?;
let specifier_v8 = v8::String::new(tc_scope, specifier.as_str()).unwrap();
let origin = script_origin(tc_scope, specifier_v8);

let (script, try_store_code_cache) = state
let (maybe_script, maybe_code_cache_hash) = state
.eval_context_get_code_cache_cb
.as_ref()
.and_then(|cb| {
cb(&specifier).unwrap().map(|code_cache| {
.map(|cb| {
let code_cache = cb(&specifier, &source).unwrap();
if let Some(code_cache_data) = &code_cache.data {
let mut source = v8::script_compiler::Source::new_with_cached_data(
source,
Some(&origin),
v8::CachedData::new(&code_cache),
v8::CachedData::new(code_cache_data),
);
let script = v8::script_compiler::compile(
tc_scope,
Expand All @@ -297,12 +298,19 @@ pub fn op_eval_context<'a>(
Some(cached_data) => cached_data.rejected(),
_ => true,
};
(script, rejected)
})
let maybe_code_cache_hash = if rejected {
Some(code_cache.hash) // recreate the cache
} else {
None
};
(Some(script), maybe_code_cache_hash)
} else {
(None, Some(code_cache.hash))
}
})
.unwrap_or_else(|| {
(v8::Script::compile(tc_scope, source, Some(&origin)), true)
});
.unwrap_or_else(|| (None, None));
let script = maybe_script
.unwrap_or_else(|| v8::Script::compile(tc_scope, source, Some(&origin)));

let null = v8::null(tc_scope);
let script = match script {
Expand All @@ -322,13 +330,13 @@ pub fn op_eval_context<'a>(
}
};

if try_store_code_cache {
if let Some(code_cache_hash) = maybe_code_cache_hash {
if let Some(cb) = state.eval_context_code_cache_ready_cb.as_ref() {
let unbound_script = script.get_unbound_script(tc_scope);
let code_cache = unbound_script.create_code_cache().ok_or_else(|| {
type_error("Unable to get code cache from unbound module script")
})?;
cb(&specifier, &code_cache);
cb(specifier, code_cache_hash, &code_cache);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please focus on this function to ensure this is correct.


Expand Down
41 changes: 28 additions & 13 deletions core/runtime/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,13 +1315,18 @@ fn eval_context_with_code_cache() {
let code_cache = {
let updated_code_cache = Arc::new(Mutex::new(HashMap::new()));

let get_code_cache_cb = Box::new(|_: &str| Ok(None));
let get_code_cache_cb = Box::new(|_: &Url, source: &v8::String| {
Ok(ModuleSourceCodeCache {
data: None,
hash: hash_source(source),
})
});

let updated_code_cache_clone = updated_code_cache.clone();
let set_code_cache_cb =
Box::new(move |specifier: &str, code_cache: &[u8]| {
Box::new(move |specifier: Url, _hash: u64, code_cache: &[u8]| {
let mut c = updated_code_cache_clone.lock();
c.insert(specifier.to_string(), code_cache.to_vec());
c.insert(specifier, code_cache.to_vec());
});

let mut runtime = JsRuntime::new(RuntimeOptions {
Expand All @@ -1337,7 +1342,7 @@ fn eval_context_with_code_cache() {
.unwrap();

let c = updated_code_cache.lock();
let mut keys = c.keys().collect::<Vec<_>>();
let mut keys = c.keys().map(|s| s.as_str()).collect::<Vec<_>>();
keys.sort();
assert_eq!(keys, vec!["file:///foo.js",]);
c.clone()
Expand All @@ -1348,19 +1353,21 @@ fn eval_context_with_code_cache() {
let updated_code_cache = Arc::new(Mutex::new(HashMap::new()));

let code_cache_clone = code_cache.clone();
let get_code_cache_cb = Box::new(move |specifier: &str| {
Ok(
code_cache_clone
.get(specifier)
.map(|code_cache| Cow::Owned(code_cache.clone())),
)
});
let get_code_cache_cb =
Box::new(move |specifier: &Url, source: &v8::String| {
Ok(ModuleSourceCodeCache {
data: code_cache_clone
.get(specifier)
.map(|code_cache| Cow::Owned(code_cache.clone())),
hash: hash_source(source),
})
});

let updated_code_cache_clone = updated_code_cache.clone();
let set_code_cache_cb =
Box::new(move |specifier: &str, code_cache: &[u8]| {
Box::new(move |specifier: Url, _hash: u64, code_cache: &[u8]| {
let mut c = updated_code_cache_clone.lock();
c.insert(specifier.to_string(), code_cache.to_vec());
c.insert(specifier, code_cache.to_vec());
});

let mut runtime = JsRuntime::new(RuntimeOptions {
Expand All @@ -1380,3 +1387,11 @@ fn eval_context_with_code_cache() {
assert!(c.is_empty());
}
}

fn hash_source(source: &v8::String) -> u64 {
use std::hash::Hash;
use std::hash::Hasher;
let mut hasher = twox_hash::XxHash64::default();
source.hash(&mut hasher);
hasher.finish()
}
Loading