src: avoid copying SEA snapshot data

Signed-off-by: Colin McDonnell <3084745+colinhacks@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/65876
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
Colin McDonnell authored and Antoine du Hamel committed 2026-09-16 21:12:16 +02:00
1 parent fa19ecad9d
commit 5a5abd8fd0
3 files changed
+46 -19

No files matched your search

+6 -1
View File
@@ -640,6 +640,7 @@ struct SnapshotData {
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
// building process.
v8::StartupData v8_snapshot_blob_data{nullptr, 0};
DataOwnership v8_snapshot_blob_data_ownership = DataOwnership::kOwned;
IsolateDataSerializeInfo isolate_data_info;
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
@@ -659,7 +660,11 @@ struct SnapshotData {
bool Check() const;
static bool FromFile(SnapshotData* out, FILE* in);
static bool FromBlob(SnapshotData* out, const std::vector<char>& in);
static bool FromBlob(SnapshotData* out, std::string_view in);
// If the V8 data is not owned, `in` must outlive `out`.
static bool FromBlob(
SnapshotData* out,
std::string_view in,
DataOwnership v8_snapshot_blob_data_ownership = DataOwnership::kOwned);
static const SnapshotData* FromEmbedderWrapper(
const EmbedderSnapshotData* data);
+5 -1
View File
@@ -1574,7 +1574,11 @@ bool LoadSnapshotData(const SnapshotData** snapshot_data_ptr) {
std::unique_ptr<SnapshotData> read_data =
std::make_unique<SnapshotData>();
std::string_view snapshot = sea.main_code_or_snapshot;
if (SnapshotData::FromBlob(read_data.get(), snapshot)) {
// The SEA resource remains mapped for the process lifetime, so V8 can
// consume the startup data directly from the executable image.
if (SnapshotData::FromBlob(read_data.get(),
snapshot,
SnapshotData::DataOwnership::kNotOwned)) {
*snapshot_data_ptr = read_data.release();
return true;
} else {
+35 -17
View File
@@ -158,6 +158,30 @@ class SnapshotDeserializer : public BlobDeserializer<SnapshotDeserializer> {
template <typename T>
requires(!std::is_arithmetic_v<T> && !std::same_as<T, std::string>)
T Read();
v8::StartupData ReadV8StartupData(SnapshotData::DataOwnership ownership) {
Debug("Read<v8::StartupData>()\n");
int raw_size = ReadArithmetic<int>();
Debug("size=%d\n", raw_size);
if (raw_size <= 0 ||
static_cast<size_t>(raw_size) > sink.size() - read_total) {
ok = false;
return v8::StartupData{nullptr, 0};
}
if (ownership == SnapshotData::DataOwnership::kOwned) {
// The data pointer of v8::StartupData would be deleted so it must be
// new'ed.
char* buf = new char[raw_size];
ReadArithmetic<char>(buf, raw_size);
return v8::StartupData{buf, raw_size};
}
const char* data = sink.data() + read_total;
read_total += raw_size;
return v8::StartupData{data, raw_size};
}
};
class SnapshotSerializer : public BlobSerializer<SnapshotSerializer> {
@@ -181,21 +205,7 @@ class SnapshotSerializer : public BlobSerializer<SnapshotSerializer> {
// [ |raw_size| bytes ] contents
template <>
v8::StartupData SnapshotDeserializer::Read() {
Debug("Read<v8::StartupData>()\n");
int raw_size = ReadArithmetic<int>();
Debug("size=%d\n", raw_size);
if (raw_size <= 0 ||
static_cast<size_t>(raw_size) > sink.size() - read_total) {
ok = false;
return v8::StartupData{nullptr, 0};
}
// The data pointer of v8::StartupData would be deleted so it must be new'ed.
char* buf = new char[raw_size];
ReadArithmetic<char>(buf, raw_size);
return v8::StartupData{buf, raw_size};
return ReadV8StartupData(SnapshotData::DataOwnership::kOwned);
}
template <>
@@ -640,7 +650,9 @@ bool SnapshotData::FromBlob(SnapshotData* out, const std::vector<char>& in) {
return FromBlob(out, std::string_view(in.data(), in.size()));
}
bool SnapshotData::FromBlob(SnapshotData* out, std::string_view in) {
bool SnapshotData::FromBlob(SnapshotData* out,
std::string_view in,
DataOwnership v8_snapshot_blob_data_ownership) {
SnapshotDeserializer r(in);
r.Debug("SnapshotData::FromBlob()\n");
@@ -660,7 +672,9 @@ bool SnapshotData::FromBlob(SnapshotData* out, std::string_view in) {
return false;
}
out->v8_snapshot_blob_data = r.Read<v8::StartupData>();
out->v8_snapshot_blob_data =
r.ReadV8StartupData(v8_snapshot_blob_data_ownership);
out->v8_snapshot_blob_data_ownership = v8_snapshot_blob_data_ownership;
r.Debug("Read isolate_data_info\n");
out->isolate_data_info = r.Read<IsolateDataSerializeInfo>();
out->env_info = r.Read<EnvSerializeInfo>();
@@ -709,6 +723,7 @@ bool SnapshotData::Check() const {
SnapshotData::~SnapshotData() {
if (data_ownership == DataOwnership::kOwned &&
v8_snapshot_blob_data_ownership == DataOwnership::kOwned &&
v8_snapshot_blob_data.data != nullptr &&
!IsFirstSnapshotBlob(v8_snapshot_blob_data.data)) {
delete[] v8_snapshot_blob_data.data;
@@ -831,6 +846,9 @@ namespace node {
// -- v8_snapshot_blob_data begins --
{ v8_snapshot_blob_data, v8_snapshot_blob_size },
// -- v8_snapshot_blob_data ends --
// -- v8_snapshot_blob_data_ownership begins --
SnapshotData::DataOwnership::kNotOwned,
// -- v8_snapshot_blob_data_ownership ends --
// -- isolate_data_info begins --
)" << data->isolate_data_info
<< R"(