diff --git a/ets2panda/es2panda.cpp b/ets2panda/es2panda.cpp index b2b63e0635fceb402425dc6bd9e747c1ccb65e5b..2dedf2cca1ffa6da7833635160b6d1b559ce46e1 100644 --- a/ets2panda/es2panda.cpp +++ b/ets2panda/es2panda.cpp @@ -20,6 +20,9 @@ #include "util/generateBin.h" #include "es2panda.h" +#include +#include +#include namespace ark::es2panda { constexpr size_t DEFAULT_THREAD_COUNT = 2; @@ -76,10 +79,13 @@ Compiler::Compiler(ScriptExtension ext, size_t threadCount) : Compiler(ext, thre Compiler::Compiler(ScriptExtension ext, size_t threadCount, std::vector &&plugins) : plugins_(std::move(plugins)), compiler_(new compiler::CompilerImpl(threadCount, &plugins_)), ext_(ext) { + // 启动后台内存预分配线程(非阻塞) + StartBackgroundMemoryPreallocation(); } Compiler::~Compiler() { + // 后台预分配线程已经detach,会自动清理,不需要等待 delete compiler_; } @@ -138,6 +144,48 @@ void Compiler::DumpAsm(const pandasm::Program *prog) compiler::CompilerImpl::DumpAsm(prog); } +void Compiler::StartBackgroundMemoryPreallocation() +{ + // 避免重复启动 + if (preallocationStarted_.exchange(true)) { + return; + } + + // 创建后台预分配线程 + backgroundPreallocationThread_ = std::make_unique([]() { + std::cout << "[Compiler] Starting background memory preallocation..." << std::endl; + + // 创建 ArenaAllocator,使用编译器空间类型 + ThreadSafeArenaAllocator allocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true); + + // 预分配 2GB 内存 + const size_t memorySize = 2ULL * 1024 * 1024 * 1024; // 2GB + void* memory = allocator.Alloc(memorySize); + + if (memory != nullptr) { + // 访问内存以确保页面被映射 + std::memset(memory, 0, memorySize); + + // 等待一段时间让页面稳定 + //std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + // 释放内存 + allocator.Resize(0); + + std::cout << "[Compiler] Background memory preallocation completed successfully" << std::endl; + } else { + std::cout << "[Compiler] Background memory preallocation failed" << std::endl; + } + + std::cout << "[Compiler] Background memory preallocation thread finished" << std::endl; + }); + + // 将线程设置为分离状态,这样主线程不需要等待 + backgroundPreallocationThread_->detach(); + + std::cout << "[Compiler] Background memory preallocation thread started (non-blocking)" << std::endl; +} + // When compiling multi thread, this is need by each thread indenpengdentlt thread_local util::DiagnosticEngine *g_diagnosticEngine = nullptr; } // namespace ark::es2panda diff --git a/ets2panda/es2panda.h b/ets2panda/es2panda.h index 1f5a0aafc4ab0388f7a7066add558876f47c1c25..fd47c4b1078356daeca2915a48169a41fb4a58dd 100644 --- a/ets2panda/es2panda.h +++ b/ets2panda/es2panda.h @@ -21,6 +21,9 @@ #include "util/diagnostic.h" #include "generated/options.h" #include "util/language.h" +#include +#include +#include namespace ark::es2panda::ir { class AstNode; @@ -133,10 +136,19 @@ public: } private: + /** + * @brief 启动后台内存预分配线程(非阻塞) + */ + void StartBackgroundMemoryPreallocation(); + std::vector const plugins_ {}; compiler::CompilerImpl *compiler_ {}; util::ThrowableDiagnostic error_ {}; ScriptExtension ext_ {}; + + // 后台内存预分配相关成员 + std::unique_ptr backgroundPreallocationThread_; + std::atomic preallocationStarted_{false}; }; // g_diagnosticEngine used only for flush diagnostic before unexpected process termination: