Skip to content

Commit 8ff6ae7

Browse files
committed
src: create compile cache when the process exits
Refs: #52696 Assisted-by: Claude Opus 5.5 Signed-off-by: Mert Can Altin <mertgold60@gmail.com>
1 parent 4176b7d commit 8ff6ae7

2 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎src/compile_cache.cc‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "compile_cache.h"
22
#include <string>
3+
#include <type_traits>
34
#include "debug_utils-inl.h"
45
#include "env-inl.h"
56
#include "node_file.h"
@@ -19,10 +20,12 @@
1920
namespace node {
2021

2122
using v8::Function;
23+
using v8::HandleScope;
2224
using v8::Local;
2325
using v8::Module;
2426
using v8::ScriptCompiler;
2527
using v8::String;
28+
using v8::UnboundModuleScript;
2629
using v8::WasmModuleObject;
2730

2831
namespace {
@@ -314,6 +317,10 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(const uint8_t* code,
314317
compile_cache_dir_ + kPathSeparator + Uint32ToHex(key);
315318
result->source_filename = filename_utf8.ToString();
316319
result->cache = nullptr;
320+
result->refreshed = false;
321+
result->persisted = false;
322+
result->pending_function.Reset();
323+
result->pending_module.Reset();
317324
result->type = type;
318325

319326
// TODO(joyeecheung): if we fail enough times, stop trying for any future
@@ -331,6 +338,10 @@ ScriptCompiler::CachedData* SerializeCodeCache(Local<Module> mod) {
331338
return ScriptCompiler::CreateCodeCache(mod->GetUnboundModuleScript());
332339
}
333340

341+
ScriptCompiler::CachedData* SerializeCodeCache(Local<UnboundModuleScript> s) {
342+
return ScriptCompiler::CreateCodeCache(s);
343+
}
344+
334345
// V8 only serializes code compiled by the optimizing tier, and produces
335346
// nothing if there is none, e.g. for a module that has only been compiled
336347
// with the baseline tier so far.
@@ -377,17 +388,73 @@ void CompileCacheHandler::MaybeSaveImpl(CompileCacheEntry* entry,
377388
entry->cache.reset(data);
378389
}
379390

391+
// Instead of serializing the code cache right after compilation, keep the
392+
// compiled script alive and serialize it in Persist(), after the module has
393+
// run, so that functions compiled lazily during execution are included.
394+
template <typename T>
395+
void CompileCacheHandler::DeferSave(CompileCacheEntry* entry,
396+
Local<T> func_or_mod,
397+
bool rejected) {
398+
DCHECK_NOT_NULL(entry);
399+
const bool accepted = entry->cache != nullptr && !rejected;
400+
if (read_only_ || accepted) {
401+
MaybeSaveImpl(entry, func_or_mod, rejected);
402+
return;
403+
}
404+
Debug("[compile cache] V8 code cache for %s %s was %s, initializing the "
405+
"in-memory entry at exit\n",
406+
entry->type_name(),
407+
entry->source_filename,
408+
rejected ? "rejected" : "not initialized");
409+
// A rejected cache must not be handed to V8 again.
410+
entry->cache.reset();
411+
if constexpr (std::is_same_v<T, Module>) {
412+
entry->pending_module.Reset(isolate_,
413+
func_or_mod->GetUnboundModuleScript());
414+
} else {
415+
entry->pending_function.Reset(isolate_, func_or_mod);
416+
}
417+
}
418+
419+
void CompileCacheHandler::SerializePending() {
420+
for (auto& pair : compiler_cache_store_) {
421+
auto* entry = pair.second.get();
422+
if (entry->pending_function.IsEmpty() && entry->pending_module.IsEmpty()) {
423+
continue;
424+
}
425+
HandleScope scope(isolate_);
426+
ScriptCompiler::CachedData* data =
427+
entry->pending_module.IsEmpty()
428+
? SerializeCodeCache(entry->pending_function.Get(isolate_))
429+
: SerializeCodeCache(entry->pending_module.Get(isolate_));
430+
entry->pending_function.Reset();
431+
entry->pending_module.Reset();
432+
if (data == nullptr) {
433+
Debug("[compile cache] nothing to serialize for %s %s\n",
434+
entry->type_name(),
435+
entry->source_filename);
436+
continue;
437+
}
438+
Debug("[compile cache] serialized %s %s at exit, size=%d\n",
439+
entry->type_name(),
440+
entry->source_filename,
441+
data->length);
442+
entry->refreshed = true;
443+
entry->cache.reset(data);
444+
}
445+
}
446+
380447
void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
381448
Local<Module> mod,
382449
bool rejected) {
383450
DCHECK(mod->IsSourceTextModule());
384-
MaybeSaveImpl(entry, mod, rejected);
451+
DeferSave(entry, mod, rejected);
385452
}
386453

387454
void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
388455
Local<Function> func,
389456
bool rejected) {
390-
MaybeSaveImpl(entry, func, rejected);
457+
DeferSave(entry, func, rejected);
391458
}
392459

393460
void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
@@ -438,6 +505,7 @@ void CompileCacheHandler::Persist() {
438505
Debug("[compile cache] read-only, skipping persistence\n");
439506
return;
440507
}
508+
SerializePending();
441509

442510
// TODO(joyeecheung): do this using a separate event loop to utilize the
443511
// libuv thread pool and do the file system operations concurrently.
@@ -591,7 +659,8 @@ std::vector<char> CompileCacheHandler::SerializeEntry(
591659
}
592660

593661
std::vector<std::pair<std::string, std::vector<char>>>
594-
CompileCacheHandler::SerializeEntries() const {
662+
CompileCacheHandler::SerializeEntries() {
663+
SerializePending();
595664
std::vector<std::pair<std::string, std::vector<char>>> result;
596665
for (const auto& pair : compiler_cache_store_) {
597666
const CompileCacheEntry* entry = pair.second.get();

‎src/compile_cache.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ struct CompileCacheEntry {
3939
bool refreshed = false;
4040
bool persisted = false;
4141

42+
// The compiled script, kept until Persist() so that the code cache is
43+
// produced after the module has run and includes lazily compiled functions.
44+
v8::Global<v8::Function> pending_function;
45+
v8::Global<v8::UnboundModuleScript> pending_module;
46+
4247
// Copy the cache into a new store for V8 to consume. Caller takes
4348
// ownership.
4449
v8::ScriptCompiler::CachedData* CopyCache() const;
@@ -91,10 +96,11 @@ class CompileCacheHandler {
9196
const std::string& key_root = std::string());
9297

9398
void Persist();
99+
// Produce the code cache of every entry whose serialization was deferred.
100+
void SerializePending();
94101
// Every entry holding a cache in the layout of a cache file, paired with
95102
// the name of the file Persist() would write it to.
96-
std::vector<std::pair<std::string, std::vector<char>>> SerializeEntries()
97-
const;
103+
std::vector<std::pair<std::string, std::vector<char>>> SerializeEntries();
98104

99105
CompileCacheEntry* GetOrInsert(v8::Local<v8::String> code,
100106
v8::Local<v8::String> filename,
@@ -125,6 +131,10 @@ class CompileCacheHandler {
125131
v8::Local<T> func_or_mod,
126132
bool rejected);
127133

134+
template <typename T>
135+
void DeferSave(CompileCacheEntry* entry, v8::Local<T> func_or_mod,
136+
bool rejected);
137+
128138
template <typename... Args>
129139
inline void Debug(const char* format, Args&&... args) const;
130140

0 commit comments

Comments
 (0)