src: make minor cleanups in compile_cache.cc

PR-URL: https://github.com/nodejs/node/pull/57448
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
James M Snell 2025-03-13 14:39:46 -07:00
parent fef180c8a2
commit 387b8cbb8b

View File

@ -14,6 +14,14 @@
#endif
namespace node {
using v8::Function;
using v8::Local;
using v8::Module;
using v8::ScriptCompiler;
using v8::String;
namespace {
std::string Uint32ToHex(uint32_t crc) {
std::string str;
str.reserve(8);
@ -40,8 +48,7 @@ std::string GetCacheVersionTag() {
// This should be fine on Windows, as there local directories tend to be
// user-specific.
std::string tag = std::string(NODE_VERSION) + '-' + std::string(NODE_ARCH) +
'-' +
Uint32ToHex(v8::ScriptCompiler::CachedDataVersionTag());
'-' + Uint32ToHex(ScriptCompiler::CachedDataVersionTag());
#ifdef NODE_IMPLEMENTS_POSIX_CREDENTIALS
tag += '-' + std::to_string(getuid());
#endif
@ -55,6 +62,7 @@ uint32_t GetCacheKey(std::string_view filename, CachedCodeType type) {
crc, reinterpret_cast<const Bytef*>(filename.data()), filename.length());
return crc;
}
} // namespace
template <typename... Args>
inline void CompileCacheHandler::Debug(const char* format,
@ -64,13 +72,13 @@ inline void CompileCacheHandler::Debug(const char* format,
}
}
v8::ScriptCompiler::CachedData* CompileCacheEntry::CopyCache() const {
ScriptCompiler::CachedData* CompileCacheEntry::CopyCache() const {
DCHECK_NOT_NULL(cache);
int cache_size = cache->length;
uint8_t* data = new uint8_t[cache_size];
memcpy(data, cache->data, cache_size);
return new v8::ScriptCompiler::CachedData(
data, cache_size, v8::ScriptCompiler::CachedData::BufferOwned);
return new ScriptCompiler::CachedData(
data, cache_size, ScriptCompiler::CachedData::BufferOwned);
}
// Used for identifying and verifying a file is a compile cache file.
@ -210,15 +218,14 @@ void CompileCacheHandler::ReadCacheFile(CompileCacheEntry* entry) {
return;
}
entry->cache.reset(new v8::ScriptCompiler::CachedData(
buffer, total_read, v8::ScriptCompiler::CachedData::BufferOwned));
entry->cache.reset(new ScriptCompiler::CachedData(
buffer, total_read, ScriptCompiler::CachedData::BufferOwned));
Debug(" success, size=%d\n", total_read);
}
CompileCacheEntry* CompileCacheHandler::GetOrInsert(
v8::Local<v8::String> code,
v8::Local<v8::String> filename,
CachedCodeType type) {
CompileCacheEntry* CompileCacheHandler::GetOrInsert(Local<String> code,
Local<String> filename,
CachedCodeType type) {
DCHECK(!compile_cache_dir_.empty());
Utf8Value filename_utf8(isolate_, filename);
@ -259,18 +266,17 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(
return result;
}
v8::ScriptCompiler::CachedData* SerializeCodeCache(
v8::Local<v8::Function> func) {
return v8::ScriptCompiler::CreateCodeCacheForFunction(func);
ScriptCompiler::CachedData* SerializeCodeCache(Local<Function> func) {
return ScriptCompiler::CreateCodeCacheForFunction(func);
}
v8::ScriptCompiler::CachedData* SerializeCodeCache(v8::Local<v8::Module> mod) {
return v8::ScriptCompiler::CreateCodeCache(mod->GetUnboundModuleScript());
ScriptCompiler::CachedData* SerializeCodeCache(Local<Module> mod) {
return ScriptCompiler::CreateCodeCache(mod->GetUnboundModuleScript());
}
template <typename T>
void CompileCacheHandler::MaybeSaveImpl(CompileCacheEntry* entry,
v8::Local<T> func_or_mod,
Local<T> func_or_mod,
bool rejected) {
DCHECK_NOT_NULL(entry);
Debug("[compile cache] V8 code cache for %s %s was %s, ",
@ -286,21 +292,21 @@ void CompileCacheHandler::MaybeSaveImpl(CompileCacheEntry* entry,
Debug("%s the in-memory entry\n",
entry->cache == nullptr ? "initializing" : "refreshing");
v8::ScriptCompiler::CachedData* data = SerializeCodeCache(func_or_mod);
DCHECK_EQ(data->buffer_policy, v8::ScriptCompiler::CachedData::BufferOwned);
ScriptCompiler::CachedData* data = SerializeCodeCache(func_or_mod);
DCHECK_EQ(data->buffer_policy, ScriptCompiler::CachedData::BufferOwned);
entry->refreshed = true;
entry->cache.reset(data);
}
void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
v8::Local<v8::Module> mod,
Local<Module> mod,
bool rejected) {
DCHECK(mod->IsSourceTextModule());
MaybeSaveImpl(entry, mod, rejected);
}
void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
v8::Local<v8::Function> func,
Local<Function> func,
bool rejected) {
MaybeSaveImpl(entry, func, rejected);
}
@ -319,8 +325,8 @@ void CompileCacheHandler::MaybeSave(CompileCacheEntry* entry,
int cache_size = static_cast<int>(transpiled.size());
uint8_t* data = new uint8_t[cache_size];
memcpy(data, transpiled.data(), cache_size);
entry->cache.reset(new v8::ScriptCompiler::CachedData(
data, cache_size, v8::ScriptCompiler::CachedData::BufferOwned));
entry->cache.reset(new ScriptCompiler::CachedData(
data, cache_size, ScriptCompiler::CachedData::BufferOwned));
entry->refreshed = true;
}
@ -377,7 +383,7 @@ void CompileCacheHandler::Persist() {
}
DCHECK_EQ(entry->cache->buffer_policy,
v8::ScriptCompiler::CachedData::BufferOwned);
ScriptCompiler::CachedData::BufferOwned);
char* cache_ptr =
reinterpret_cast<char*>(const_cast<uint8_t*>(entry->cache->data));
uint32_t cache_size = static_cast<uint32_t>(entry->cache->length);