From a7c1af08cab11d64fbe7da1eeacf421d9e25e24c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 13 Sep 2023 21:32:51 +0200 Subject: [PATCH] LibJS: Store bytecode instruction length in instruction itself Instead of running a big switch statement on the opcode when checking how long an instruction is, we now simply store that in a member variable at construction time for instant access. This yields a 10.2% speed-up on Kraken/ai-astar :^) --- .../Libraries/LibJS/Bytecode/Instruction.h | 6 +- Userland/Libraries/LibJS/Bytecode/Op.h | 188 ++++++++---------- 2 files changed, 90 insertions(+), 104 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Instruction.h b/Userland/Libraries/LibJS/Bytecode/Instruction.h index 6f3fddfaa3..3330569a30 100644 --- a/Userland/Libraries/LibJS/Bytecode/Instruction.h +++ b/Userland/Libraries/LibJS/Bytecode/Instruction.h @@ -134,7 +134,7 @@ public: bool is_terminator() const; Type type() const { return m_type; } - size_t length() const; + size_t length() const { return m_length; } DeprecatedString to_deprecated_string(Bytecode::Executable const&) const; ThrowCompletionOr execute(Bytecode::Interpreter&) const; static void destroy(Instruction&); @@ -144,14 +144,16 @@ public: SourceRecord source_record() const { return m_source_record; } protected: - explicit Instruction(Type type) + Instruction(Type type, size_t length) : m_type(type) + , m_length(length) { } private: SourceRecord m_source_record {}; Type m_type {}; + size_t m_length {}; }; class InstructionStreamIterator { diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index f4508e92bd..850812e93d 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -32,7 +32,7 @@ namespace JS::Bytecode::Op { class Load final : public Instruction { public: explicit Load(Register src) - : Instruction(Type::Load) + : Instruction(Type::Load, sizeof(*this)) , m_src(src) { } @@ -47,7 +47,7 @@ private: class LoadImmediate final : public Instruction { public: explicit LoadImmediate(Value value) - : Instruction(Type::LoadImmediate) + : Instruction(Type::LoadImmediate, sizeof(*this)) , m_value(value) { } @@ -62,7 +62,7 @@ private: class Store final : public Instruction { public: explicit Store(Register dst) - : Instruction(Type::Store) + : Instruction(Type::Store, sizeof(*this)) , m_dst(dst) { } @@ -104,7 +104,7 @@ private: class OpTitleCase final : public Instruction { \ public: \ explicit OpTitleCase(Register lhs_reg) \ - : Instruction(Type::OpTitleCase) \ + : Instruction(Type::OpTitleCase, sizeof(*this)) \ , m_lhs_reg(lhs_reg) \ { \ } \ @@ -130,7 +130,7 @@ JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP) class OpTitleCase final : public Instruction { \ public: \ OpTitleCase() \ - : Instruction(Type::OpTitleCase) \ + : Instruction(Type::OpTitleCase, sizeof(*this)) \ { \ } \ \ @@ -144,7 +144,7 @@ JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP) class NewString final : public Instruction { public: explicit NewString(StringTableIndex string) - : Instruction(Type::NewString) + : Instruction(Type::NewString, sizeof(*this)) , m_string(string) { } @@ -159,7 +159,7 @@ private: class NewObject final : public Instruction { public: NewObject() - : Instruction(Type::NewObject) + : Instruction(Type::NewObject, sizeof(*this)) { } @@ -170,7 +170,7 @@ public: class NewRegExp final : public Instruction { public: NewRegExp(StringTableIndex source_index, StringTableIndex flags_index, RegexTableIndex regex_index) - : Instruction(Type::NewRegExp) + : Instruction(Type::NewRegExp, sizeof(*this)) , m_source_index(source_index) , m_flags_index(flags_index) , m_regex_index(regex_index) @@ -193,7 +193,7 @@ private: class New##ErrorName final : public Instruction { \ public: \ explicit New##ErrorName(StringTableIndex error_string) \ - : Instruction(Type::New##ErrorName) \ + : Instruction(Type::New##ErrorName, sizeof(*this)) \ , m_error_string(error_string) \ { \ } \ @@ -212,7 +212,7 @@ JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP) class CopyObjectExcludingProperties final : public Instruction { public: CopyObjectExcludingProperties(Register from_object, Vector const& excluded_names) - : Instruction(Type::CopyObjectExcludingProperties) + : Instruction(Type::CopyObjectExcludingProperties, length_impl(excluded_names.size())) , m_from_object(from_object) , m_excluded_names_count(excluded_names.size()) { @@ -223,7 +223,10 @@ public: ThrowCompletionOr execute_impl(Bytecode::Interpreter&) const; DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; - size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; } + size_t length_impl(size_t excluded_names_count) const + { + return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Register) * excluded_names_count); + } private: Register m_from_object; @@ -234,7 +237,7 @@ private: class NewBigInt final : public Instruction { public: explicit NewBigInt(Crypto::SignedBigInteger bigint) - : Instruction(Type::NewBigInt) + : Instruction(Type::NewBigInt, sizeof(*this)) , m_bigint(move(bigint)) { } @@ -250,13 +253,13 @@ private: class NewArray final : public Instruction { public: NewArray() - : Instruction(Type::NewArray) + : Instruction(Type::NewArray, length_impl(0)) , m_element_count(0) { } explicit NewArray(AK::Array const& elements_range) - : Instruction(Type::NewArray) + : Instruction(Type::NewArray, length_impl(elements_range[1].index() - elements_range[0].index() + 1)) , m_element_count(elements_range[1].index() - elements_range[0].index() + 1) { m_elements[0] = elements_range[0]; @@ -266,9 +269,9 @@ public: ThrowCompletionOr execute_impl(Bytecode::Interpreter&) const; DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; - size_t length_impl() const + size_t length_impl(size_t element_count) const { - return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2); + return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Register) * (element_count == 0 ? 0 : 2)); } Register start() const @@ -293,7 +296,7 @@ private: class Append final : public Instruction { public: Append(Register lhs, bool is_spread) - : Instruction(Type::Append) + : Instruction(Type::Append, sizeof(*this)) , m_lhs(lhs) , m_is_spread(is_spread) { @@ -310,7 +313,7 @@ private: class ImportCall final : public Instruction { public: ImportCall(Register specifier, Register options) - : Instruction(Type::ImportCall) + : Instruction(Type::ImportCall, sizeof(*this)) , m_specifier(specifier) , m_options(options) { @@ -327,7 +330,7 @@ private: class IteratorToArray final : public Instruction { public: IteratorToArray() - : Instruction(Type::IteratorToArray) + : Instruction(Type::IteratorToArray, sizeof(*this)) { } @@ -338,7 +341,7 @@ public: class ConcatString final : public Instruction { public: explicit ConcatString(Register lhs) - : Instruction(Type::ConcatString) + : Instruction(Type::ConcatString, sizeof(*this)) , m_lhs(lhs) { } @@ -358,7 +361,7 @@ enum class EnvironmentMode { class CreateLexicalEnvironment final : public Instruction { public: explicit CreateLexicalEnvironment() - : Instruction(Type::CreateLexicalEnvironment) + : Instruction(Type::CreateLexicalEnvironment, sizeof(*this)) { } @@ -369,7 +372,7 @@ public: class EnterObjectEnvironment final : public Instruction { public: explicit EnterObjectEnvironment() - : Instruction(Type::EnterObjectEnvironment) + : Instruction(Type::EnterObjectEnvironment, sizeof(*this)) { } @@ -380,7 +383,7 @@ public: class CreateVariable final : public Instruction { public: explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false) - : Instruction(Type::CreateVariable) + : Instruction(Type::CreateVariable, sizeof(*this)) , m_identifier(identifier) , m_mode(mode) , m_is_immutable(is_immutable) @@ -406,7 +409,7 @@ public: InitializeOrSet, }; explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical) - : Instruction(Type::SetVariable) + : Instruction(Type::SetVariable, sizeof(*this)) , m_identifier(identifier) , m_mode(mode) , m_initialization_mode(initialization_mode) @@ -427,7 +430,7 @@ private: class SetLocal final : public Instruction { public: explicit SetLocal(size_t index) - : Instruction(Type::SetLocal) + : Instruction(Type::SetLocal, sizeof(*this)) , m_index(index) { } @@ -444,7 +447,7 @@ private: class GetCalleeAndThisFromEnvironment final : public Instruction { public: explicit GetCalleeAndThisFromEnvironment(IdentifierTableIndex identifier, Register callee_reg, Register this_reg) - : Instruction(Type::GetCalleeAndThisFromEnvironment) + : Instruction(Type::GetCalleeAndThisFromEnvironment, sizeof(*this)) , m_identifier(identifier) , m_callee_reg(callee_reg) , m_this_reg(this_reg) @@ -467,7 +470,7 @@ private: class GetVariable final : public Instruction { public: explicit GetVariable(IdentifierTableIndex identifier) - : Instruction(Type::GetVariable) + : Instruction(Type::GetVariable, sizeof(*this)) , m_identifier(identifier) { } @@ -486,7 +489,7 @@ private: class GetGlobal final : public Instruction { public: explicit GetGlobal(IdentifierTableIndex identifier, u32 cache_index) - : Instruction(Type::GetGlobal) + : Instruction(Type::GetGlobal, sizeof(*this)) , m_identifier(identifier) , m_cache_index(cache_index) { @@ -503,7 +506,7 @@ private: class GetLocal final : public Instruction { public: explicit GetLocal(size_t index) - : Instruction(Type::GetLocal) + : Instruction(Type::GetLocal, sizeof(*this)) , m_index(index) { } @@ -520,7 +523,7 @@ private: class DeleteVariable final : public Instruction { public: explicit DeleteVariable(IdentifierTableIndex identifier) - : Instruction(Type::DeleteVariable) + : Instruction(Type::DeleteVariable, sizeof(*this)) , m_identifier(identifier) { } @@ -537,7 +540,7 @@ private: class GetById final : public Instruction { public: GetById(IdentifierTableIndex property, u32 cache_index) - : Instruction(Type::GetById) + : Instruction(Type::GetById, sizeof(*this)) , m_property(property) , m_cache_index(cache_index) { @@ -554,7 +557,7 @@ private: class GetByIdWithThis final : public Instruction { public: GetByIdWithThis(IdentifierTableIndex property, Register this_value, u32 cache_index) - : Instruction(Type::GetByIdWithThis) + : Instruction(Type::GetByIdWithThis, sizeof(*this)) , m_property(property) , m_this_value(this_value) , m_cache_index(cache_index) @@ -573,7 +576,7 @@ private: class GetPrivateById final : public Instruction { public: explicit GetPrivateById(IdentifierTableIndex property) - : Instruction(Type::GetPrivateById) + : Instruction(Type::GetPrivateById, sizeof(*this)) , m_property(property) { } @@ -588,7 +591,7 @@ private: class HasPrivateId final : public Instruction { public: explicit HasPrivateId(IdentifierTableIndex property) - : Instruction(Type::HasPrivateId) + : Instruction(Type::HasPrivateId, sizeof(*this)) , m_property(property) { } @@ -612,7 +615,7 @@ enum class PropertyKind { class PutById final : public Instruction { public: explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue) - : Instruction(Type::PutById) + : Instruction(Type::PutById, sizeof(*this)) , m_base(base) , m_property(property) , m_kind(kind) @@ -631,7 +634,7 @@ private: class PutByIdWithThis final : public Instruction { public: PutByIdWithThis(Register base, Register this_value, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue) - : Instruction(Type::PutByIdWithThis) + : Instruction(Type::PutByIdWithThis, sizeof(*this)) , m_base(base) , m_this_value(this_value) , m_property(property) @@ -652,7 +655,7 @@ private: class PutPrivateById final : public Instruction { public: explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue) - : Instruction(Type::PutPrivateById) + : Instruction(Type::PutPrivateById, sizeof(*this)) , m_base(base) , m_property(property) , m_kind(kind) @@ -671,7 +674,7 @@ private: class DeleteById final : public Instruction { public: explicit DeleteById(IdentifierTableIndex property) - : Instruction(Type::DeleteById) + : Instruction(Type::DeleteById, sizeof(*this)) , m_property(property) { } @@ -686,7 +689,7 @@ private: class DeleteByIdWithThis final : public Instruction { public: DeleteByIdWithThis(Register this_value, IdentifierTableIndex property) - : Instruction(Type::DeleteByIdWithThis) + : Instruction(Type::DeleteByIdWithThis, sizeof(*this)) , m_this_value(this_value) , m_property(property) { @@ -703,7 +706,7 @@ private: class GetByValue final : public Instruction { public: explicit GetByValue(Register base) - : Instruction(Type::GetByValue) + : Instruction(Type::GetByValue, sizeof(*this)) , m_base(base) { } @@ -718,7 +721,7 @@ private: class GetByValueWithThis final : public Instruction { public: GetByValueWithThis(Register base, Register this_value) - : Instruction(Type::GetByValueWithThis) + : Instruction(Type::GetByValueWithThis, sizeof(*this)) , m_base(base) , m_this_value(this_value) { @@ -735,7 +738,7 @@ private: class PutByValue final : public Instruction { public: PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue) - : Instruction(Type::PutByValue) + : Instruction(Type::PutByValue, sizeof(*this)) , m_base(base) , m_property(property) , m_kind(kind) @@ -754,7 +757,7 @@ private: class PutByValueWithThis final : public Instruction { public: PutByValueWithThis(Register base, Register property, Register this_value, PropertyKind kind = PropertyKind::KeyValue) - : Instruction(Type::PutByValueWithThis) + : Instruction(Type::PutByValueWithThis, sizeof(*this)) , m_base(base) , m_property(property) , m_this_value(this_value) @@ -775,7 +778,7 @@ private: class DeleteByValue final : public Instruction { public: DeleteByValue(Register base) - : Instruction(Type::DeleteByValue) + : Instruction(Type::DeleteByValue, sizeof(*this)) , m_base(base) { } @@ -790,7 +793,7 @@ private: class DeleteByValueWithThis final : public Instruction { public: DeleteByValueWithThis(Register base, Register this_value) - : Instruction(Type::DeleteByValueWithThis) + : Instruction(Type::DeleteByValueWithThis, sizeof(*this)) , m_base(base) , m_this_value(this_value) { @@ -809,14 +812,14 @@ public: constexpr static bool IsTerminator = true; explicit Jump(Type type, Optional