From 33e27c545ed7515185ef93ed0c0c0f49e6759e01 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sun, 3 Apr 2022 18:04:57 +0430 Subject: [PATCH] AK: Return Optional from HashMap<..., T>::get() This avoids a useless copy of the value, as most of the users (except one) actually just need a reference to the value. --- AK/Traits.h | 4 ++-- .../LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AK/Traits.h b/AK/Traits.h index a797d4da96..fc67ce4b9d 100644 --- a/AK/Traits.h +++ b/AK/Traits.h @@ -16,8 +16,8 @@ namespace AK { template struct GenericTraits { - using PeekType = T; - using ConstPeekType = T; + using PeekType = T&; + using ConstPeekType = T const&; static constexpr bool is_trivial() { return false; } static constexpr bool equals(const T& a, const T& b) { return a == b; } template U> diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp index d90c092eec..95c586ce6b 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyInstanceObject.cpp @@ -34,7 +34,7 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object) for (auto& export_ : instance.exports()) { export_.value().visit( [&](Wasm::FunctionAddress const& address) { - auto object = cache.function_instances.get(address); + Optional object = cache.function_instances.get(address); if (!object.has_value()) { object = create_native_function(global_object, address, export_.name()); cache.function_instances.set(address, *object); @@ -42,7 +42,7 @@ void WebAssemblyInstanceObject::initialize(JS::GlobalObject& global_object) m_exports_object->define_direct_property(export_.name(), *object, JS::default_attributes); }, [&](Wasm::MemoryAddress const& address) { - auto object = cache.memory_instances.get(address); + Optional object = cache.memory_instances.get(address); if (!object.has_value()) { object = heap().allocate(global_object, global_object, address); cache.memory_instances.set(address, *object);