2018-11-21 09:08:02 +08:00
|
|
|
#include <unistd.h>
|
2018-11-30 22:37:57 +08:00
|
|
|
#include "simdjson/jsonparser.h"
|
2018-08-21 05:30:30 +08:00
|
|
|
|
|
|
|
#include "benchmark.h"
|
2018-08-21 05:51:38 +08:00
|
|
|
|
2018-11-30 22:37:57 +08:00
|
|
|
// #define RAPIDJSON_SSE2 // bad for performance
|
|
|
|
// #define RAPIDJSON_SSE42 // bad for performance
|
2018-08-21 05:30:30 +08:00
|
|
|
#include "rapidjson/document.h"
|
2018-11-30 22:37:57 +08:00
|
|
|
#include "rapidjson/reader.h"
|
2018-08-21 05:30:30 +08:00
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include "rapidjson/writer.h"
|
2018-11-30 22:37:57 +08:00
|
|
|
|
2018-11-21 03:09:43 +08:00
|
|
|
#include "json11.cpp"
|
2018-09-28 12:00:52 +08:00
|
|
|
#include "sajson.h"
|
2018-11-21 03:32:12 +08:00
|
|
|
#include "fastjson.cpp"
|
|
|
|
#include "fastjson_dom.cpp"
|
2018-11-21 05:43:22 +08:00
|
|
|
#include "gason.cpp"
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "ultrajsondec.c"
|
|
|
|
#include "ujdecode.h"
|
|
|
|
}
|
2018-08-21 05:30:30 +08:00
|
|
|
using namespace rapidjson;
|
|
|
|
using namespace std;
|
|
|
|
|
2018-11-21 03:32:12 +08:00
|
|
|
|
|
|
|
// fastjson has a tricky interface
|
|
|
|
void on_json_error( void *, const fastjson::ErrorContext& ec) {
|
2018-11-21 09:08:02 +08:00
|
|
|
//std::cerr<<"ERROR: "<<ec.mesg<<std::endl;
|
2018-11-21 03:32:12 +08:00
|
|
|
}
|
|
|
|
bool fastjson_parse(const char *input) {
|
|
|
|
fastjson::Token token;
|
|
|
|
fastjson::dom::Chunk chunk;
|
|
|
|
return fastjson::dom::parse_string(input, &token, &chunk, 0, &on_json_error, NULL);
|
|
|
|
}
|
2018-11-21 05:43:22 +08:00
|
|
|
// end of fastjson stuff
|
2018-11-21 03:32:12 +08:00
|
|
|
|
2018-08-21 05:30:30 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-21 09:08:02 +08:00
|
|
|
bool verbose = false;
|
|
|
|
bool all = false;
|
|
|
|
int c;
|
|
|
|
while ((c = getopt (argc, argv, "va")) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
all = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (optind >= argc) {
|
2018-08-21 05:30:30 +08:00
|
|
|
cerr << "Usage: " << argv[0] << " <jsonfile>\n";
|
|
|
|
cerr << "Or " << argv[0] << " -v <jsonfile>\n";
|
2018-11-21 09:08:02 +08:00
|
|
|
cerr << "To enable parsers that are not standard compliant, use the -a flag\n";
|
2018-08-21 05:30:30 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-21 09:08:02 +08:00
|
|
|
const char * filename = argv[optind];
|
|
|
|
if(optind + 1 < argc) {
|
|
|
|
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
|
2018-08-21 05:30:30 +08:00
|
|
|
}
|
2018-12-01 10:31:05 +08:00
|
|
|
std::string_view p;
|
2018-11-28 03:37:59 +08:00
|
|
|
try {
|
|
|
|
p = get_corpus(filename);
|
|
|
|
} catch (const std::exception& e) { // caught by reference to base
|
|
|
|
std::cout << "Could not load the file " << filename << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-08-21 05:40:50 +08:00
|
|
|
if (verbose) {
|
|
|
|
std::cout << "Input has ";
|
2018-12-01 09:27:16 +08:00
|
|
|
if (p.size() > 1024 * 1024)
|
|
|
|
std::cout << p.size() / (1024 * 1024) << " MB ";
|
|
|
|
else if (p.size() > 1024)
|
|
|
|
std::cout << p.size() / 1024 << " KB ";
|
2018-08-21 05:40:50 +08:00
|
|
|
else
|
2018-12-01 09:27:16 +08:00
|
|
|
std::cout << p.size() << " B ";
|
2018-08-21 05:40:50 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2018-12-01 09:27:16 +08:00
|
|
|
ParsedJson pj;
|
|
|
|
bool allocok = pj.allocateCapacity(p.size(), 1024);
|
|
|
|
|
|
|
|
if (!allocok) {
|
2018-08-21 05:30:30 +08:00
|
|
|
std::cerr << "can't allocate memory" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
int repeat = 10;
|
2018-12-01 09:27:16 +08:00
|
|
|
int volume = p.size();
|
|
|
|
BEST_TIME("simdjson", json_parse(p, pj), true, , repeat, volume, true);
|
2018-12-07 10:44:26 +08:00
|
|
|
BEST_TIME("simdjson (with dyn alloc) ", build_parsed_json(p).isValid(), true, , repeat, volume, true);
|
2018-08-21 05:30:30 +08:00
|
|
|
|
|
|
|
rapidjson::Document d;
|
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
char *buffer = (char *)malloc(p.size() + 1);
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size());
|
2018-12-01 09:27:16 +08:00
|
|
|
buffer[p.size()] = '\0';
|
2018-08-21 05:30:30 +08:00
|
|
|
|
2018-10-24 09:36:32 +08:00
|
|
|
BEST_TIME("RapidJSON",
|
2018-08-21 05:30:30 +08:00
|
|
|
d.Parse<kParseValidateEncodingFlag>((const char *)buffer).HasParseError(),
|
2018-12-01 10:31:05 +08:00
|
|
|
false, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-10-24 09:36:32 +08:00
|
|
|
BEST_TIME("RapidJSON Insitu", d.ParseInsitu<kParseValidateEncodingFlag>(buffer).HasParseError(), false,
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-08-21 05:30:30 +08:00
|
|
|
|
2018-12-01 10:31:05 +08:00
|
|
|
BEST_TIME("sajson (dynamic mem)", sajson::parse(sajson::dynamic_allocation(), sajson::mutable_string_view(p.size(), buffer)).is_valid(), true, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
size_t astbuffersize = p.size();
|
2018-09-28 12:00:52 +08:00
|
|
|
size_t * ast_buffer = (size_t *) malloc(astbuffersize * sizeof(size_t));
|
|
|
|
|
2018-12-01 10:31:05 +08:00
|
|
|
BEST_TIME("sajson (static alloc)", sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize), sajson::mutable_string_view(p.size(), buffer)).is_valid(), true, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-11-21 03:09:43 +08:00
|
|
|
std::string json11err;
|
2018-12-01 10:31:05 +08:00
|
|
|
if(all) BEST_TIME("dropbox (json11) ", (( json11::Json::parse(buffer,json11err).is_null() ) || ( ! json11err.empty() )), false, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-12-01 10:31:05 +08:00
|
|
|
if(all) BEST_TIME("fastjson ", fastjson_parse(buffer), true, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-11-21 05:43:22 +08:00
|
|
|
JsonValue value;
|
|
|
|
JsonAllocator allocator;
|
|
|
|
char *endptr;
|
2018-12-01 10:31:05 +08:00
|
|
|
if(all) BEST_TIME("gason ", jsonParse(buffer, &endptr, &value, allocator), JSON_OK, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-11-21 05:43:22 +08:00
|
|
|
void *state;
|
2018-12-01 10:31:05 +08:00
|
|
|
if(all) BEST_TIME("ultrajson ", (UJDecode(buffer, p.size(), NULL, &state) == NULL), false, memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
|
|
|
BEST_TIME("memcpy ", (memcpy(buffer, p.data(), p.size()) == buffer), true, , repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
free(ast_buffer);
|
2018-11-30 11:15:02 +08:00
|
|
|
free(buffer);
|
2018-08-21 05:30:30 +08:00
|
|
|
}
|