diff --git a/src/node.h b/src/node.h index e5b23f30d9530b..c9b8960e85cec5 100644 --- a/src/node.h +++ b/src/node.h @@ -505,6 +505,9 @@ struct IsolateSettings { // feature during the build step by passing the --disable-shared-readonly-heap // flag to the configure script. // +// The snapshot *must* be kept alive during the execution of the Isolate +// that was created using it. +// // Snapshots are an *experimental* feature. In particular, the embedder API // exposed through this class is subject to change or removal between Node.js // versions, including possible API and ABI breakage. diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 776ac8f2358aca..245b17b80adf4c 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -501,13 +501,17 @@ void BuiltinLoader::CopyCodeCache(std::vector* out) const { void BuiltinLoader::RefreshCodeCache(const std::vector& in) { RwLock::ScopedLock lock(code_cache_->mutex); + code_cache_->map.reserve(in.size()); + DCHECK(code_cache_->map.empty()); for (auto const& item : in) { - size_t length = item.data.size(); - uint8_t* buffer = new uint8_t[length]; - memcpy(buffer, item.data.data(), length); - auto new_cache = std::make_unique( - buffer, length, v8::ScriptCompiler::CachedData::BufferOwned); - code_cache_->map[item.id] = std::move(new_cache); + auto result = code_cache_->map.emplace( + item.id, + std::make_unique( + item.data.data(), + item.data.size(), + v8::ScriptCompiler::CachedData::BufferNotOwned)); + USE(result.second); + DCHECK(result.second); } code_cache_->has_code_cache = true; }