fix(plugin/runner): Use fs cache properly (#7748)

This commit is contained in:
Fy 2023-08-04 06:20:51 +08:00 committed by GitHub
parent 875a7a7393
commit 1122de7d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 15 deletions

View File

@ -696,17 +696,17 @@ impl Options {
// target.
init_plugin_module_cache_once(true, &experimental.cache_root);
let mut inner_cache = PLUGIN_MODULE_CACHE
.inner
.get()
.expect("Cache should be available")
.lock();
// Populate cache to the plugin modules if not loaded
for plugin_config in plugins.iter() {
let plugin_name = &plugin_config.0;
if !PLUGIN_MODULE_CACHE
.inner
.get()
.unwrap()
.lock()
.contains(&plugin_name)
{
if !inner_cache.contains(&plugin_name) {
let resolved_path = plugin_resolver.resolve(
&FileName::Real(PathBuf::from(&plugin_name)),
&plugin_name,
@ -718,12 +718,8 @@ impl Options {
anyhow::bail!("Failed to resolve plugin path: {:?}", resolved_path);
};
let mut inner_cache = PLUGIN_MODULE_CACHE
.inner
.get()
.expect("Cache should be available")
.lock();
inner_cache.store_bytes_from_path(&path, &plugin_name)?;
tracing::debug!("Initialized WASM plugin {plugin_name}");
}
}
}

View File

@ -116,9 +116,17 @@ impl PluginModuleCacheInner {
if let Some(fs_cache_store) = &mut self.fs_cache_store {
let module_bytes_hash = Hash::generate(&raw_module_bytes);
let store = new_store();
let module = Module::new(&store, raw_module_bytes.clone())
.context("Cannot compile plugin binary")?;
fs_cache_store.store(module_bytes_hash, &module)?;
let module =
if let Ok(module) = unsafe { fs_cache_store.load(&store, module_bytes_hash) } {
tracing::debug!("Build WASM from cache: {key}");
module
} else {
let module = Module::new(&store, raw_module_bytes.clone())
.context("Cannot compile plugin binary")?;
fs_cache_store.store(module_bytes_hash, &module)?;
module
};
// Store hash to load from fs_cache_store later.
self.fs_cache_hash_store