| 42 | } |
| 43 | |
| 44 | int main(int argc, char** argv) { |
| 45 | const char* path = (argc > 1) ? argv[1] : "dev/all_fluids.json"; |
| 46 | std::string s = read_file(path); |
| 47 | std::printf("file: %s size: %.2f MB\n\n", path, s.size() / (1024.0 * 1024.0)); |
| 48 | |
| 49 | const int iters = 15; |
| 50 | |
| 51 | // RapidJSON full DOM parse (matches the loaders' d.Parse<0>(...) usage). |
| 52 | run("rapidjson", iters, [&] { |
| 53 | rapidjson::Document d; |
| 54 | d.Parse<0>(s.c_str()); |
| 55 | if (d.HasParseError()) { |
| 56 | std::fprintf(stderr, "rapidjson parse error\n"); |
| 57 | std::abort(); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | // nlohmann full DOM parse. |
| 62 | run("nlohmann-json", iters, [&] { |
| 63 | auto j = nlohmann::json::parse(s); |
| 64 | if (!j.is_array() && !j.is_object()) { |
| 65 | std::fprintf(stderr, "nlohmann parse error\n"); |
| 66 | std::abort(); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | // Mitigation: pre-parsed binary blobs (nlohmann reads these natively and |
| 71 | // much faster than text JSON). Generate once from the parsed doc, then |
| 72 | // time deserialization — this is what a CBOR/MessagePack-embedded build |
| 73 | // would pay at first load. |
| 74 | nlohmann::json doc = nlohmann::json::parse(s); |
| 75 | std::vector<std::uint8_t> mp = nlohmann::json::to_msgpack(doc); |
| 76 | std::vector<std::uint8_t> cb = nlohmann::json::to_cbor(doc); |
| 77 | std::printf("\nbinary blob sizes: msgpack %.2f MB cbor %.2f MB (json text %.2f MB)\n\n", mp.size() / (1024.0 * 1024.0), |
| 78 | cb.size() / (1024.0 * 1024.0), s.size() / (1024.0 * 1024.0)); |
| 79 | |
| 80 | run("nlohmann-mpack", iters, [&] { |
| 81 | auto j = nlohmann::json::from_msgpack(mp); |
| 82 | if (!j.is_array() && !j.is_object()) { |
| 83 | std::fprintf(stderr, "msgpack error\n"); |
| 84 | std::abort(); |
| 85 | } |
| 86 | }); |
| 87 | run("nlohmann-cbor", iters, [&] { |
| 88 | auto j = nlohmann::json::from_cbor(cb); |
| 89 | if (!j.is_array() && !j.is_object()) { |
| 90 | std::fprintf(stderr, "cbor error\n"); |
| 91 | std::abort(); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | return 0; |
| 96 | } |