From 8737f2747ecf565842166caa81ce04df92bc9ccc Mon Sep 17 00:00:00 2001 From: udo Date: Thu, 16 Jul 2026 19:27:24 +0000 Subject: [PATCH] Reuse unit global types per worker --- src/wasm/worker.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/wasm/worker.cpp b/src/wasm/worker.cpp index 8db7b4c..400631f 100644 --- a/src/wasm/worker.cpp +++ b/src/wasm/worker.cpp @@ -658,7 +658,9 @@ class WasmWorker public: WasmWorkerConfig cfg; - explicit WasmWorker(WasmWorkerConfig config) : cfg(std::move(config)), engine(make_engine()) + explicit WasmWorker(WasmWorkerConfig config) : cfg(std::move(config)), engine(make_engine()), + unit_base_global_type(wasmtime::ValType::i32(), false), + unit_got_global_type(wasmtime::ValType::i32(), true) { } @@ -738,6 +740,8 @@ public: } wasmtime::Engine engine; + wasmtime::GlobalType unit_base_global_type; + wasmtime::GlobalType unit_got_global_type; std::optional core_module; WasmDylinkInfo core_dylink; @@ -1377,8 +1381,6 @@ private: imports.reserve(mod->imports.size()); std::vector> deferred_got; deferred_got.reserve(mod->got_memory_imports); - wasmtime::GlobalType base_global_type(wasmtime::ValType::i32(), false); - wasmtime::GlobalType got_global_type(wasmtime::ValType::i32(), true); for(const auto& import : mod->imports) { const String& name = import.name; @@ -1403,7 +1405,7 @@ private: if(import.kind == WasmUnitImportKind::MemoryBase || import.kind == WasmUnitImportKind::TableBase) { int32_t value = import.kind == WasmUnitImportKind::MemoryBase ? (int32_t)memory_base : (int32_t)table_base; - auto global = wasmtime::Global::create(cx, base_global_type, wasmtime::Val(value)); + auto global = wasmtime::Global::create(cx, worker.unit_base_global_type, wasmtime::Val(value)); if(!global) return("global create failed: " + String(global.err().message())); imports.push_back(global.ok()); @@ -1431,7 +1433,7 @@ private: std::chrono::steady_clock::now() - resolve_start).count(); if(resolved) { - auto global = wasmtime::Global::create(cx, got_global_type, wasmtime::Val((int32_t)address)); + auto global = wasmtime::Global::create(cx, worker.unit_got_global_type, wasmtime::Val((int32_t)address)); if(!global) return("global create failed: " + String(global.err().message())); imports.push_back(global.ok()); @@ -1440,7 +1442,7 @@ private: { // provisional; self-resolved from the unit's own export // (plus its memory base) after instantiation - auto global = wasmtime::Global::create(cx, got_global_type, wasmtime::Val((int32_t)0)); + auto global = wasmtime::Global::create(cx, worker.unit_got_global_type, wasmtime::Val((int32_t)0)); if(!global) return("global create failed: " + String(global.err().message())); deferred_got.push_back({ name, global.ok() }); @@ -1461,7 +1463,7 @@ private: error = place_funcref(*func, slot); if(error != "") return(error); - auto global = wasmtime::Global::create(cx, got_global_type, wasmtime::Val((int32_t)slot)); + auto global = wasmtime::Global::create(cx, worker.unit_got_global_type, wasmtime::Val((int32_t)slot)); if(!global) return("global create failed: " + String(global.err().message())); imports.push_back(global.ok());