MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / get_or_compile

Method get_or_compile

nodedb/src/control/planner/wasm/runtime.rs:50–72  ·  view source on GitHub ↗

Get or compile a WASM module from raw bytes. Modules are cached by SHA-256 hash of the binary. First compilation is slow (Cranelift JIT); subsequent calls return the cached module.

(&self, wasm_bytes: &[u8])

Source from the content-addressed store, hash-verified

48 /// Modules are cached by SHA-256 hash of the binary. First compilation
49 /// is slow (Cranelift JIT); subsequent calls return the cached module.
50 pub fn get_or_compile(&self, wasm_bytes: &[u8]) -> crate::Result<Arc<Module>> {
51 let hash = sha256(wasm_bytes);
52
53 // Fast path: check cache.
54 {
55 let cache = self.module_cache.lock().unwrap_or_else(|p| p.into_inner());
56 if let Some(module) = cache.get(&hash) {
57 return Ok(Arc::clone(module));
58 }
59 }
60
61 // Slow path: compile and cache.
62 let module =
63 Module::new(&self.engine, wasm_bytes).map_err(|e| crate::Error::BadRequest {
64 detail: format!("WASM module compilation failed: {e}"),
65 })?;
66 let arc = Arc::new(module);
67
68 let mut cache = self.module_cache.lock().unwrap_or_else(|p| p.into_inner());
69 cache.entry(hash).or_insert_with(|| Arc::clone(&arc));
70
71 Ok(arc)
72 }
73
74 /// Get a reference to the underlying wasmtime Engine.
75 pub fn engine(&self) -> &Engine {

Callers 1

create_wasm_aggregateFunction · 0.80

Calls 4

sha256Function · 0.85
lockMethod · 0.80
entryMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected