diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index a6e82613d376b4456258bb5a6fe5e4a2fba3afda..aa3c20310770810705b2873412f687bd7f7e8e5d 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -141,18 +141,17 @@ int Run(int argc, const char **argv) return 1; } - std::vector programs; - std::unordered_map programsInfo; + std::map programsInfo; size_t expectedProgsCount = options->CompilerOptions().sourceFiles.size(); panda::ArenaAllocator allocator(panda::SpaceType::SPACE_TYPE_COMPILER, nullptr, true); - std::unordered_map *cachePrograms = nullptr; + std::map *cachePrograms = nullptr; if (!options->CacheFile().empty()) { cachePrograms = proto::ProtobufSnapshotGenerator::GetCacheContext(options->CacheFile(), options->CompilerOptions().isDebug, &allocator); } - Compiler::CompileFiles(options->CompilerOptions(), cachePrograms, programs, programsInfo, &allocator); + Compiler::CompileFiles(options->CompilerOptions(), cachePrograms, programsInfo, &allocator); if (options->ParseOnly()) { return 0; @@ -160,7 +159,7 @@ int Run(int argc, const char **argv) if (!options->NpmModuleEntryList().empty()) { es2panda::util::ModuleHelpers::CompileNpmModuleEntryList(options->NpmModuleEntryList(), cachePrograms, - programs, programsInfo, &allocator); + programsInfo, &allocator); expectedProgsCount++; } @@ -169,11 +168,19 @@ int Run(int argc, const char **argv) options->CacheFile()); } + std::vector programs; + programs.reserve(programsInfo.size()); + for (auto &it : programsInfo) { + programs.emplace_back(it.second->program); + } if (programs.size() != expectedProgsCount) { + std::cerr << "the size of programs is expected to be " << expectedProgsCount + << ", but is " << programs.size() << std::endl; return 1; } if (!GenerateProgram(programs, options)) { + std::cerr << "GenerateProgram Failed!" << std::endl; return 1; } diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index 5fb4c8847d8543626245917bf2f3f7a23be5f2f4..f8709aa36dccf91705432f5920f71668ccd9f809 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -122,7 +122,6 @@ void CompileFileJob::Run() std::unique_lock lock(global_m_); auto *cache = allocator_->New(src_->hash, prog); progsInfo_.insert({src_->fileName, cache}); - progs_.push_back(prog); } } @@ -238,7 +237,7 @@ void CompileFileQueue::Schedule() std::unique_lock lock(m_); for (auto &input: options_->sourceFiles) { - auto *fileJob = new CompileFileJob(&input, options_, progs_, progsInfo_, symbolTable_, allocator_); + auto *fileJob = new CompileFileJob(&input, options_, progsInfo_, symbolTable_, allocator_); jobs_.push_back(fileJob); jobsCount_++; } diff --git a/es2panda/compiler/core/compileQueue.h b/es2panda/compiler/core/compileQueue.h index b42737d9256c51cf9be92c6c012a831224936c45..17392fe4a0409a48bc24a12994872b7ce6320a29 100644 --- a/es2panda/compiler/core/compileQueue.h +++ b/es2panda/compiler/core/compileQueue.h @@ -90,11 +90,9 @@ private: class CompileFileJob : public CompileJob { public: explicit CompileFileJob(es2panda::SourceFile *src, es2panda::CompilerOptions *options, - std::vector &progs, - std::unordered_map &progsInfo, + std::map &progsInfo, util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator) - : src_(src), options_(options), progs_(progs), progsInfo_(progsInfo), symbolTable_(symbolTable), - allocator_(allocator) {}; + : src_(src), options_(options), progsInfo_(progsInfo), symbolTable_(symbolTable), allocator_(allocator) {}; NO_COPY_SEMANTIC(CompileFileJob); NO_MOVE_SEMANTIC(CompileFileJob); ~CompileFileJob() = default; @@ -105,8 +103,7 @@ private: static std::mutex global_m_; es2panda::SourceFile *src_; es2panda::CompilerOptions *options_; - std::vector &progs_; - std::unordered_map &progsInfo_; + std::map &progsInfo_; util::SymbolTable *symbolTable_; panda::ArenaAllocator *allocator_; }; @@ -154,10 +151,9 @@ private: class CompileFileQueue : public CompileQueue { public: explicit CompileFileQueue(size_t threadCount, es2panda::CompilerOptions *options, - std::vector &progs, - std::unordered_map &progsInfo, + std::map &progsInfo, util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator) - : CompileQueue(threadCount), options_(options), progs_(progs), progsInfo_(progsInfo), + : CompileQueue(threadCount), options_(options), progsInfo_(progsInfo), symbolTable_(symbolTable), allocator_(allocator) {} NO_COPY_SEMANTIC(CompileFileQueue); @@ -168,8 +164,7 @@ public: private: es2panda::CompilerOptions *options_; - std::vector &progs_; - std::unordered_map &progsInfo_; + std::map &progsInfo_; util::SymbolTable *symbolTable_; panda::ArenaAllocator *allocator_; }; diff --git a/es2panda/compiler/core/emitter/emitter.cpp b/es2panda/compiler/core/emitter/emitter.cpp index a4cacd0b1e78cf28a9dc01c266ee558f778da64a..53064fdc1dea20e7c450060e75a09504cadcdc15 100644 --- a/es2panda/compiler/core/emitter/emitter.cpp +++ b/es2panda/compiler/core/emitter/emitter.cpp @@ -341,7 +341,6 @@ Emitter::Emitter(const CompilerContext *context) { prog_ = new panda::pandasm::Program(); prog_->lang = LANG_EXT; - prog_->function_table.reserve(context->Binder()->Functions().size()); if (context->IsMergeAbc()) { auto recordName = context->Binder()->Program()->RecordName().Mutf8(); diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 002a1ddf2e4b04a3847a6b6823bfa6536e9c08df..fcb7f8208afd65955f971e5d9386169a6c5e952e 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -107,9 +107,8 @@ static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss) } void Compiler::SelectCompileFile(CompilerOptions &options, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator) { if (cacheProgs == nullptr) { @@ -135,7 +134,6 @@ void Compiler::SelectCompileFile(CompilerOptions &options, auto it = cacheProgs->find(input.fileName); if (it != cacheProgs->end() && hash == it->second->hashCode) { - progs.push_back(it->second->program); auto *cache = allocator->New(it->second->hashCode, it->second->program); progsInfo.insert({input.fileName, cache}); } else { @@ -147,9 +145,8 @@ void Compiler::SelectCompileFile(CompilerOptions &options, } void Compiler::CompileFiles(CompilerOptions &options, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator) { util::SymbolTable *symbolTable = nullptr; @@ -161,10 +158,9 @@ void Compiler::CompileFiles(CompilerOptions &options, } } - SelectCompileFile(options, cacheProgs, progs, progsInfo, allocator); + SelectCompileFile(options, cacheProgs, progsInfo, allocator); - auto queue = new compiler::CompileFileQueue(options.fileThreadCount, &options, progs, progsInfo, symbolTable, - allocator); + auto queue = new compiler::CompileFileQueue(options.fileThreadCount, &options, progsInfo, symbolTable, allocator); queue->Schedule(); queue->Consume(); @@ -180,7 +176,7 @@ void Compiler::CompileFiles(CompilerOptions &options, } panda::pandasm::Program *Compiler::CompileFile(CompilerOptions &options, SourceFile *src, - util::SymbolTable *symbolTable) + util::SymbolTable *symbolTable) { std::string buffer; if (src->source.empty()) { diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index ab10b5ca07c1996b55528413b9f7d06404eb5148..dc0e9480ee368666994e677414916114dc90fe07 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -166,15 +166,13 @@ public: panda::pandasm::Program *CompileFile(CompilerOptions &options, SourceFile *src, util::SymbolTable *symbolTable_); static void CompileFiles(CompilerOptions &options, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator); static void SelectCompileFile(CompilerOptions &options, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator); inline panda::pandasm::Program *Compile(const SourceFile &input) diff --git a/es2panda/util/moduleHelpers.cpp b/es2panda/util/moduleHelpers.cpp index 95ee9d64f38c0b46b04a4f6445918e34b720ad7b..53841408ee5b52f8fb806231a9711a610d3aac85 100644 --- a/es2panda/util/moduleHelpers.cpp +++ b/es2panda/util/moduleHelpers.cpp @@ -19,9 +19,8 @@ namespace panda::es2panda::util { void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator) { std::stringstream ss; @@ -37,7 +36,6 @@ void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo, if (cacheProgs != nullptr) { auto it = cacheProgs->find(entriesInfo); if (it != cacheProgs->end() && hash == it->second->hashCode) { - progs.push_back(it->second->program); auto *cache = allocator->New(it->second->hashCode, it->second->program); progsInfo.insert({entriesInfo, cache}); return; @@ -63,7 +61,6 @@ void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo, prog->record_table.emplace(recordName, std::move(*entryRecord)); } - progs.push_back(prog); auto *cache = allocator->New(hash, prog); progsInfo.insert({entriesInfo, cache}); } diff --git a/es2panda/util/moduleHelpers.h b/es2panda/util/moduleHelpers.h index 5d4035e7b146f0d1d78e53dac3c95ea2bbedbe99..3c0a68e918ec54d13db3585b19bccbc0e6c0bb1d 100644 --- a/es2panda/util/moduleHelpers.h +++ b/es2panda/util/moduleHelpers.h @@ -24,9 +24,8 @@ namespace panda::es2panda::util { class ModuleHelpers { public: static void CompileNpmModuleEntryList(const std::string &entriesInfo, - std::unordered_map *cacheProgs, - std::vector &progs, - std::unordered_map &progsInfo, + std::map *cacheProgs, + std::map &progsInfo, panda::ArenaAllocator *allocator); }; } // panda::es2panda::util diff --git a/merge_abc/src/assemblyProgramProto.cpp b/merge_abc/src/assemblyProgramProto.cpp index 8e8f4ad0806291696232d20e9e889befe8aa5e82..f5240111e028f2369b5e47f2061930de65c4d3b5 100644 --- a/merge_abc/src/assemblyProgramProto.cpp +++ b/merge_abc/src/assemblyProgramProto.cpp @@ -54,7 +54,6 @@ void Program::Deserialize(const protoPanda::Program &protoProgram, panda::pandas { program.lang = static_cast(protoProgram.lang()); - program.record_table.reserve(protoProgram.recordtable_size()); for (const auto &recordUnit : protoProgram.recordtable()) { auto &name = recordUnit.key(); auto &protoRecord = recordUnit.value(); @@ -64,7 +63,6 @@ void Program::Deserialize(const protoPanda::Program &protoProgram, panda::pandas program.record_table.insert({name, std::move(record)}); } - program.function_table.reserve(protoProgram.functiontable_size()); for (const auto &functionUnit : protoProgram.functiontable()) { auto &name = functionUnit.key(); auto &protoFunction = functionUnit.value(); @@ -82,12 +80,10 @@ void Program::Deserialize(const protoPanda::Program &protoProgram, panda::pandas program.literalarray_table.insert({name, std::move(literalArray)}); } - program.strings.reserve(protoProgram.strings_size()); for (const auto &protoString : protoProgram.strings()) { program.strings.insert(protoString); } - program.array_types.reserve(protoProgram.arraytypes_size()); for (const auto &protoArrayType : protoProgram.arraytypes()) { auto &arrayType = Type::Deserialize(protoArrayType, allocator); program.array_types.insert(std::move(arrayType)); diff --git a/merge_abc/src/compositeProgramProto.cpp b/merge_abc/src/compositeProgramProto.cpp index 2fd536387ce7aa4121668216aa7a5e5bb2e467a0..7cfb1c2590ac825a1a6109068deb1991aee9d690 100644 --- a/merge_abc/src/compositeProgramProto.cpp +++ b/merge_abc/src/compositeProgramProto.cpp @@ -16,9 +16,8 @@ #include "compositeProgramProto.h" namespace panda::proto { -void CompositeProgram::Serialize( - const std::unordered_map &compositeProgramMap, bool isDebug, - protoPanda::CompositeProgram &protoCompositeProgram) +void CompositeProgram::Serialize(const std::map &compositeProgramMap, + bool isDebug, protoPanda::CompositeProgram &protoCompositeProgram) { for (const auto &[fileName, programCache] : compositeProgramMap) { auto *protoProgramcache = protoCompositeProgram.add_programcache(); @@ -31,10 +30,9 @@ void CompositeProgram::Serialize( } void CompositeProgram::Deserialize(const protoPanda::CompositeProgram &protoCompositeProgram, - std::unordered_map &compositeProgramMap, - panda::ArenaAllocator *allocator) + std::map &compositeProgramMap, + panda::ArenaAllocator *allocator) { - compositeProgramMap.reserve(protoCompositeProgram.programcache_size()); for (const auto &protoProgramcache : protoCompositeProgram.programcache()) { auto &fileName = protoProgramcache.filename(); auto hashCode = protoProgramcache.hashcode(); diff --git a/merge_abc/src/compositeProgramProto.h b/merge_abc/src/compositeProgramProto.h index 8f72c554722753c6138419a99eb51ea7cc20fba8..3291a666a1acb16e4810f9672712b24ce8dcc877 100644 --- a/merge_abc/src/compositeProgramProto.h +++ b/merge_abc/src/compositeProgramProto.h @@ -23,12 +23,11 @@ namespace panda::proto { class CompositeProgram { public: - static void Serialize( - const std::unordered_map &compositeProgramMap, bool isDebug, - protoPanda::CompositeProgram &protoCompositeProgram); + static void Serialize(const std::map &compositeProgramMap, + bool isDebug, protoPanda::CompositeProgram &protoCompositeProgram); static void Deserialize(const protoPanda::CompositeProgram &protoCompositeProgram, - std::unordered_map &compositeProgramMap, - panda::ArenaAllocator *allocator); + std::map &compositeProgramMap, + panda::ArenaAllocator *allocator); }; } // panda::proto #endif diff --git a/merge_abc/src/protobufSnapshotGenerator.cpp b/merge_abc/src/protobufSnapshotGenerator.cpp index 7b0af28c2be8ebe85ff4275dfb9253c81e8cc1d3..112e88f4e149d944defe6e60da3cbc6a6a7fd7b3 100644 --- a/merge_abc/src/protobufSnapshotGenerator.cpp +++ b/merge_abc/src/protobufSnapshotGenerator.cpp @@ -50,7 +50,7 @@ void ProtobufSnapshotGenerator::GenerateProgram(const std::string &inputName, pa } void ProtobufSnapshotGenerator::UpdateCacheFile( - const std::unordered_map &compositeProgramMap, + const std::map &compositeProgramMap, bool &isDebug, const std::string &cacheFilePath) { protoPanda::CompositeProgram protoCompositeProgram; @@ -64,7 +64,7 @@ void ProtobufSnapshotGenerator::UpdateCacheFile( output.close(); } -std::unordered_map *ProtobufSnapshotGenerator::GetCacheContext( +std::map *ProtobufSnapshotGenerator::GetCacheContext( const std::string &cacheFilePath, bool isDebug, panda::ArenaAllocator *allocator) { std::fstream input(cacheFilePath, std::ios::in | std::ios::binary); @@ -82,7 +82,7 @@ std::unordered_map *ProtobufS return nullptr; } - auto compositeProgramMap = allocator->New>(); + auto compositeProgramMap = allocator->New>(); CompositeProgram::Deserialize(protoCompositeProgram, *compositeProgramMap, allocator); return compositeProgramMap; diff --git a/merge_abc/src/protobufSnapshotGenerator.h b/merge_abc/src/protobufSnapshotGenerator.h index 20bb7118e9b57c20531194bfc7de00a273cd070b..f1da5d27c9be89bf7e3eb6e08f01fa394d9455bd 100644 --- a/merge_abc/src/protobufSnapshotGenerator.h +++ b/merge_abc/src/protobufSnapshotGenerator.h @@ -25,11 +25,10 @@ public: static void GenerateSnapshot(const panda::pandasm::Program &prog, const std::string &outputName); static void GenerateProgram(const std::string &inputName, panda::pandasm::Program &prog, panda::ArenaAllocator *allocator); - static std::unordered_map *GetCacheContext( + static std::map *GetCacheContext( const std::string &cacheFilePath, bool isDebug, panda::ArenaAllocator *allocator); - static void UpdateCacheFile( - const std::unordered_map &compositeProgram, - bool &isDebug, const std::string &cacheFilePath); + static void UpdateCacheFile(const std::map &compositeProgram, + bool &isDebug, const std::string &cacheFilePath); }; } // panda::proto #endif