Add large_random/rapidjson_sax.h and large_random/nlohmann_json_sax.h. Clean up kostya/rapidjson_sax.h (add flags also) and kostya/nlohmann_json_sax.h (#1600)

This commit is contained in:
Nicolas Boyer 2021-06-03 16:40:39 -04:00 committed by GitHub
parent d7d81c7152
commit 05f15d88b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 161 additions and 21 deletions

View File

@ -39,7 +39,9 @@ SIMDJSON_POP_DISABLE_WARNINGS
#include "large_random/yyjson.h"
#include "large_random/sajson.h"
#include "large_random/rapidjson.h"
#include "large_random/rapidjson_sax.h"
#include "large_random/nlohmann_json.h"
#include "large_random/nlohmann_json_sax.h"
#include "kostya/simdjson_dom.h"
#include "kostya/simdjson_ondemand.h"

View File

@ -15,9 +15,9 @@ struct nlohmann_json_sax {
{
size_t k{0};
double buffer[3];
std::vector<point>* result;
std::vector<point>& result;
Handler(std::vector<point> &r) { result=&r; }
Handler(std::vector<point>& r) : result(r) { }
bool key(string_t& val) override {
switch(val[0]) {
@ -33,18 +33,18 @@ struct nlohmann_json_sax {
}
return true;
}
bool number_unsigned(number_unsigned_t val) override {
bool number_float(number_float_t val, const string_t& s) override {
buffer[k] = val;
if (k==2) {
(*result).emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
}
bool number_float(number_float_t val, const string_t& s) override {
}
bool number_unsigned(number_unsigned_t val) override { // Need this event because coordinate value can be equal to 1
buffer[k] = val;
if (k==2) {
(*result).emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
@ -65,7 +65,6 @@ struct nlohmann_json_sax {
bool run(simdjson::padded_string &json, std::vector<point> &result) {
Handler handler(result);
json::sax_parse(json.data(), &handler);
return true;
}
}; // nlohmann_json_sax

View File

@ -14,9 +14,9 @@ struct rapidjson_sax {
struct Handler {
size_t k{0};
double buffer[3];
std::vector<point>* result;
std::vector<point>& result;
Handler(std::vector<point> &r) { result=&r; }
Handler(std::vector<point> &r) : result(r) { }
bool Key(const char* key, SizeType length, bool copy) {
switch(key[0]) {
@ -34,15 +34,13 @@ struct rapidjson_sax {
}
bool Double(double d) {
buffer[k] = d;
if (k==2) {
(*result).emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
}
bool Uint(unsigned i) { // Need this event because coordinate value can be equal to 1
return Double(i);
}
bool Uint(unsigned i) { return Double(i); } // Need this event because coordinate value can be equal to 1
// Irrelevant events
bool Null() { return true; }
bool Bool(bool b) { return true; }
@ -60,14 +58,13 @@ struct rapidjson_sax {
bool run(simdjson::padded_string &json, std::vector<point> &result) {
Reader reader;
Handler handler(result);
StringStream ss(json.data());
reader.Parse(ss,handler);
InsituStringStream ss(json.data());
reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag | kParseFullPrecisionFlag>(ss,handler);
return true;
}
}; // rapid_jason_sax
BENCHMARK_TEMPLATE(kostya, rapidjson_sax)->UseManualTime();
} // namespace kostyacd
} // namespace kostya
#endif // SIMDJSON_COMPETITION_RAPIDJSON

View File

@ -0,0 +1,73 @@
#pragma once
#ifdef SIMDJSON_COMPETITION_NLOHMANN_JSON
#include "large_random.h"
namespace large_random {
using json = nlohmann::json;
struct nlohmann_json_sax {
static constexpr diff_flags DiffFlags = diff_flags::NONE;
struct Handler : json::json_sax_t
{
size_t k{0};
double buffer[3];
std::vector<point>& result;
Handler(std::vector<point> &r) : result(r) { }
bool key(string_t& val) override {
switch(val[0]) {
case 'x':
k = 0;
break;
case 'y':
k = 1;
break;
case 'z':
k = 2;
break;
}
return true;
}
bool number_unsigned(number_unsigned_t val) override {
buffer[k] = val;
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
}
bool number_float(number_float_t val, const string_t& s) override {
buffer[k] = val;
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
}
// Irrelevant events
bool null() override { return true; }
bool boolean(bool val) override { return true; }
bool number_integer(number_integer_t val) override { return true; }
bool string(string_t& val) override { return true; }
bool start_object(std::size_t elements) override { return true; }
bool end_object() override { return true; }
bool start_array(std::size_t elements) override { return true; }
bool end_array() override { return true; }
bool binary(json::binary_t& val) override { return true; }
bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override { return false; }
}; // Handler
bool run(simdjson::padded_string &json, std::vector<point> &result) {
Handler handler(result);
json::sax_parse(json.data(), &handler);
return true;
}
}; // nlohmann_json_sax
BENCHMARK_TEMPLATE(large_random, nlohmann_json_sax)->UseManualTime();
} // namespace large_random
#endif // SIMDJSON_COMPETITION_NLOHMANN_JSON

View File

@ -0,0 +1,69 @@
#pragma once
#ifdef SIMDJSON_COMPETITION_RAPIDJSON
#include "large_random.h"
namespace large_random {
using namespace rapidjson;
struct rapidjson_sax {
static constexpr diff_flags DiffFlags = diff_flags::NONE;
struct Handler {
size_t k{0};
double buffer[3];
std::vector<point>& result;
Handler(std::vector<point> &r) : result(r) { }
bool Key(const char* key, SizeType length, bool copy) {
switch(key[0]) {
case 'x':
k = 0;
break;
case 'y':
k = 1;
break;
case 'z':
k = 2;
break;
}
return true;
}
bool Double(double d) {
buffer[k] = d;
if (k == 2) {
result.emplace_back(json_benchmark::point{buffer[0],buffer[1],buffer[2]});
k = 0;
}
return true;
}
bool Uint(unsigned i) { return Double(i); } // Need this event because coordinate value can be equal to 1
// Irrelevant events
bool Null() { return true; }
bool Bool(bool b) { return true; }
bool Int(int i) { return true; }
bool Int64(int64_t i) { return true; }
bool Uint64(uint64_t i) { return true; }
bool RawNumber(const char* str, SizeType length, bool copy) { return true; }
bool String(const char* str, SizeType length, bool copy) { return true; }
bool StartObject() { return true; }
bool EndObject(SizeType memberCount) { return true; }
bool StartArray() { return true; }
bool EndArray(SizeType elementCount) { return true; }
}; // handler
bool run(simdjson::padded_string &json, std::vector<point> &result) {
Reader reader;
Handler handler(result);
InsituStringStream ss(json.data());
reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag | kParseFullPrecisionFlag>(ss,handler);
return true;
}
}; // rapid_jason_sax
BENCHMARK_TEMPLATE(large_random, rapidjson_sax)->UseManualTime();
} // namespace large_random
#endif // SIMDJSON_COMPETITION_RAPIDJSON