From 8ff6ae71d49df650f7afd74e87d80efe31a35b65 Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Tue, 1 Dec 2026 23:33:48 +0300 Subject: [PATCH] src: create compile cache when the process exits Refs: https://github.com/nodejs/node/issues/52696 Assisted-by: Claude Opus 5.5 Signed-off-by: Mert Can Altin --- src/compile_cache.cc | 75 ++++++++++++++++++++++++++++++++++++++++++-- src/compile_cache.h | 14 +++++++-- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/compile_cache.cc b/src/compile_cache.cc index 90ed63f448ae..638353c18908 100644 --- a/src/compile_cache.cc +++ b/src/compile_cache.cc @@ -1,5 +1,6 @@ #include "compile_cache.h" #include +#include #include "debug_utils-inl.h" #include "env-inl.h" #include "node_file.h" @@ -19,10 +20,12 @@ namespace node { using v8::Function; +using v8::HandleScope; using v8::Local; using v8::Module; using v8::ScriptCompiler; using v8::String; +using v8::UnboundModuleScript; using v8::WasmModuleObject; namespace { @@ -314,6 +317,10 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(const uint8_t* code, compile_cache_dir_ + kPathSeparator + Uint32ToHex(key); result->source_filename = filename_utf8.ToString(); result->cache = nullptr; + result->refreshed = false; + result->persisted = false; + result->pending_function.Reset(); + result->pending_module.Reset(); result->type = type; // TODO(joyeecheung): if we fail enough times, stop trying for any future @@ -331,6 +338,10 @@ ScriptCompiler::CachedData* SerializeCodeCache(Local mod) { return ScriptCompiler::CreateCodeCache(mod->GetUnboundModuleScript()); } +ScriptCompiler::CachedData* SerializeCodeCache(Local s) { + return ScriptCompiler::CreateCodeCache(s); +} + // V8 only serializes code compiled by the optimizing tier, and produces // nothing if there is none, e.g. for a module that has only been compiled // with the baseline tier so far. @@ -377,17 +388,73 @@ void CompileCacheHandler::MaybeSaveImpl(CompileCacheEntry* entry, entry->cache.reset(data); } +// Instead of serializing the code cache right after compilation, keep the +// compiled script alive and serialize it in Persist(), after the module has +// run, so that functions compiled lazily during execution are included. +template +void CompileCacheHandler::DeferSave(CompileCacheEntry* entry, + Local func_or_mod, + bool rejected) { + DCHECK_NOT_NULL(entry); + const bool accepted = entry->cache != nullptr && !rejected; + if (read_only_ || accepted) { + MaybeSaveImpl(entry, func_or_mod, rejected); + return; + } + Debug("[compile cache] V8 code cache for %s %s was %s, initializing the " + "in-memory entry at exit\n", + entry->type_name(), + entry->source_filename, + rejected ? "rejected" : "not initialized"); + // A rejected cache must not be handed to V8 again. + entry->cache.reset(); + if constexpr (std::is_same_v) { + entry->pending_module.Reset(isolate_, + func_or_mod->GetUnboundModuleScript()); + } else { + entry->pending_function.Reset(isolate_, func_or_mod); + } +} + +void CompileCacheHandler::SerializePending() { + for (auto& pair : compiler_cache_store_) { + auto* entry = pair.second.get(); + if (entry->pending_function.IsEmpty() && entry->pending_module.IsEmpty()) { + continue; + } + HandleScope scope(isolate_); + ScriptCompiler::CachedData* data = + entry->pending_module.IsEmpty() + ? SerializeCodeCache(entry->pending_function.Get(isolate_)) + : SerializeCodeCache(entry->pending_module.Get(isolate_)); + entry->pending_function.Reset(); + entry->pending_module.Reset(); + if (data == nullptr) { + Debug("[compile cache] nothing to serialize for %s %s\n", + entry->type_name(), + entry->source_filename); + continue; + } + Debug("[compile cache] serialized %s %s at exit, size=%d\n", + entry->type_name(), + entry->source_filename, + data->length); + entry->refreshed = true; + entry->cache.reset(data); + } +} + void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry, Local mod, bool rejected) { DCHECK(mod->IsSourceTextModule()); - MaybeSaveImpl(entry, mod, rejected); + DeferSave(entry, mod, rejected); } void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry, Local func, bool rejected) { - MaybeSaveImpl(entry, func, rejected); + DeferSave(entry, func, rejected); } void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry, @@ -438,6 +505,7 @@ void CompileCacheHandler::Persist() { Debug("[compile cache] read-only, skipping persistence\n"); return; } + SerializePending(); // TODO(joyeecheung): do this using a separate event loop to utilize the // libuv thread pool and do the file system operations concurrently. @@ -591,7 +659,8 @@ std::vector CompileCacheHandler::SerializeEntry( } std::vector>> -CompileCacheHandler::SerializeEntries() const { +CompileCacheHandler::SerializeEntries() { + SerializePending(); std::vector>> result; for (const auto& pair : compiler_cache_store_) { const CompileCacheEntry* entry = pair.second.get(); diff --git a/src/compile_cache.h b/src/compile_cache.h index 1bd2ca082860..edc156a11fb8 100644 --- a/src/compile_cache.h +++ b/src/compile_cache.h @@ -39,6 +39,11 @@ struct CompileCacheEntry { bool refreshed = false; bool persisted = false; + // The compiled script, kept until Persist() so that the code cache is + // produced after the module has run and includes lazily compiled functions. + v8::Global pending_function; + v8::Global pending_module; + // Copy the cache into a new store for V8 to consume. Caller takes // ownership. v8::ScriptCompiler::CachedData* CopyCache() const; @@ -91,10 +96,11 @@ class CompileCacheHandler { const std::string& key_root = std::string()); void Persist(); + // Produce the code cache of every entry whose serialization was deferred. + void SerializePending(); // Every entry holding a cache in the layout of a cache file, paired with // the name of the file Persist() would write it to. - std::vector>> SerializeEntries() - const; + std::vector>> SerializeEntries(); CompileCacheEntry* GetOrInsert(v8::Local code, v8::Local filename, @@ -125,6 +131,10 @@ class CompileCacheHandler { v8::Local func_or_mod, bool rejected); + template + void DeferSave(CompileCacheEntry* entry, v8::Local func_or_mod, + bool rejected); + template inline void Debug(const char* format, Args&&... args) const;