From dd3840e36ca4c0ef8248161e263d5ff83d7d78ae Mon Sep 17 00:00:00 2001 From: zhaiyukun Date: Tue, 10 Jun 2025 01:39:14 +0800 Subject: [PATCH 1/2] [Memory] Avoid duplicate copies of the record table entries Signed-off-by: zhaiyukun --- ets2panda/compiler/core/ETSemitter.cpp | 60 +++++++++-------- .../mutiple_annotations_for_class.cpp | 38 +++++------ .../test/unit/annotations/standard_test.cpp | 64 +++++++++---------- .../test/unit/union_normalisation_test.h | 16 ++--- ets2panda/test/utils/checker_test.cpp | 18 +++--- ets2panda/varbinder/ETSBinder.cpp | 2 - ets2panda/varbinder/recordTable.cpp | 46 +++++++++++++ ets2panda/varbinder/recordTable.h | 25 ++------ 8 files changed, 155 insertions(+), 114 deletions(-) diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 8f0b822ef9..d9902baa3c 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -268,21 +268,24 @@ void ETSEmitter::GenAnnotation() const auto *varbinder = static_cast(Context()->parserProgram->VarBinder()); auto *globalRecordTable = varbinder->GetGlobalRecordTable(); - auto baseName = varbinder->GetRecordTable()->RecordName().Mutf8(); - for (auto *annoDecl : globalRecordTable->AnnotationDeclarations()) { - auto newBaseName = GenerateMangledName(baseName, annoDecl->GetBaseName()->Name().Mutf8()); - GenCustomAnnotationRecord(annoDecl, newBaseName, annoDecl->IsDeclare()); - } - - for (auto *classDecl : globalRecordTable->ClassDefinitions()) { + auto classDeclHandler = [this](const ir::ClassDefinition *classDecl) { GenClassRecord(classDecl, classDecl->IsDeclare()); - } + }; + globalRecordTable->Visit(classDeclHandler); - for (auto *interfaceDecl : globalRecordTable->InterfaceDeclarations()) { + auto interfaceDeclHandler = [this](const ir::TSInterfaceDeclaration *interfaceDecl) { GenInterfaceRecord(interfaceDecl, interfaceDecl->IsDeclare()); - } + }; + globalRecordTable->Visit(interfaceDeclHandler); - for (auto *signature : globalRecordTable->Signatures()) { + auto baseName = varbinder->GetRecordTable()->RecordName().Mutf8(); + auto annoDeclHandler = [this, &baseName](const ir::AnnotationDeclaration *annoDecl) { + auto newBaseName = GenerateMangledName(baseName, annoDecl->GetBaseName()->Name().Mutf8()); + GenCustomAnnotationRecord(annoDecl, newBaseName, annoDecl->IsDeclare()); + }; + globalRecordTable->Visit(annoDeclHandler); + + auto signatureHandler = [this](const varbinder::FunctionScope *signature) { auto *scriptFunc = signature->Node()->AsScriptFunction(); auto func = GenScriptFunction(scriptFunc, this); if (scriptFunc->IsDeclare()) { @@ -290,11 +293,12 @@ void ETSEmitter::GenAnnotation() } if (scriptFunc->IsAsyncFunc()) { std::vector annotations; - annotations.push_back(GenAnnotationAsync(scriptFunc)); + annotations.push_back(GenAnnotationAsync(const_cast(scriptFunc))); func.metadata->AddAnnotations(annotations); } Program()->AddToFunctionTable(std::move(func)); - } + }; + globalRecordTable->Visit(signatureHandler); for (auto [extProg, recordTable] : varbinder->GetExternalRecordTable()) { if (recordTable == varbinder->GetRecordTable()) { @@ -326,21 +330,24 @@ void ETSEmitter::GenExternalRecord(varbinder::RecordTable *recordTable, const pa { bool isGenStdLib = recordTable->Program()->VarBinder()->IsGenStdLib(); const auto *varbinder = static_cast(Context()->parserProgram->VarBinder()); - auto baseName = varbinder->GetRecordTable()->RecordName().Mutf8(); - for (auto *annoDecl : recordTable->AnnotationDeclarations()) { - auto newBaseName = GenerateMangledName(baseName, annoDecl->GetBaseName()->Name().Mutf8()); - GenCustomAnnotationRecord(annoDecl, newBaseName, !isGenStdLib); - } - - for (auto *classDecl : recordTable->ClassDefinitions()) { + auto classDeclHandler = [this, &isGenStdLib](const ir::ClassDefinition *classDecl) { GenClassRecord(classDecl, !isGenStdLib); - } + }; + recordTable->Visit(classDeclHandler); - for (auto *interfaceDecl : recordTable->InterfaceDeclarations()) { + auto interfaceDeclHandler = [this, &isGenStdLib](const ir::TSInterfaceDeclaration *interfaceDecl) { GenInterfaceRecord(interfaceDecl, !isGenStdLib); - } + }; + recordTable->Visit(interfaceDeclHandler); - for (auto const *signature : recordTable->Signatures()) { + auto baseName = varbinder->GetRecordTable()->RecordName().Mutf8(); + auto annoDeclHandler = [this, &baseName, &isGenStdLib](const ir::AnnotationDeclaration *annoDecl) { + auto newBaseName = GenerateMangledName(baseName, annoDecl->GetBaseName()->Name().Mutf8()); + GenCustomAnnotationRecord(annoDecl, newBaseName, !isGenStdLib); + }; + recordTable->Visit(annoDeclHandler); + + auto signatureHandler = [this, &isGenStdLib, &extProg](const varbinder::FunctionScope *signature) { auto func = GenScriptFunction(signature->Node()->AsScriptFunction(), this); if (!isGenStdLib) { @@ -348,13 +355,14 @@ void ETSEmitter::GenExternalRecord(varbinder::RecordTable *recordTable, const pa } if (func.metadata->IsForeign() && IsFromSelfHeadFile(func.name, Context()->parserProgram, extProg)) { - continue; + return; } if (Program()->functionStaticTable.find(func.name) == Program()->functionStaticTable.cend()) { Program()->AddToFunctionTable(std::move(func)); } - } + }; + recordTable->Visit(signatureHandler); } // Helper function to reduce EmitDefaultFieldValue size and pass code check diff --git a/ets2panda/test/unit/annotations/mutiple_annotations_for_class.cpp b/ets2panda/test/unit/annotations/mutiple_annotations_for_class.cpp index 4e7ef99a40..dae46259c4 100644 --- a/ets2panda/test/unit/annotations/mutiple_annotations_for_class.cpp +++ b/ets2panda/test/unit/annotations/mutiple_annotations_for_class.cpp @@ -43,8 +43,8 @@ public: const std::vector> expectedAnnotations = { { {"favorColor", "1"}, - {"color", "ETSGLOBAL$Anno2$color$0"}, - {"reviewers", "ETSGLOBAL$Anno2$reviewers$1"}, + {"color", "ETSGLOBAL$Anno2$color$8"}, + {"reviewers", "ETSGLOBAL$Anno2$reviewers$9"}, }, }; AnnotationEmitTest::CheckAnnoDecl(program, annoName, expectedAnnotations); @@ -63,14 +63,14 @@ public: {"Anno2", { {"favorColor", "1"}, - {"color", "A$Anno2$color$2"}, - {"reviewers", "A$Anno2$reviewers$3"}, + {"color", "A$Anno2$color$0"}, + {"reviewers", "A$Anno2$reviewers$1"}, }}, {"Anno3", { - {"reviewersAge", "A$Anno3$reviewersAge$4"}, - {"testBools", "A$Anno3$testBools$5"}, - {"mutiArray", "A$Anno3$mutiArray$9"}, + {"reviewersAge", "A$Anno3$reviewersAge$2"}, + {"testBools", "A$Anno3$testBools$3"}, + {"mutiArray", "A$Anno3$mutiArray$7"}, }}, }; AnnotationEmitTest::CheckRecordAnnotations(program, recordName, expectedClassAnnotations); @@ -79,21 +79,21 @@ public: void CheckLiteralArrayTable(pandasm::Program *program) { std::vector>> expectedLiteralArrayTable = { - {"ETSGLOBAL$Anno2$color$0", std::vector {COLOR_0, COLOR_1}}, - {"ETSGLOBAL$Anno2$reviewers$1", + {"ETSGLOBAL$Anno2$color$8", std::vector {COLOR_0, COLOR_1}}, + {"ETSGLOBAL$Anno2$reviewers$9", std::vector {std::string("Bob"), std::string("Jim"), std::string("Tom")}}, - {"A$Anno2$color$2", std::vector {COLOR_0, COLOR_1}}, - {"A$Anno2$reviewers$3", + {"A$Anno2$color$0", std::vector {COLOR_0, COLOR_1}}, + {"A$Anno2$reviewers$1", std::vector {std::string("Bob"), std::string("Jim"), std::string("Tom")}}, - {"A$Anno3$reviewersAge$4", + {"A$Anno3$reviewersAge$2", std::vector {REVIEWER_AGE_19, REVIEWER_AGE_20, REVIEWER_AGE_24}}, - {"A$Anno3$testBools$5", std::vector {true, true, true}}, - {"A$Anno3$mutiArray$6", std::vector {VALUE_9, VALUE_8, VALUE_7}}, - {"A$Anno3$mutiArray$7", std::vector {VALUE_6, VALUE_5, VALUE_4}}, - {"A$Anno3$mutiArray$8", std::vector {VALUE_3, VALUE_2, VALUE_1}}, - {"A$Anno3$mutiArray$9", - std::vector {std::string("A$Anno3$mutiArray$6"), std::string("A$Anno3$mutiArray$7"), - std::string("A$Anno3$mutiArray$8")}}}; + {"A$Anno3$testBools$3", std::vector {true, true, true}}, + {"A$Anno3$mutiArray$4", std::vector {VALUE_9, VALUE_8, VALUE_7}}, + {"A$Anno3$mutiArray$5", std::vector {VALUE_6, VALUE_5, VALUE_4}}, + {"A$Anno3$mutiArray$6", std::vector {VALUE_3, VALUE_2, VALUE_1}}, + {"A$Anno3$mutiArray$7", + std::vector {std::string("A$Anno3$mutiArray$4"), std::string("A$Anno3$mutiArray$5"), + std::string("A$Anno3$mutiArray$6")}}}; AnnotationEmitTest::CheckLiteralArrayTable(program, expectedLiteralArrayTable); } diff --git a/ets2panda/test/unit/annotations/standard_test.cpp b/ets2panda/test/unit/annotations/standard_test.cpp index 8cf5681999..30dd87d241 100644 --- a/ets2panda/test/unit/annotations/standard_test.cpp +++ b/ets2panda/test/unit/annotations/standard_test.cpp @@ -52,11 +52,11 @@ public: {"authorAge", "35.000000"}, {"testBool", "0"}, {"favorColor", "1"}, - {"color", "ETSGLOBAL$ClassAuthor$color$0"}, - {"reviewers", "ETSGLOBAL$ClassAuthor$reviewers$1"}, - {"reviewersAge", "ETSGLOBAL$ClassAuthor$reviewersAge$2"}, - {"testBools", "ETSGLOBAL$ClassAuthor$testBools$3"}, - {"mutiArray", "ETSGLOBAL$ClassAuthor$mutiArray$7"}, + {"color", "ETSGLOBAL$ClassAuthor$color$8"}, + {"reviewers", "ETSGLOBAL$ClassAuthor$reviewers$9"}, + {"reviewersAge", "ETSGLOBAL$ClassAuthor$reviewersAge$10"}, + {"testBools", "ETSGLOBAL$ClassAuthor$testBools$11"}, + {"mutiArray", "ETSGLOBAL$ClassAuthor$mutiArray$15"}, }; AnnotationEmitTest::CheckAnnoDecl(program, annoName, expectedAnnotations); } @@ -71,11 +71,11 @@ public: {"authorAge", "35.000000"}, {"testBool", "0"}, {"favorColor", "1"}, - {"color", "MyClass$ClassAuthor$color$8"}, - {"reviewers", "MyClass$ClassAuthor$reviewers$9"}, - {"reviewersAge", "MyClass$ClassAuthor$reviewersAge$10"}, - {"testBools", "MyClass$ClassAuthor$testBools$11"}, - {"mutiArray", "MyClass$ClassAuthor$mutiArray$15"}, + {"color", "MyClass$ClassAuthor$color$0"}, + {"reviewers", "MyClass$ClassAuthor$reviewers$1"}, + {"reviewersAge", "MyClass$ClassAuthor$reviewersAge$2"}, + {"testBools", "MyClass$ClassAuthor$testBools$3"}, + {"mutiArray", "MyClass$ClassAuthor$mutiArray$7"}, }}, }; AnnotationEmitTest::CheckRecordAnnotations(program, recordName, expectedClassAnnotations); @@ -104,30 +104,30 @@ public: void CheckLiteralArrayTable(pandasm::Program *program) { std::vector>> expectedLiteralArrayTable = { - {"ETSGLOBAL$ClassAuthor$color$0", std::vector {COLOR_OPTION_0, COLOR_OPTION_1}}, - {"ETSGLOBAL$ClassAuthor$reviewers$1", + {"ETSGLOBAL$ClassAuthor$color$8", std::vector {COLOR_OPTION_0, COLOR_OPTION_1}}, + {"ETSGLOBAL$ClassAuthor$reviewers$9", std::vector {std::string("Bob"), std::string("Jim"), std::string("Tom")}}, - {"ETSGLOBAL$ClassAuthor$reviewersAge$2", std::vector {AGE_18, AGE_21, AGE_32}}, - {"ETSGLOBAL$ClassAuthor$testBools$3", std::vector {false, true, false}}, - {"ETSGLOBAL$ClassAuthor$mutiArray$4", std::vector {VALUE_1, VALUE_2, VALUE_3}}, - {"ETSGLOBAL$ClassAuthor$mutiArray$5", std::vector {VALUE_4, VALUE_5, VALUE_6}}, - {"ETSGLOBAL$ClassAuthor$mutiArray$6", std::vector {VALUE_7, VALUE_8, VALUE_9}}, - {"ETSGLOBAL$ClassAuthor$mutiArray$7", - std::vector {std::string("ETSGLOBAL$ClassAuthor$mutiArray$4"), - std::string("ETSGLOBAL$ClassAuthor$mutiArray$5"), - std::string("ETSGLOBAL$ClassAuthor$mutiArray$6")}}, - {"MyClass$ClassAuthor$color$8", std::vector {COLOR_OPTION_0, COLOR_OPTION_1}}, - {"MyClass$ClassAuthor$reviewers$9", + {"ETSGLOBAL$ClassAuthor$reviewersAge$10", std::vector {AGE_18, AGE_21, AGE_32}}, + {"ETSGLOBAL$ClassAuthor$testBools$11", std::vector {false, true, false}}, + {"ETSGLOBAL$ClassAuthor$mutiArray$12", std::vector {VALUE_1, VALUE_2, VALUE_3}}, + {"ETSGLOBAL$ClassAuthor$mutiArray$13", std::vector {VALUE_4, VALUE_5, VALUE_6}}, + {"ETSGLOBAL$ClassAuthor$mutiArray$14", std::vector {VALUE_7, VALUE_8, VALUE_9}}, + {"ETSGLOBAL$ClassAuthor$mutiArray$15", + std::vector {std::string("ETSGLOBAL$ClassAuthor$mutiArray$12"), + std::string("ETSGLOBAL$ClassAuthor$mutiArray$13"), + std::string("ETSGLOBAL$ClassAuthor$mutiArray$14")}}, + {"MyClass$ClassAuthor$color$0", std::vector {COLOR_OPTION_0, COLOR_OPTION_1}}, + {"MyClass$ClassAuthor$reviewers$1", std::vector {std::string("Bob"), std::string("Jim"), std::string("Tom")}}, - {"MyClass$ClassAuthor$reviewersAge$10", std::vector {AGE_18, AGE_21, AGE_32}}, - {"MyClass$ClassAuthor$testBools$11", std::vector {false, true, false}}, - {"MyClass$ClassAuthor$mutiArray$12", std::vector {VALUE_1, VALUE_2, VALUE_3}}, - {"MyClass$ClassAuthor$mutiArray$13", std::vector {VALUE_4, VALUE_5, VALUE_6}}, - {"MyClass$ClassAuthor$mutiArray$14", std::vector {VALUE_7, VALUE_8, VALUE_9}}, - {"MyClass$ClassAuthor$mutiArray$15", - std::vector {std::string("MyClass$ClassAuthor$mutiArray$12"), - std::string("MyClass$ClassAuthor$mutiArray$13"), - std::string("MyClass$ClassAuthor$mutiArray$14")}}, + {"MyClass$ClassAuthor$reviewersAge$2", std::vector {AGE_18, AGE_21, AGE_32}}, + {"MyClass$ClassAuthor$testBools$3", std::vector {false, true, false}}, + {"MyClass$ClassAuthor$mutiArray$4", std::vector {VALUE_1, VALUE_2, VALUE_3}}, + {"MyClass$ClassAuthor$mutiArray$5", std::vector {VALUE_4, VALUE_5, VALUE_6}}, + {"MyClass$ClassAuthor$mutiArray$6", std::vector {VALUE_7, VALUE_8, VALUE_9}}, + {"MyClass$ClassAuthor$mutiArray$7", + std::vector {std::string("MyClass$ClassAuthor$mutiArray$4"), + std::string("MyClass$ClassAuthor$mutiArray$5"), + std::string("MyClass$ClassAuthor$mutiArray$6")}}, {"MyClass.foo:void;$ClassAuthor$color$16", std::vector {COLOR_OPTION_0, COLOR_OPTION_1}}, {"MyClass.foo:void;$ClassAuthor$reviewers$17", diff --git a/ets2panda/test/unit/union_normalisation_test.h b/ets2panda/test/unit/union_normalisation_test.h index 36b345f6c3..ed71e675b0 100644 --- a/ets2panda/test/unit/union_normalisation_test.h +++ b/ets2panda/test/unit/union_normalisation_test.h @@ -131,14 +131,14 @@ public: static checker::Type *FindClassType(varbinder::ETSBinder *varbinder, std::string_view className) { - auto classDefs = varbinder->AsETSBinder()->GetRecordTable()->ClassDefinitions(); - auto baseClass = std::find_if(classDefs.begin(), classDefs.end(), [className](ir::ClassDefinition *cdef) { - return cdef->Ident()->Name().Is(className); - }); - if (baseClass == classDefs.end()) { - return nullptr; - } - return (*baseClass)->TsType(); + ark::es2panda::ir::ClassDefinition *baseClass = nullptr; + auto classDeclHandler = [&baseClass, className](const ark::es2panda::ir::ClassDefinition *classDecl) { + if (baseClass == nullptr && classDecl->Ident()->Name().Is(className)) { + baseClass = const_cast(classDecl); + } + }; + varbinder->AsETSBinder()->GetRecordTable()->Visit(classDeclHandler); + return baseClass == nullptr ? nullptr : baseClass->TsType(); } static checker::Type *FindTypeAlias(checker::ETSChecker *checker, std::string_view aliasName) diff --git a/ets2panda/test/utils/checker_test.cpp b/ets2panda/test/utils/checker_test.cpp index d4a23dd699..194f04a8f1 100644 --- a/ets2panda/test/utils/checker_test.cpp +++ b/ets2panda/test/utils/checker_test.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,14 +20,14 @@ namespace test::utils { checker_alias::Type *CheckerTest::FindClassType(varbinder_alias::ETSBinder *varbinder, std::string_view className) { - auto classDefs = varbinder->AsETSBinder()->GetRecordTable()->ClassDefinitions(); - auto baseClass = std::find_if(classDefs.begin(), classDefs.end(), [className](ir_alias::ClassDefinition *cdef) { - return cdef->Ident()->Name().Is(className); - }); - if (baseClass == classDefs.end()) { - return nullptr; - } - return (*baseClass)->TsType(); + ark::es2panda::ir::ClassDefinition *baseClass = nullptr; + auto classDeclHandler = [&baseClass, className](const ark::es2panda::ir::ClassDefinition *classDecl) { + if (baseClass == nullptr && classDecl->Ident()->Name().Is(className)) { + baseClass = const_cast(classDecl); + } + }; + varbinder->AsETSBinder()->GetRecordTable()->Visit(classDeclHandler); + return baseClass == nullptr ? nullptr : baseClass->TsType(); } checker_alias::Type *CheckerTest::FindTypeAlias(checker_alias::ETSChecker *checker, std::string_view aliasName) diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index f2983e06ca..7cb6c06394 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -1250,8 +1250,6 @@ void ETSBinder::BuildExternalProgram(parser::Program *extProgram) if (!extProgram->IsASTLowered()) { BuildProgram(); - } else { - extRecordTable->Merge(extProgram->VarBinder()->AsETSBinder()->GetExternalRecordTable().at(extProgram)); } SetProgram(savedProgram); diff --git a/ets2panda/varbinder/recordTable.cpp b/ets2panda/varbinder/recordTable.cpp index 6971fd4da3..1f8ab046ed 100644 --- a/ets2panda/varbinder/recordTable.cpp +++ b/ets2panda/varbinder/recordTable.cpp @@ -25,6 +25,52 @@ #include "generated/signatures.h" namespace ark::es2panda::varbinder { +void RecordTable::Visit(const std::function &handler) const +{ + std::for_each(classDefinitions_.begin(), classDefinitions_.end(), [&handler](auto &decl) { handler(decl); }); + auto extRecordTable = program_->VarBinder()->AsETSBinder()->GetExternalRecordTable(); + if (extRecordTable.find(program_) == extRecordTable.end()) { + return; + } + auto decls = extRecordTable.at(program_)->classDefinitions_; + std::for_each(decls.begin(), decls.end(), [&handler](auto &decl) { handler(decl); }); +} + +void RecordTable::Visit(const std::function &handler) const +{ + std::for_each(interfaceDeclarations_.begin(), interfaceDeclarations_.end(), + [&handler](auto &decl) { handler(decl); }); + auto extRecordTable = program_->VarBinder()->AsETSBinder()->GetExternalRecordTable(); + if (extRecordTable.find(program_) == extRecordTable.end()) { + return; + } + auto decls = extRecordTable.at(program_)->interfaceDeclarations_; + std::for_each(decls.begin(), decls.end(), [&handler](auto &decl) { handler(decl); }); +} + +void RecordTable::Visit(const std::function &handler) const +{ + std::for_each(annotationDeclarations_.begin(), annotationDeclarations_.end(), + [&handler](auto &decl) { handler(decl); }); + auto extRecordTable = program_->VarBinder()->AsETSBinder()->GetExternalRecordTable(); + if (extRecordTable.find(program_) == extRecordTable.end()) { + return; + } + auto decls = extRecordTable.at(program_)->annotationDeclarations_; + std::for_each(decls.begin(), decls.end(), [&handler](auto &decl) { handler(decl); }); +} + +void RecordTable::Visit(const std::function &handler) const +{ + std::for_each(signatures_.begin(), signatures_.end(), [&handler](auto &signature) { handler(signature); }); + auto extRecordTable = program_->VarBinder()->AsETSBinder()->GetExternalRecordTable(); + if (extRecordTable.find(program_) == extRecordTable.end()) { + return; + } + auto signatures = extRecordTable.at(program_)->signatures_; + std::for_each(signatures.begin(), signatures.end(), [&handler](auto &signature) { handler(signature); }); +} + BoundContext::BoundContext(RecordTable *recordTable, ir::ClassDefinition *classDef, bool force) : prev_(recordTable->boundCtx_), recordTable_(recordTable), diff --git a/ets2panda/varbinder/recordTable.h b/ets2panda/varbinder/recordTable.h index 437a2ffb79..813b79cb34 100644 --- a/ets2panda/varbinder/recordTable.h +++ b/ets2panda/varbinder/recordTable.h @@ -74,29 +74,18 @@ public: ~RecordTable() = default; - void Merge(RecordTable *other) + bool IsExternal() const { - for (auto classDef : other->classDefinitions_) { - classDefinitions_.insert(classDef); - } + return (flags_ & RecordTableFlags::EXTERNAL) != 0; + } - for (auto interfaceDecl : other->InterfaceDeclarations()) { - interfaceDeclarations_.insert(interfaceDecl); - } + void Visit(const std::function &handler) const; - for (auto annoDecl : other->AnnotationDeclarations()) { - annotationDeclarations_.insert(annoDecl); - } + void Visit(const std::function &handler) const; - for (auto sig : other->signatures_) { - signatures_.insert(sig); - } - } + void Visit(const std::function &handler) const; - bool IsExternal() const - { - return (flags_ & RecordTableFlags::EXTERNAL) != 0; - } + void Visit(const std::function &handler) const; ArenaSet &ClassDefinitions() { -- Gitee From d38988070e1c3ad4c51ca3cf5b54526aef91822b Mon Sep 17 00:00:00 2001 From: zhaiyukun Date: Tue, 10 Jun 2025 01:51:10 +0800 Subject: [PATCH 2/2] [Memory] Avoid redundant historical nodes Signed-off-by: zhaiyukun --- ets2panda/ir/astNode.h | 9 +++++++-- ets2panda/ir/typed.h | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index 5489ceb5f6..f624a1b8f5 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -344,9 +344,14 @@ public: void SetVariable(varbinder::Variable *variable) noexcept { - if (GetHistoryNode()->variable_ != variable) { - GetOrCreateHistoryNode()->variable_ = variable; + auto nowNode = GetHistoryNode(); + if (nowNode->variable_ == variable) { + return; } + if (nowNode->variable_ != nullptr) { + nowNode = GetOrCreateHistoryNode(); + } + nowNode->variable_ = variable; } // When no decorators are allowed, we cannot return a reference to an empty vector. diff --git a/ets2panda/ir/typed.h b/ets2panda/ir/typed.h index cec0ce52c3..e40c362238 100644 --- a/ets2panda/ir/typed.h +++ b/ets2panda/ir/typed.h @@ -49,9 +49,13 @@ public: checker::Type *SetTsType(checker::Type *tsType) noexcept { auto nowNode = AstNode::GetHistoryNodeAs>(); - if (nowNode->tsType_ != tsType) { - AstNode::GetOrCreateHistoryNodeAs>()->tsType_ = tsType; + if (nowNode->tsType_ == tsType) { + return tsType; } + if (nowNode->tsType_ != nullptr) { + nowNode = AstNode::GetOrCreateHistoryNodeAs>(); + } + nowNode->tsType_ = tsType; return tsType; } -- Gitee