Skip to content

Commit 0f251e6

Browse files
committed
feat(plugin_runner): shared runtime engine
1 parent ed9a4ae commit 0f251e6

File tree

3 files changed

+60
-36
lines changed

3 files changed

+60
-36
lines changed

crates/swc_plugin_runner/src/cache.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use wasmer::{Module, Store};
1919
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
2020
use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash};
2121

22-
use crate::plugin_module_bytes::{
23-
CompiledPluginModuleBytes, PluginModuleBytes, RawPluginModuleBytes,
22+
use crate::{
23+
plugin_module_bytes::{CompiledPluginModuleBytes, PluginModuleBytes, RawPluginModuleBytes},
24+
wasix_runtime::new_store,
2425
};
2526

2627
/// Version for bytecode cache stored in local filesystem.
@@ -114,7 +115,7 @@ impl PluginModuleCacheInner {
114115
// If FilesystemCache is available, store serialized bytes into fs.
115116
if let Some(fs_cache_store) = &mut self.fs_cache_store {
116117
let module_bytes_hash = Hash::generate(&raw_module_bytes);
117-
let store = crate::plugin_module_bytes::new_store();
118+
let store = new_store();
118119
let module = Module::new(&store, raw_module_bytes.clone())
119120
.context("Cannot compile plugin binary")?;
120121
fs_cache_store.store(module_bytes_hash, &module)?;
@@ -154,7 +155,7 @@ impl PluginModuleCacheInner {
154155
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
155156
if let Some(fs_cache_store) = &self.fs_cache_store {
156157
let hash = self.fs_cache_hash_store.get(key)?;
157-
let store = crate::plugin_module_bytes::new_store();
158+
let store = new_store();
158159
let module = unsafe { fs_cache_store.load(&store, *hash) };
159160
if let Ok(module) = module {
160161
return Some(Box::new(CompiledPluginModuleBytes::new(

crates/swc_plugin_runner/src/plugin_module_bytes.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@ use anyhow::Error;
22
use serde::{Deserialize, Serialize};
33
use wasmer::{Module, Store};
44

5-
/// Creates an instnace of [Store].
6-
///
7-
/// This function exists because we need to disable simd.
8-
#[cfg(not(target_arch = "wasm32"))]
9-
#[allow(unused_mut)]
10-
pub(crate) fn new_store() -> Store {
11-
// Use empty enumset to disable simd.
12-
use enumset::EnumSet;
13-
use wasmer::{BaseTunables, CompilerConfig, EngineBuilder, Target, Triple};
14-
let mut set = EnumSet::new();
15-
16-
// [TODO]: Should we use is_x86_feature_detected! macro instead?
17-
#[cfg(target_arch = "x86_64")]
18-
set.insert(wasmer::CpuFeature::SSE2);
19-
let target = Target::new(Triple::host(), set);
20-
21-
let config = wasmer_compiler_cranelift::Cranelift::default();
22-
let mut engine = EngineBuilder::new(Box::new(config) as Box<dyn CompilerConfig>)
23-
.set_target(Some(target))
24-
.engine();
25-
let tunables = BaseTunables::for_target(engine.target());
26-
engine.set_tunables(tunables);
27-
28-
Store::new(engine)
29-
}
30-
31-
#[cfg(target_arch = "wasm32")]
32-
fn new_store() -> Store {
33-
Store::default()
34-
}
5+
use crate::wasix_runtime::new_store;
356

367
// A trait abstracts plugin's wasm compilation and instantiation.
378
// Depends on the caller, this could be a simple clone from existing module, or

crates/swc_plugin_runner/src/wasix_runtime.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
use std::{path::PathBuf, sync::Arc};
22

3+
use parking_lot::Mutex;
4+
use swc_common::sync::{Lazy, OnceCell};
5+
use wasmer::Store;
36
use wasmer_wasix::Runtime;
47

8+
/// A shared instance to plugin runtime engine.
9+
/// ref: https://github.com/wasmerio/wasmer/issues/3793#issuecomment-1607117480
10+
static ENGINE: Lazy<OnceCell<Mutex<wasmer::Engine>>> = Lazy::new(|| {
11+
// Use empty enumset to disable simd.
12+
use enumset::EnumSet;
13+
use wasmer::{BaseTunables, CompilerConfig, EngineBuilder, Target, Triple};
14+
let mut set = EnumSet::new();
15+
16+
// [TODO]: Should we use is_x86_feature_detected! macro instead?
17+
#[cfg(target_arch = "x86_64")]
18+
set.insert(wasmer::CpuFeature::SSE2);
19+
let target = Target::new(Triple::host(), set);
20+
21+
let config = wasmer_compiler_cranelift::Cranelift::default();
22+
let mut engine = EngineBuilder::new(Box::new(config) as Box<dyn CompilerConfig>)
23+
.set_target(Some(target))
24+
.engine();
25+
let tunables = BaseTunables::for_target(engine.target());
26+
engine.set_tunables(tunables);
27+
OnceCell::with_value(parking_lot::Mutex::new(wasmer::Engine::from(engine)))
28+
});
29+
30+
/// Dummy http client for wasix runtime to avoid instantiation failure for the
31+
/// default pluggable runtime. We don't support network in the host runtime
32+
/// anyway (we init vnet instead), and for the default runtime mostly it's for
33+
/// the wapm registry which is redundant for the plugin.
534
#[derive(Debug)]
635
struct StubHttpClient;
736

@@ -38,11 +67,16 @@ pub fn build_wasi_runtime(
3867
SharedCache::default().with_fallback(wasmer_wasix::runtime::module_cache::in_memory());
3968

4069
let dummy_loader = BuiltinPackageLoader::new_with_client(".", Arc::new(StubHttpClient));
41-
4270
let rt = PluggableRuntime {
4371
rt: Arc::new(TokioTaskManager::shared()),
4472
networking: Arc::new(virtual_net::UnsupportedVirtualNetworking::default()),
45-
engine: Some(wasmer::Engine::default()),
73+
engine: Some(
74+
ENGINE
75+
.get()
76+
.expect("Engine should be initialized all time")
77+
.lock()
78+
.clone(),
79+
),
4680
tty: None,
4781
source: Arc::new(MultiSource::new()),
4882
module_cache: Arc::new(cache),
@@ -52,3 +86,21 @@ pub fn build_wasi_runtime(
5286

5387
Some(Arc::new(rt))
5488
}
89+
90+
/// Creates an instnace of [Store] with custom engine instead of default one to
91+
/// disable simd for certain platform targets
92+
#[cfg(not(target_arch = "wasm32"))]
93+
#[allow(unused_mut)]
94+
pub(crate) fn new_store() -> Store {
95+
let engine = ENGINE
96+
.get()
97+
.expect("Engine should be initialized all time")
98+
.lock()
99+
.clone();
100+
Store::new(engine)
101+
}
102+
103+
#[cfg(target_arch = "wasm32")]
104+
pub(crate) fn new_store() -> Store {
105+
Store::default()
106+
}

0 commit comments

Comments
 (0)