diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index e893406d6d08d2a759554f61b7d252f7f2041f8c..76a71d4a91586edf326bc11d54eb50b6a3583f68 100644 --- a/ets2panda/ir/base/methodDefinition.cpp +++ b/ets2panda/ir/base/methodDefinition.cpp @@ -26,7 +26,7 @@ namespace ark::es2panda::ir { void MethodDefinition::SetDefaultAccessModifier(bool isDefault) { - this->GetOrCreateHistoryNodeAs()->isDefault_ = isDefault; + this->GetOrCreateHistoryNodeAs()->kindFlags_.isDefault = isDefault; } void MethodDefinition::SetBaseOverloadMethod(MethodDefinition *const baseOverloadMethod) @@ -473,8 +473,7 @@ void MethodDefinition::CopyTo(AstNode *other) const { auto otherImpl = other->AsMethodDefinition(); - otherImpl->isDefault_ = isDefault_; - otherImpl->kind_ = kind_; + otherImpl->kindFlags_ = kindFlags_; otherImpl->overloads_ = overloads_; otherImpl->baseOverloadMethod_ = baseOverloadMethod_; otherImpl->asyncPairMethod_ = asyncPairMethod_; diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index 6ae3271cf5a6febf1a9a253e09ac1cab73c9c162..22ce92d5db482d7ce22f513b0e82d79a02a98303 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -61,7 +61,7 @@ public: explicit MethodDefinition(MethodDefinitionKind const kind, Expression *const key, Expression *const value, ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, isComputed), - kind_(kind), + kindFlags_ {kind, false}, overloads_(allocator->Adapter()), baseOverloadMethod_(nullptr), asyncPairMethod_(nullptr) @@ -74,7 +74,7 @@ public: ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed, AstNodeHistory *history) : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, isComputed), - kind_(kind), + kindFlags_ {kind, false}, overloads_(allocator->Adapter()), baseOverloadMethod_(nullptr), asyncPairMethod_(nullptr) @@ -91,7 +91,7 @@ public: MethodDefinitionKind Kind() const { - return GetHistoryNodeAs()->kind_; + return GetHistoryNodeAs()->kindFlags_.kind; } [[nodiscard]] bool IsConstructor() const noexcept @@ -123,7 +123,7 @@ public: [[nodiscard]] bool IsDefaultAccessModifier() const noexcept { - return GetHistoryNodeAs()->isDefault_; + return GetHistoryNodeAs()->kindFlags_.isDefault; } void SetDefaultAccessModifier(bool isDefault); @@ -252,8 +252,14 @@ private: bool FilterForDeclGen(ir::SrcDumper *dumper) const; friend class SizeOfNodeTest; - bool isDefault_ = false; - MethodDefinitionKind kind_; + + // Bit-field optimization: pack kind_ and isDefault_ into 8 bits + struct Flags { + MethodDefinitionKind kind : 4; // MethodDefinitionKind needs 8bits (NONE, CONSTRUCTOR, METHOD, + // EXTENSION_METHOD, GET, SET, EXTENSION_GET, EXTENSION_SET) + bool isDefault : 1; // bool needs 1 bit + unsigned : 3; // Reserved for future use + } kindFlags_; // Overloads are stored like in an 1:N fashion. // The very firstly processed method becomes the base(1) and the others tied into it as overloads(N). OverloadsT overloads_; diff --git a/ets2panda/ir/expressions/callExpression.cpp b/ets2panda/ir/expressions/callExpression.cpp index c0619e941d1b7b27068498d53a1ab72630f0ea31..a26551673350aa1e23d6259db3005bcefd0980fd 100644 --- a/ets2panda/ir/expressions/callExpression.cpp +++ b/ets2panda/ir/expressions/callExpression.cpp @@ -41,10 +41,10 @@ void CallExpression::TransformChildren(const NodeTransformer &cb, std::string_vi } } - if (trailingLambdaInfo_.block != nullptr) { - if (auto *transformedNode = cb(trailingLambdaInfo_.block); trailingLambdaInfo_.block != transformedNode) { - trailingLambdaInfo_.block->SetTransformedNode(transformationName, transformedNode); - trailingLambdaInfo_.block = transformedNode->AsBlockStatement(); + if (trailingBlock_ != nullptr) { + if (auto *transformedNode = cb(trailingBlock_); trailingBlock_ != transformedNode) { + trailingBlock_->SetTransformedNode(transformationName, transformedNode); + trailingBlock_ = transformedNode->AsBlockStatement(); } } } @@ -61,8 +61,8 @@ void CallExpression::Iterate(const NodeTraverser &cb) const cb(it); } - if (trailingLambdaInfo_.block != nullptr) { - cb(trailingLambdaInfo_.block); + if (trailingBlock_ != nullptr) { + cb(trailingBlock_); } } @@ -96,11 +96,11 @@ void CallExpression::Dump(ir::SrcDumper *dumper) const } } dumper->Add(")"); - if (trailingLambdaInfo_.block != nullptr) { - if (trailingLambdaInfo_.isBlockInNewLine) { + if (trailingBlock_ != nullptr) { + if (callFlags_.isBlockInNewLine) { dumper->Endl(); } - trailingLambdaInfo_.block->Dump(dumper); + trailingBlock_->Dump(dumper); } } @@ -128,9 +128,8 @@ CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *cons : MaybeOptionalExpression(static_cast(other)), arguments_(allocator->Adapter()), signature_(other.signature_), - trailingComma_(other.trailingComma_), - trailingLambdaInfo_({other.trailingLambdaInfo_.block, other.trailingLambdaInfo_.isBlockInNewLine, - other.trailingLambdaInfo_.isTrailingCall}) + trailingBlock_(nullptr), + callFlags_(other.callFlags_) { callee_ = other.callee_->Clone(allocator, this)->AsExpression(); typeParams_ = other.typeParams_ != nullptr ? other.typeParams_->Clone(allocator, this) : nullptr; @@ -139,9 +138,8 @@ CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *cons arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression()); } - trailingLambdaInfo_.block = other.trailingLambdaInfo_.block != nullptr - ? other.trailingLambdaInfo_.block->Clone(allocator, this)->AsBlockStatement() - : nullptr; + trailingBlock_ = + other.trailingBlock_ != nullptr ? other.trailingBlock_->Clone(allocator, this)->AsBlockStatement() : nullptr; } CallExpression *CallExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) @@ -166,9 +164,9 @@ void CallExpression::SetTypeParams(TSTypeParameterInstantiation *typeParams) noe void CallExpression::SetTrailingBlock(ir::BlockStatement *const block) noexcept { - trailingLambdaInfo_.block = block; - if (trailingLambdaInfo_.block != nullptr) { - trailingLambdaInfo_.block->SetParent(this); + trailingBlock_ = block; + if (trailingBlock_ != nullptr) { + trailingBlock_->SetParent(this); } } diff --git a/ets2panda/ir/expressions/callExpression.h b/ets2panda/ir/expressions/callExpression.h index 36d3e3be58be85e68fc5f9559146d1e5b2633941..ba9b53b82f7bf9a93628cbdd482e3640f130943a 100644 --- a/ets2panda/ir/expressions/callExpression.h +++ b/ets2panda/ir/expressions/callExpression.h @@ -50,7 +50,7 @@ public: callee_(callee), arguments_(std::move(arguments)), typeParams_(typeParams), - trailingComma_(trailingComma) + callFlags_ {trailingComma, false, false, 0} { } @@ -100,7 +100,7 @@ public: [[nodiscard]] bool HasTrailingComma() const noexcept { - return trailingComma_; + return callFlags_.trailingComma; } [[nodiscard]] checker::Signature *Signature() noexcept @@ -136,27 +136,27 @@ public: [[nodiscard]] ir::BlockStatement *TrailingBlock() const noexcept { - return trailingLambdaInfo_.block; + return trailingBlock_; } void SetIsTrailingBlockInNewLine(bool const isNewLine) noexcept { - trailingLambdaInfo_.isBlockInNewLine = isNewLine; + callFlags_.isBlockInNewLine = isNewLine; } [[nodiscard]] bool IsTrailingBlockInNewLine() const noexcept { - return trailingLambdaInfo_.isBlockInNewLine; + return callFlags_.isBlockInNewLine; } void SetIsTrailingCall(bool const isTrailingCall) noexcept { - trailingLambdaInfo_.isTrailingCall = isTrailingCall; + callFlags_.isTrailingCall = isTrailingCall; } [[nodiscard]] bool IsTrailingCall() const noexcept { - return trailingLambdaInfo_.isTrailingCall; + return callFlags_.isTrailingCall; } bool IsETSConstructorCall() const noexcept @@ -191,10 +191,11 @@ public: } private: - struct TrailingLambdaInfo { - ir::BlockStatement *block {nullptr}; - bool isTrailingCall {false}; - bool isBlockInNewLine {false}; + struct CallFlags { + bool trailingComma : 1; + bool isTrailingCall : 1; + bool isBlockInNewLine : 1; + unsigned reserved : 5; }; protected: @@ -203,10 +204,9 @@ protected: ArenaVector arguments_; TSTypeParameterInstantiation *typeParams_; checker::Signature *signature_ {}; - bool trailingComma_; - // for trailing lambda feature in ets - TrailingLambdaInfo trailingLambdaInfo_ {}; + ir::BlockStatement *trailingBlock_ = nullptr; checker::Type *uncheckedType_ {}; + CallFlags callFlags_ {}; // NOLINTEND(misc-non-private-member-variables-in-classes) }; } // namespace ark::es2panda::ir diff --git a/ets2panda/test/unit/sizeof_node_test.cpp b/ets2panda/test/unit/sizeof_node_test.cpp index 40ed613d568551f2bc679bd4a69cd6fac9458a17..2205a6cf62e7510ad46ce24cc03238d35f61d2ba 100644 --- a/ets2panda/test/unit/sizeof_node_test.cpp +++ b/ets2panda/test/unit/sizeof_node_test.cpp @@ -214,8 +214,7 @@ size_t SizeOfNodeTest::SizeOf() // clang-format off return SizeOf() + - Align(sizeof(node->isDefault_) + - sizeof(node->kind_)) + + Align(sizeof(node->kindFlags_)) + sizeof(node->overloads_) + sizeof(node->baseOverloadMethod_) + sizeof(node->asyncPairMethod_) +