1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::{
env::current_dir,
path::{Path, PathBuf},
};
use anyhow::{Context, Error};
use parking_lot::Mutex;
use swc_common::{
collections::AHashMap,
sync::{Lazy, OnceCell},
};
use wasmer::{Module, Store};
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash};
#[cfg(all(not(feature = "filesystem_cache"), not(feature = "memory_cache")))]
compile_error!("Plugin_runner should enable either filesystem, or memory cache");
#[cfg(all(feature = "filesystem_cache", feature = "memory_cache"))]
compile_error!(
"Only one cache feature should be enabled. If you enabled filesystem_cache, it activates its \
memory cache as well."
);
const MODULE_SERIALIZATION_VERSION: &str = "v3";
pub static PLUGIN_MODULE_CACHE: Lazy<PluginModuleCache> = Lazy::new(Default::default);
#[cfg(feature = "filesystem_cache")]
#[derive(Default)]
pub struct CacheInner {
fs_cache: Option<FileSystemCache>,
loaded_module_bytes: AHashMap<PathBuf, Module>,
}
#[cfg(feature = "memory_cache")]
#[derive(Default)]
pub struct CacheInner {
loaded_module_bytes: AHashMap<PathBuf, Vec<u8>>,
}
#[derive(Default)]
pub struct PluginModuleCache {
inner: OnceCell<Mutex<CacheInner>>,
instantiation_lock: Mutex<()>,
}
#[cfg(feature = "filesystem_cache")]
#[tracing::instrument(level = "info", skip_all)]
fn create_filesystem_cache(filesystem_cache_root: &Option<String>) -> Option<FileSystemCache> {
let mut root_path = if let Some(root) = filesystem_cache_root {
Some(PathBuf::from(root))
} else if let Ok(cwd) = current_dir() {
Some(cwd.join(".swc"))
} else {
None
};
if let Some(root_path) = &mut root_path {
root_path.push("plugins");
root_path.push(MODULE_SERIALIZATION_VERSION);
return FileSystemCache::new(&root_path).ok();
}
None
}
#[cfg(feature = "filesystem_cache")]
pub fn init_plugin_module_cache_once(filesystem_cache_root: &Option<String>) {
PLUGIN_MODULE_CACHE.inner.get_or_init(|| {
Mutex::new(CacheInner {
fs_cache: create_filesystem_cache(filesystem_cache_root),
loaded_module_bytes: Default::default(),
})
});
}
#[cfg(feature = "memory_cache")]
pub fn init_plugin_module_cache_once() {
PLUGIN_MODULE_CACHE.inner.get_or_init(|| {
Mutex::new(CacheInner {
loaded_module_bytes: Default::default(),
})
});
}
impl PluginModuleCache {
pub fn new() -> Self {
PluginModuleCache {
inner: OnceCell::from(Mutex::new(Default::default())),
instantiation_lock: Mutex::new(()),
}
}
#[cfg(feature = "filesystem_cache")]
#[tracing::instrument(level = "info", skip_all)]
pub fn load_module(&self, binary_path: &Path) -> Result<Module, Error> {
let binary_path = binary_path.to_path_buf();
let mut inner_cache = self.inner.get().expect("Cache should be available").lock();
let in_memory_module = inner_cache.loaded_module_bytes.get(&binary_path);
if let Some(module) = in_memory_module {
return Ok(module.clone());
}
let module_bytes =
std::fs::read(&binary_path).context("Cannot read plugin from specified path")?;
let module_bytes_hash = Hash::generate(&module_bytes);
let wasmer_store = Store::default();
let load_cold_wasm_bytes = || {
let span = tracing::span!(
tracing::Level::INFO,
"load_cold_wasm_bytes",
plugin_module = binary_path.to_str()
);
let span_guard = span.enter();
let _lock = self.instantiation_lock.lock();
let ret =
Module::new(&wasmer_store, module_bytes).context("Cannot compile plugin binary");
drop(span_guard);
ret
};
let module = if let Some(fs_cache) = &mut inner_cache.fs_cache {
let load_result = unsafe { fs_cache.load(&wasmer_store, module_bytes_hash) };
if let Ok(module) = load_result {
module
} else {
let cold_bytes = load_cold_wasm_bytes()?;
fs_cache.store(module_bytes_hash, &cold_bytes)?;
cold_bytes
}
} else {
load_cold_wasm_bytes()?
};
inner_cache
.loaded_module_bytes
.insert(binary_path, module.clone());
Ok(module)
}
#[cfg(feature = "memory_cache")]
#[tracing::instrument(level = "info", skip_all)]
pub fn load_module(&self, binary_path: &Path) -> Result<Module, Error> {
let binary_path = binary_path.to_path_buf();
let mut inner_cache = self.inner.get().expect("Cache should be available").lock();
let in_memory_module_bytes = inner_cache
.loaded_module_bytes
.get(&binary_path)
.ok_or_else(|| {
anyhow::anyhow!("Could not locate plugin binary {}", binary_path.display())
})?;
let wasmer_store = Store::default();
let module = Module::new(&wasmer_store, in_memory_module_bytes)?;
Ok(module)
}
#[cfg(feature = "memory_cache")]
#[tracing::instrument(level = "info", skip_all)]
pub fn store_once(&self, module_name: &str, module_bytes: Vec<u8>) {
let binary_path = PathBuf::from(module_name);
let mut inner_cache = self.inner.get().expect("Cache should be available").lock();
if !inner_cache.loaded_module_bytes.contains_key(&binary_path) {
inner_cache
.loaded_module_bytes
.insert(binary_path, module_bytes);
}
}
}