LibWeb/WebAssembly: Avoid scanning all externs when resolving references

This was very hot on a profile of ruffle.
This commit is contained in:
Ali Mohammad Pur 2025-08-03 19:33:03 +02:00 committed by Ali Mohammad Pur
commit 1ef536194d
Notes: github-actions[bot] 2025-08-08 10:55:41 +00:00
2 changed files with 11 additions and 5 deletions

View file

@ -560,10 +560,8 @@ JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM& vm, JS::Value va
if (value.is_null())
return Wasm::Value(Wasm::ValueType { Wasm::ValueType::Kind::ExternReference });
auto& cache = get_cache(*vm.current_realm());
for (auto& entry : cache.extern_values()) {
if (entry.value == value)
return Wasm::Value { Wasm::Reference { Wasm::Reference::Extern { entry.key } } };
}
if (auto entry = cache.inverse_extern_values().get(value); entry.has_value())
return Wasm::Value { Wasm::Reference { Wasm::Reference::Extern { *entry } } };
Wasm::ExternAddress extern_addr = cache.extern_values().size();
cache.add_extern_value(extern_addr, value);
return Wasm::Value { Wasm::Reference { Wasm::Reference::Extern { extern_addr } } };