Reuse unit global types per worker

This commit is contained in:
udo 2026-07-16 19:27:24 +00:00
parent 8ea1fed38b
commit 8737f2747e

View File

@ -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<wasmtime::Module> core_module;
WasmDylinkInfo core_dylink;
@ -1377,8 +1381,6 @@ private:
imports.reserve(mod->imports.size());
std::vector<std::pair<String, wasmtime::Global>> 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());