Skip to content

Commit cc473f3

Browse files
committed
src: use simdjson to parse --snapshot-config
1 parent af5d1c9 commit cc473f3

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/node_snapshotable.cc

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "embedded_data.h"
1212
#include "encoding_binding.h"
1313
#include "env-inl.h"
14-
#include "json_parser.h"
14+
#include "simdjson.h"
1515
#include "node_blob.h"
1616
#include "node_builtins.h"
1717
#include "node_contextify.h"
@@ -909,32 +909,62 @@ 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+
bool without_code_cache_value = false;
932+
933+
for (auto field : main_object) {
934+
std::string_view key;
935+
if (field.unescaped_key().get(key)) {
936+
FPrintF(stderr, "Cannot read key from %s\n", config_path);
937+
return std::nullopt;
938+
}
939+
if (key == "builder") {
940+
std::string builder_path;
941+
if (field.value().get_string().get(builder_path) ||
942+
builder_path.empty()) {
943+
FPrintF(stderr,
944+
"\"builder\" field of %s is not a non-empty string\n",
945+
config_path);
946+
return std::nullopt;
947+
}
948+
result.builder_script_path = builder_path;
949+
} else if (key == "withoutCodeCache") {
950+
if (field.value().get_bool().get(without_code_cache_value)) {
951+
FPrintF(stderr,
952+
"\"withoutCodeCache\" field of %s is not a boolean\n",
953+
config_path);
954+
return std::nullopt;
955+
}
956+
if (without_code_cache_value) {
957+
result.flags |= SnapshotFlags::kWithoutCodeCache;
958+
}
959+
}
960+
}
961+
962+
if (!result.builder_script_path.has_value()) {
930963
FPrintF(stderr,
931-
"\"withoutCodeCache\" field of %s is not a boolean\n",
964+
"\"builder\" field of %s is not a non-empty string\n",
932965
config_path);
933966
return std::nullopt;
934967
}
935-
if (WithoutCodeCache.value()) {
936-
result.flags |= SnapshotFlags::kWithoutCodeCache;
937-
}
938968

939969
return result;
940970
}

0 commit comments

Comments
 (0)