From 4143e9ad50c87cf064b2d31ae550861784deb5f9 Mon Sep 17 00:00:00 2001 From: luobohua Date: Sun, 10 Aug 2025 21:03:19 +0800 Subject: [PATCH 1/2] use bitfield to save 4B for methodDefinition Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICS787 Change-Id: I489591969a8cdf44682d9419e18caf047f85bccf Signed-off-by: luobohua --- ets2panda/ir/base/methodDefinition.cpp | 5 ++--- ets2panda/ir/base/methodDefinition.h | 18 ++++++++++++------ ets2panda/test/unit/sizeof_node_test.cpp | 3 +-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index e893406d6d..76a71d4a91 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 6ae3271cf5..22ce92d5db 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/test/unit/sizeof_node_test.cpp b/ets2panda/test/unit/sizeof_node_test.cpp index 40ed613d56..2205a6cf62 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_) + -- Gitee From 26980c762a98cf982050865a27a8177c6bf79d6d Mon Sep 17 00:00:00 2001 From: luobohua Date: Tue, 12 Aug 2025 14:34:13 +0800 Subject: [PATCH 2/2] use bitfield to save 8B for every CallExpression Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICS787 Change-Id: I8c9301a3bac2e41c31ddcc61d59ef41cf3d38f77 Signed-off-by: luobohua --- ets2panda/ir/expressions/callExpression.cpp | 34 ++++++++++----------- ets2panda/ir/expressions/callExpression.h | 28 ++++++++--------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/ets2panda/ir/expressions/callExpression.cpp b/ets2panda/ir/expressions/callExpression.cpp index c0619e941d..a265516733 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 36d3e3be58..ba9b53b82f 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 -- Gitee