Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 72 additions & 3 deletions src/compile_cache.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "compile_cache.h"
#include <string>
#include <type_traits>
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "node_file.h"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -331,6 +338,10 @@ ScriptCompiler::CachedData* SerializeCodeCache(Local<Module> mod) {
return ScriptCompiler::CreateCodeCache(mod->GetUnboundModuleScript());
}

ScriptCompiler::CachedData* SerializeCodeCache(Local<UnboundModuleScript> 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.
Expand Down Expand Up @@ -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 <typename T>
void CompileCacheHandler::DeferSave(CompileCacheEntry* entry,
Local<T> 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<T, Module>) {
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<Module> mod,
bool rejected) {
DCHECK(mod->IsSourceTextModule());
MaybeSaveImpl(entry, mod, rejected);
DeferSave(entry, mod, rejected);
}

void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
Local<Function> func,
bool rejected) {
MaybeSaveImpl(entry, func, rejected);
DeferSave(entry, func, rejected);
}

void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -591,7 +659,8 @@ std::vector<char> CompileCacheHandler::SerializeEntry(
}

std::vector<std::pair<std::string, std::vector<char>>>
CompileCacheHandler::SerializeEntries() const {
CompileCacheHandler::SerializeEntries() {
SerializePending();
std::vector<std::pair<std::string, std::vector<char>>> result;
for (const auto& pair : compiler_cache_store_) {
const CompileCacheEntry* entry = pair.second.get();
Expand Down
14 changes: 12 additions & 2 deletions src/compile_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Function> pending_function;
v8::Global<v8::UnboundModuleScript> pending_module;

// Copy the cache into a new store for V8 to consume. Caller takes
// ownership.
v8::ScriptCompiler::CachedData* CopyCache() const;
Expand Down Expand Up @@ -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<std::pair<std::string, std::vector<char>>> SerializeEntries()
const;
std::vector<std::pair<std::string, std::vector<char>>> SerializeEntries();

CompileCacheEntry* GetOrInsert(v8::Local<v8::String> code,
v8::Local<v8::String> filename,
Expand Down Expand Up @@ -125,6 +131,10 @@ class CompileCacheHandler {
v8::Local<T> func_or_mod,
bool rejected);

template <typename T>
void DeferSave(CompileCacheEntry* entry, v8::Local<T> func_or_mod,
bool rejected);

template <typename... Args>
inline void Debug(const char* format, Args&&... args) const;

Expand Down