Skip to content

Commit 1b4885a

Browse files
joyeecheungrichardlau
authored andcommitted
src: use simdjson to parse --snapshot-config
PR-URL: #59473 Refs: #59288 Reviewed-By: Daniel Lemire <[email protected]>
1 parent 75bf3f4 commit 1b4885a

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/node_snapshotable.cc

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "embedded_data.h"
1212
#include "encoding_binding.h"
1313
#include "env-inl.h"
14-
#include "json_parser.h"
1514
#include "node_blob.h"
1615
#include "node_builtins.h"
1716
#include "node_contextify.h"
@@ -27,6 +26,7 @@
2726
#include "node_url.h"
2827
#include "node_v8.h"
2928
#include "node_v8_platform-inl.h"
29+
#include "simdjson.h"
3030
#include "timers.h"
3131

3232
#if HAVE_INSPECTOR
@@ -908,32 +908,61 @@ std::optional<SnapshotConfig> ReadSnapshotConfig(const char* config_path) {
908908
return std::nullopt;
909909
}
910910

911-
JSONParser parser;
912-
if (!parser.Parse(config_content)) {
913-
FPrintF(stderr, "Cannot parse JSON from %s\n", config_path);
914-
return std::nullopt;
915-
}
916-
917911
SnapshotConfig result;
918-
result.builder_script_path = parser.GetTopLevelStringField("builder");
919-
if (!result.builder_script_path.has_value()) {
912+
913+
simdjson::ondemand::parser parser;
914+
simdjson::ondemand::document document;
915+
simdjson::ondemand::object main_object;
916+
simdjson::error_code error =
917+
parser.iterate(simdjson::pad(config_content)).get(document);
918+
919+
if (!error) {
920+
error = document.get_object().get(main_object);
921+
}
922+
if (error) {
920923
FPrintF(stderr,
921-
"\"builder\" field of %s is not a non-empty string\n",
922-
config_path);
924+
"Cannot parse JSON from %s: %s\n",
925+
config_path,
926+
simdjson::error_message(error));
923927
return std::nullopt;
924928
}
925929

926-
std::optional<bool> WithoutCodeCache =
927-
parser.GetTopLevelBoolField("withoutCodeCache");
928-
if (!WithoutCodeCache.has_value()) {
930+
for (auto field : main_object) {
931+
std::string_view key;
932+
if (field.unescaped_key().get(key)) {
933+
FPrintF(stderr, "Cannot read key from %s\n", config_path);
934+
return std::nullopt;
935+
}
936+
if (key == "builder") {
937+
std::string builder_path;
938+
if (field.value().get_string().get(builder_path) ||
939+
builder_path.empty()) {
940+
FPrintF(stderr,
941+
"\"builder\" field of %s is not a non-empty string\n",
942+
config_path);
943+
return std::nullopt;
944+
}
945+
result.builder_script_path = builder_path;
946+
} else if (key == "withoutCodeCache") {
947+
bool without_code_cache_value = false;
948+
if (field.value().get_bool().get(without_code_cache_value)) {
949+
FPrintF(stderr,
950+
"\"withoutCodeCache\" field of %s is not a boolean\n",
951+
config_path);
952+
return std::nullopt;
953+
}
954+
if (without_code_cache_value) {
955+
result.flags |= SnapshotFlags::kWithoutCodeCache;
956+
}
957+
}
958+
}
959+
960+
if (!result.builder_script_path.has_value()) {
929961
FPrintF(stderr,
930-
"\"withoutCodeCache\" field of %s is not a boolean\n",
962+
"\"builder\" field of %s is not a non-empty string\n",
931963
config_path);
932964
return std::nullopt;
933965
}
934-
if (WithoutCodeCache.value()) {
935-
result.flags |= SnapshotFlags::kWithoutCodeCache;
936-
}
937966

938967
return result;
939968
}

0 commit comments

Comments
 (0)