|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | + |
| 3 | +Date: Tue, 6 Oct 2026 17:11:53 -0400 |
| 4 | +Subject: src: stop including simdjson.h from env.h |
| 5 | + |
| 6 | +Backport of https://github.com/nodejs/node/pull/66620 (upstream commit |
| 7 | +https://github.com/nodejs/node/commit/2b17b43eeba4484d43afaa1d5e3f951f006c5df6). |
| 8 | +Only the include-reduction commit is taken; the simdjson 5.0.3 update |
| 9 | +from the same PR is not. |
| 10 | + |
| 11 | +env.h includes inspector_profiler.h, which included simdjson.h for |
| 12 | +the types of two virtual methods and a parser member. As a result, |
| 13 | +about 155 files in src/ parsed the amalgamated simdjson header, which |
| 14 | +is over 10 MB. |
| 15 | + |
| 16 | +Keep simdjson out of inspector_profiler.h: the profile connections |
| 17 | +now say which field of `result` holds the profile, and |
| 18 | +WriteProfile() takes the profile as raw JSON. The parser becomes a |
| 19 | +local variable, so its buffers are no longer kept alive for the |
| 20 | +lifetime of the Environment. Also drop the unused include from |
| 21 | +node_task_runner.h. |
| 22 | + |
| 23 | +The upstream change to src/node_config_file.cc is omitted: it removes |
| 24 | +a second parse of the config file that does not exist on v24.x. |
| 25 | + |
| 26 | +This patch can be removed when Electron updates to a Node.js release |
| 27 | +that contains nodejs/node#66620. |
| 28 | + |
| 29 | +diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc |
| 30 | +index 559b4fd27d56abed0a9aaa7981e10fe12ebb0c2a..791ff993f84dcb2ee2bf85758e44362a5c23312e 100644 |
| 31 | +--- a/src/inspector_profiler.cc |
| 32 | ++++ b/src/inspector_profiler.cc |
| 33 | +@@ -13,7 +13,9 @@ |
| 34 | + |
| 35 | + #include <cinttypes> |
| 36 | + #include <limits> |
| 37 | ++#include <optional> |
| 38 | + #include <sstream> |
| 39 | ++#include "simdjson.h" |
| 40 | + #include "simdutf.h" |
| 41 | + |
| 42 | + namespace node { |
| 43 | +@@ -109,6 +111,28 @@ bool StringViewToUTF8(const v8_inspector::StringView& source, |
| 44 | + return *utf8_length == result_len; |
| 45 | + } |
| 46 | + |
| 47 | ++// Return the raw JSON of the profile object in `result`: either the |
| 48 | ++// `field` member of `result` or, if `field` is nullptr, `result` itself. |
| 49 | ++static std::optional<std::string_view> GetProfile( |
| 50 | ++ simdjson::ondemand::object* result, const char* field, const char* type) { |
| 51 | ++ simdjson::ondemand::object profile_object; |
| 52 | ++ if (field == nullptr) { |
| 53 | ++ profile_object = *result; |
| 54 | ++ } else if ((*result)[field].get_object().get(profile_object)) { |
| 55 | ++ fprintf( |
| 56 | ++ stderr, "'%s' from %s profile result is not an Object\n", field, type); |
| 57 | ++ return std::nullopt; |
| 58 | ++ } |
| 59 | ++ std::string_view profile_raw; |
| 60 | ++ if (profile_object.raw_json().get(profile_raw)) { |
| 61 | ++ fprintf(stderr, |
| 62 | ++ "Cannot get raw string of the 'profile' field from %s profile\n", |
| 63 | ++ type); |
| 64 | ++ return std::nullopt; |
| 65 | ++ } |
| 66 | ++ return profile_raw; |
| 67 | ++} |
| 68 | ++ |
| 69 | + void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
| 70 | + const v8_inspector::StringView& message) { |
| 71 | + Environment* env = connection_->env(); |
| 72 | +@@ -134,9 +158,10 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | ++ simdjson::ondemand::parser json_parser; |
| 77 | + simdjson::ondemand::document parsed; |
| 78 | + simdjson::ondemand::object response; |
| 79 | +- if (connection_->json_parser_ |
| 80 | ++ if (json_parser |
| 81 | + .iterate( |
| 82 | + message_utf8.data(), message_utf8_length, message_utf8.size()) |
| 83 | + .get(parsed) || |
| 84 | +@@ -184,7 +209,10 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend( |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | +- connection_->WriteProfile(&result); |
| 89 | ++ auto profile = GetProfile(&result, connection_->profile_field(), type); |
| 90 | ++ if (profile.has_value()) { |
| 91 | ++ connection_->WriteProfile(*profile); |
| 92 | ++ } |
| 93 | + connection_->RemoveProfileId(id); |
| 94 | + } |
| 95 | + |
| 96 | +@@ -214,32 +242,7 @@ std::string V8CoverageConnection::GetFilename() const { |
| 97 | + env()->thread_id()); |
| 98 | + } |
| 99 | + |
| 100 | +-std::optional<std::string_view> V8ProfilerConnection::GetProfile( |
| 101 | +- simdjson::ondemand::object* result) { |
| 102 | +- simdjson::ondemand::object profile_object; |
| 103 | +- if ((*result)["profile"].get_object().get(profile_object)) { |
| 104 | +- fprintf( |
| 105 | +- stderr, "'profile' from %s profile result is not an Object\n", type()); |
| 106 | +- return std::nullopt; |
| 107 | +- } |
| 108 | +- std::string_view profile_raw; |
| 109 | +- if (profile_object.raw_json().get(profile_raw)) { |
| 110 | +- fprintf(stderr, |
| 111 | +- "Cannot get raw string of the 'profile' field from %s profile\n", |
| 112 | +- type()); |
| 113 | +- return std::nullopt; |
| 114 | +- } |
| 115 | +- return profile_raw; |
| 116 | +-} |
| 117 | +- |
| 118 | +-void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 119 | +- // Generate the profile output from the subclass. |
| 120 | +- auto profile_opt = GetProfile(result); |
| 121 | +- if (!profile_opt.has_value()) { |
| 122 | +- return; |
| 123 | +- } |
| 124 | +- std::string_view profile = profile_opt.value(); |
| 125 | +- |
| 126 | ++void V8ProfilerConnection::WriteProfile(std::string_view profile) { |
| 127 | + // Create the directory if necessary. |
| 128 | + std::string directory = GetDirectory(); |
| 129 | + DCHECK(!directory.empty()); |
| 130 | +@@ -254,7 +257,7 @@ void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 131 | + WriteResult(env_, path.c_str(), profile); |
| 132 | + } |
| 133 | + |
| 134 | +-void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 135 | ++void V8CoverageConnection::WriteProfile(std::string_view profile) { |
| 136 | + Isolate* isolate = env_->isolate(); |
| 137 | + HandleScope handle_scope(isolate); |
| 138 | + |
| 139 | +@@ -271,13 +274,6 @@ void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 140 | + Local<Context> context = env_->context(); |
| 141 | + Context::Scope context_scope(context); |
| 142 | + |
| 143 | +- // Generate the profile output from the subclass. |
| 144 | +- auto profile_opt = GetProfile(result); |
| 145 | +- if (!profile_opt.has_value()) { |
| 146 | +- return; |
| 147 | +- } |
| 148 | +- std::string_view profile = profile_opt.value(); |
| 149 | +- |
| 150 | + // append source-map cache information to coverage object: |
| 151 | + Local<Value> source_map_cache_v; |
| 152 | + { |
| 153 | +@@ -349,18 +345,6 @@ void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) { |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +-std::optional<std::string_view> V8CoverageConnection::GetProfile( |
| 158 | +- simdjson::ondemand::object* result) { |
| 159 | +- std::string_view profile_raw; |
| 160 | +- if (result->raw_json().get(profile_raw)) { |
| 161 | +- fprintf(stderr, |
| 162 | +- "Cannot get raw string of the 'profile' field from %s profile\n", |
| 163 | +- type()); |
| 164 | +- return std::nullopt; |
| 165 | +- } |
| 166 | +- return profile_raw; |
| 167 | +-} |
| 168 | +- |
| 169 | + std::string V8CoverageConnection::GetDirectory() const { |
| 170 | + return env()->coverage_directory(); |
| 171 | + } |
| 172 | +diff --git a/src/inspector_profiler.h b/src/inspector_profiler.h |
| 173 | +index d07334c818d90ec32594e5776b87232744425246..cdd6bcf6f0aa0bcbc77b916a827afa35c3b59ea2 100644 |
| 174 | +--- a/src/inspector_profiler.h |
| 175 | ++++ b/src/inspector_profiler.h |
| 176 | +@@ -7,10 +7,9 @@ |
| 177 | + #error("This header can only be used when inspector is enabled") |
| 178 | + #endif |
| 179 | + |
| 180 | +-#include <optional> |
| 181 | ++#include <string_view> |
| 182 | + #include <unordered_set> |
| 183 | + #include "inspector_agent.h" |
| 184 | +-#include "simdjson.h" |
| 185 | + |
| 186 | + namespace node { |
| 187 | + // Forward declaration to break recursive dependency chain with src/env.h. |
| 188 | +@@ -59,11 +58,11 @@ class V8ProfilerConnection { |
| 189 | + virtual std::string GetDirectory() const = 0; |
| 190 | + // Return the filename the profile should be written as. |
| 191 | + virtual std::string GetFilename() const = 0; |
| 192 | +- // Return the profile object parsed from `message.result`, |
| 193 | +- // which will be then written as a JSON. |
| 194 | +- virtual std::optional<std::string_view> GetProfile( |
| 195 | +- simdjson::ondemand::object* result); |
| 196 | +- virtual void WriteProfile(simdjson::ondemand::object* result); |
| 197 | ++ // Return the field of `message.result` that holds the profile object, |
| 198 | ++ // or nullptr if `message.result` is itself the profile object. |
| 199 | ++ virtual const char* profile_field() const { return "profile"; } |
| 200 | ++ // Write the profile, given as raw JSON. |
| 201 | ++ virtual void WriteProfile(std::string_view profile); |
| 202 | + |
| 203 | + bool HasProfileId(uint64_t id) const { return profile_ids_.contains(id); } |
| 204 | + |
| 205 | +@@ -76,7 +75,6 @@ class V8ProfilerConnection { |
| 206 | + std::unordered_set<uint64_t> profile_ids_; |
| 207 | + |
| 208 | + protected: |
| 209 | +- simdjson::ondemand::parser json_parser_; |
| 210 | + Environment* env_ = nullptr; |
| 211 | + }; |
| 212 | + |
| 213 | +@@ -92,9 +90,8 @@ class V8CoverageConnection : public V8ProfilerConnection { |
| 214 | + |
| 215 | + std::string GetDirectory() const override; |
| 216 | + std::string GetFilename() const override; |
| 217 | +- std::optional<std::string_view> GetProfile( |
| 218 | +- simdjson::ondemand::object* result) override; |
| 219 | +- void WriteProfile(simdjson::ondemand::object* result) override; |
| 220 | ++ const char* profile_field() const override { return nullptr; } |
| 221 | ++ void WriteProfile(std::string_view profile) override; |
| 222 | + void WriteSourceMapCache(); |
| 223 | + void TakeCoverage(); |
| 224 | + void StopCoverage(); |
| 225 | +diff --git a/src/node_task_runner.cc b/src/node_task_runner.cc |
| 226 | +index 55f348afc69ae3244bca41901c94cb0ad0263a97..136dec24fa023fdd32150401be38194a80116910 100644 |
| 227 | +--- a/src/node_task_runner.cc |
| 228 | ++++ b/src/node_task_runner.cc |
| 229 | +@@ -1,4 +1,5 @@ |
| 230 | + #include "node_task_runner.h" |
| 231 | ++#include "simdjson.h" |
| 232 | + #include "util-inl.h" |
| 233 | + |
| 234 | + #include <regex> // NOLINT(build/c++11) |
| 235 | +diff --git a/src/node_task_runner.h b/src/node_task_runner.h |
| 236 | +index 15573498f7393a04302237dd42e265bba73141b3..8cdfe572353395567465e29b0e63b8919e2608d1 100644 |
| 237 | +--- a/src/node_task_runner.h |
| 238 | ++++ b/src/node_task_runner.h |
| 239 | +@@ -4,7 +4,6 @@ |
| 240 | + #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
| 241 | + |
| 242 | + #include "node_internals.h" |
| 243 | +-#include "simdjson.h" |
| 244 | + #include "spawn_sync.h" |
| 245 | + #include "uv.h" |
| 246 | + |