From 8cf0f36f7d917ce9f0f6759f27ba0553db00e82a Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 18 Jul 2024 03:41:06 +0200 Subject: [PATCH] LibWasm: Replace a hashtable with an RBTree to make instantiation faster ...by about 40%. --- .../Libraries/LibWasm/AbstractMachine/Validator.cpp | 11 +++++++---- .../Libraries/LibWasm/AbstractMachine/Validator.h | 8 ++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp index fb64c4e1b97..9c4d9bab444 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp @@ -124,15 +124,18 @@ ErrorOr Validator::validate(Module& module) // - Exports auto scan_expression_for_function_indices = [&](auto& expression) { for (auto& instruction : expression.instructions()) { - if (instruction.opcode() == Instructions::ref_func) - m_context.references.set(instruction.arguments().template get()); + if (instruction.opcode() == Instructions::ref_func) { + auto index = instruction.arguments().template get(); + m_context.references->tree.insert(index.value(), index); + } } }; module.for_each_section_of_type([&](ExportSection const& section) { for (auto& export_ : section.entries()) { if (!export_.description().has()) continue; - m_context.references.set(export_.description().get()); + auto index = export_.description().get(); + m_context.references->tree.insert(index.value(), index); } }); module.for_each_section_of_type([&](ElementSection const& section) { @@ -1313,7 +1316,7 @@ VALIDATE_INSTRUCTION(ref_func) auto index = instruction.arguments().get(); TRY(validate(index)); - if (!m_context.references.contains(index)) + if (m_context.references->tree.find(index.value()) == nullptr) return Errors::invalid("function reference"sv); is_constant = true; diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h index 61e62a37e1f..13863401196 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -18,6 +18,10 @@ namespace Wasm { struct Context { + struct RefRBTree : RefCounted { + RedBlackTree tree; + }; + COWVector types; COWVector functions; COWVector tables; @@ -27,7 +31,7 @@ struct Context { COWVector datas; COWVector locals; Optional data_count; - AK::HashTable references; + RefPtr references { make_ref_counted() }; size_t imported_function_count { 0 }; };