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"
1920namespace node {
2021
2122using v8::Function;
23+ using v8::HandleScope;
2224using v8::Local;
2325using v8::Module;
2426using v8::ScriptCompiler;
2527using v8::String;
28+ using v8::UnboundModuleScript;
2629using v8::WasmModuleObject;
2730
2831namespace {
@@ -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+
380447void 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
387454void CompileCacheHandler::MaybeSave (CompileCacheEntry* entry,
388455 Local<Function> func,
389456 bool rejected) {
390- MaybeSaveImpl (entry, func, rejected);
457+ DeferSave (entry, func, rejected);
391458}
392459
393460void 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
593661std::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 ();
0 commit comments