LibWasm: Replace a hashtable with an RBTree to make instantiation faster

...by about 40%.
This commit is contained in:
Ali Mohammad Pur 2024-07-18 03:41:06 +02:00 committed by Andreas Kling
commit 8cf0f36f7d
Notes: sideshowbarker 2024-07-18 23:45:29 +09:00
2 changed files with 13 additions and 6 deletions

View file

@ -124,15 +124,18 @@ ErrorOr<void, ValidationError> 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<FunctionIndex>());
if (instruction.opcode() == Instructions::ref_func) {
auto index = instruction.arguments().template get<FunctionIndex>();
m_context.references->tree.insert(index.value(), index);
}
}
};
module.for_each_section_of_type<ExportSection>([&](ExportSection const& section) {
for (auto& export_ : section.entries()) {
if (!export_.description().has<FunctionIndex>())
continue;
m_context.references.set(export_.description().get<FunctionIndex>());
auto index = export_.description().get<FunctionIndex>();
m_context.references->tree.insert(index.value(), index);
}
});
module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
@ -1313,7 +1316,7 @@ VALIDATE_INSTRUCTION(ref_func)
auto index = instruction.arguments().get<FunctionIndex>();
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;

View file

@ -8,7 +8,7 @@
#include <AK/COWVector.h>
#include <AK/Debug.h>
#include <AK/HashTable.h>
#include <AK/RedBlackTree.h>
#include <AK/SourceLocation.h>
#include <AK/Tuple.h>
#include <AK/Vector.h>
@ -18,6 +18,10 @@
namespace Wasm {
struct Context {
struct RefRBTree : RefCounted<RefRBTree> {
RedBlackTree<size_t, FunctionIndex> tree;
};
COWVector<FunctionType> types;
COWVector<FunctionType> functions;
COWVector<TableType> tables;
@ -27,7 +31,7 @@ struct Context {
COWVector<bool> datas;
COWVector<ValueType> locals;
Optional<u32> data_count;
AK::HashTable<FunctionIndex> references;
RefPtr<RefRBTree> references { make_ref_counted<RefRBTree>() };
size_t imported_function_count { 0 };
};