From 4df894856d041e88a4b5518cff8b81c1a9e2a665 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 14 Aug 2025 20:24:48 +0200 Subject: [PATCH 1/2] src: use simdjson to parse --snapshot-config --- src/node_snapshotable.cc | 65 +++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 9bdef1032e4dc9..61a5920954e912 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -11,7 +11,7 @@ #include "embedded_data.h" #include "encoding_binding.h" #include "env-inl.h" -#include "json_parser.h" +#include "simdjson.h" #include "node_blob.h" #include "node_builtins.h" #include "node_contextify.h" @@ -909,32 +909,61 @@ std::optional ReadSnapshotConfig(const char* config_path) { return std::nullopt; } - JSONParser parser; - if (!parser.Parse(config_content)) { - FPrintF(stderr, "Cannot parse JSON from %s\n", config_path); - return std::nullopt; - } - SnapshotConfig result; - result.builder_script_path = parser.GetTopLevelStringField("builder"); - if (!result.builder_script_path.has_value()) { + + simdjson::ondemand::parser parser; + simdjson::ondemand::document document; + simdjson::ondemand::object main_object; + simdjson::error_code error = + parser.iterate(simdjson::pad(config_content)).get(document); + + if (!error) { + error = document.get_object().get(main_object); + } + if (error) { FPrintF(stderr, - "\"builder\" field of %s is not a non-empty string\n", - config_path); + "Cannot parse JSON from %s: %s\n", + config_path, + simdjson::error_message(error)); return std::nullopt; } - std::optional WithoutCodeCache = - parser.GetTopLevelBoolField("withoutCodeCache"); - if (!WithoutCodeCache.has_value()) { + for (auto field : main_object) { + std::string_view key; + if (field.unescaped_key().get(key)) { + FPrintF(stderr, "Cannot read key from %s\n", config_path); + return std::nullopt; + } + if (key == "builder") { + std::string builder_path; + if (field.value().get_string().get(builder_path) || + builder_path.empty()) { + FPrintF(stderr, + "\"builder\" field of %s is not a non-empty string\n", + config_path); + return std::nullopt; + } + result.builder_script_path = builder_path; + } else if (key == "withoutCodeCache") { + bool without_code_cache_value = false; + if (field.value().get_bool().get(without_code_cache_value)) { + FPrintF(stderr, + "\"withoutCodeCache\" field of %s is not a boolean\n", + config_path); + return std::nullopt; + } + if (without_code_cache_value) { + result.flags |= SnapshotFlags::kWithoutCodeCache; + } + } + } + + if (!result.builder_script_path.has_value()) { FPrintF(stderr, - "\"withoutCodeCache\" field of %s is not a boolean\n", + "\"builder\" field of %s is not a non-empty string\n", config_path); return std::nullopt; } - if (WithoutCodeCache.value()) { - result.flags |= SnapshotFlags::kWithoutCodeCache; - } return result; } From c8df039ef8a9a8328b6b10a2fa45f1dcb0b293db Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 14 Aug 2025 22:35:50 +0200 Subject: [PATCH 2/2] fixup! src: use simdjson to parse --snapshot-config --- src/node_snapshotable.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 61a5920954e912..c2e24b4645e790 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -11,7 +11,6 @@ #include "embedded_data.h" #include "encoding_binding.h" #include "env-inl.h" -#include "simdjson.h" #include "node_blob.h" #include "node_builtins.h" #include "node_contextify.h" @@ -27,6 +26,7 @@ #include "node_url.h" #include "node_v8.h" #include "node_v8_platform-inl.h" +#include "simdjson.h" #include "timers.h" #if HAVE_INSPECTOR