Skip to content

Commit 93a368d

Browse files
joyeecheungtargos
authored andcommitted
src: use simdjson to parse --snapshot-config
PR-URL: #59473 Refs: #59288 Reviewed-By: Daniel Lemire <daniel@lemire.me>
1 parent 99128d9 commit 93a368d

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
@@ -909,32 +909,61 @@ std::optional<SnapshotConfig> ReadSnapshotConfig(const char* config_path) {
909909
return std::nullopt;
910910
}
911911

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

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

939968
return result;
940969
}

0 commit comments

Comments
 (0)