From 2b35e9f9becd63a90e2772614d0b111fa896a275 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 12 Aug 2021 00:55:40 +0430 Subject: [PATCH] LibWasm: Generate Value::type() on the fly instead of storing it The variant member already contains enough information to give us the type when needed, so remove the type member and synthesize it when needed, this allows lots of optimisation opportunaties when copying and moving Values around. --- .../LibWasm/AbstractMachine/AbstractMachine.h | 66 ++++++------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h index 659a3bae78..b391e19e18 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h @@ -66,37 +66,18 @@ class Value { public: Value() : m_value(0) - , m_type(ValueType::I32) { } using AnyValueType = Variant; explicit Value(AnyValueType value) : m_value(move(value)) - , m_type(ValueType::I32) { - if (m_value.has()) - m_type = ValueType { ValueType::I32 }; - else if (m_value.has()) - m_type = ValueType { ValueType::I64 }; - else if (m_value.has()) - m_type = ValueType { ValueType::F32 }; - else if (m_value.has()) - m_type = ValueType { ValueType::F64 }; - else if (m_value.has() && m_value.get().ref().has()) - m_type = ValueType { ValueType::FunctionReference }; - else if (m_value.has() && m_value.get().ref().has()) - m_type = ValueType { ValueType::ExternReference }; - else if (m_value.has()) - m_type = m_value.get().ref().get().type; - else - VERIFY_NOT_REACHED(); } template requires(sizeof(T) == sizeof(u64)) explicit Value(ValueType type, T raw_value) : m_value(0) - , m_type(type) { switch (type.kind()) { case ValueType::Kind::ExternReference: @@ -130,31 +111,10 @@ public: } } - ALWAYS_INLINE Value(Value const& value) - : m_value(AnyValueType { value.m_value }) - , m_type(value.m_type) - { - } - - ALWAYS_INLINE Value(Value&& value) - : m_value(move(value.m_value)) - , m_type(move(value.m_type)) - { - } - - ALWAYS_INLINE Value& operator=(Value&& value) - { - m_value = move(value.m_value); - m_type = move(value.m_type); - return *this; - } - - ALWAYS_INLINE Value& operator=(Value const& value) - { - m_value = value.m_value; - m_type = value.m_type; - return *this; - } + ALWAYS_INLINE Value(Value const& value) = default; + ALWAYS_INLINE Value(Value&& value) = default; + ALWAYS_INLINE Value& operator=(Value&& value) = default; + ALWAYS_INLINE Value& operator=(Value const& value) = default; template ALWAYS_INLINE Optional to() @@ -184,12 +144,26 @@ public: return result; } - auto& type() const { return m_type; } + ValueType type() const + { + return ValueType(m_value.visit( + [](i32) { return ValueType::Kind::I32; }, + [](i64) { return ValueType::Kind::I64; }, + [](float) { return ValueType::Kind::F32; }, + [](double) { return ValueType::Kind::F64; }, + [&](Reference const& type) { + return type.ref().visit( + [](Reference::Func const&) { return ValueType::Kind::FunctionReference; }, + [](Reference::Null const& null_type) { + return null_type.type.kind() == ValueType::ExternReference ? ValueType::Kind::NullExternReference : ValueType::Kind::NullFunctionReference; + }, + [](Reference::Extern const&) { return ValueType::Kind::ExternReference; }); + })); + } auto& value() const { return m_value; } private: AnyValueType m_value; - ValueType m_type; }; struct Trap {