From cced5558797a7a1ee33933a99798db7bdc91a9e8 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 11 Mar 2024 16:54:37 +0100 Subject: [PATCH] LibWasm: Avoid pointless vector copies in Validator::Context These vector copies accounted for more than 50% of the current runtime of the validator on a large wasm file, this commit makes them copy-on-write to avoid the copies where possible, gaining nearly a 50% speedup. --- .../LibWasm/AbstractMachine/Validator.cpp | 11 +++++----- .../LibWasm/AbstractMachine/Validator.h | 21 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp index fcbb76525f..eb994d49e3 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.cpp @@ -38,7 +38,8 @@ ErrorOr Validator::validate(Module& module) m_context = {}; module.for_each_section_of_type([this](TypeSection const& section) { - m_context.types = section.types(); + for (auto& type : section.types()) + m_context.types.append(type); }); module.for_each_section_of_type([&](ImportSection const& section) { @@ -90,23 +91,23 @@ ErrorOr Validator::validate(Module& module) module.for_each_section_of_type([this](TableSection const& section) { m_context.tables.ensure_capacity(m_context.tables.size() + section.tables().size()); for (auto& table : section.tables()) - m_context.tables.unchecked_append(table.type()); + m_context.tables.append(table.type()); }); module.for_each_section_of_type([this](MemorySection const& section) { m_context.memories.ensure_capacity(m_context.memories.size() + section.memories().size()); for (auto& memory : section.memories()) - m_context.memories.unchecked_append(memory.type()); + m_context.memories.append(memory.type()); }); module.for_each_section_of_type([this](GlobalSection const& section) { m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size()); for (auto& global : section.entries()) - m_context.globals.unchecked_append(global.type()); + m_context.globals.append(global.type()); }); module.for_each_section_of_type([this](ElementSection const& section) { m_context.elements.ensure_capacity(section.segments().size()); for (auto& segment : section.segments()) - m_context.elements.unchecked_append(segment.type); + m_context.elements.append(segment.type); }); module.for_each_section_of_type([this](DataSection const& section) { m_context.datas.resize(section.data().size()); diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h index bcf7377487..7e69b0ae33 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Validator.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/Validator.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -17,15 +18,15 @@ namespace Wasm { struct Context { - Vector types; - Vector functions; - Vector tables; - Vector memories; - Vector globals; - Vector elements; - Vector datas; - Vector locals; - Vector labels; + COWVector types; + COWVector functions; + COWVector tables; + COWVector memories; + COWVector globals; + COWVector elements; + COWVector datas; + COWVector locals; + COWVector labels; Optional return_; AK::HashTable references; size_t imported_function_count { 0 }; @@ -345,7 +346,7 @@ private: Vector m_entered_scopes; Vector m_block_details; Vector m_entered_blocks; - Vector m_globals_without_internal_globals; + COWVector m_globals_without_internal_globals; }; }