2018-11-28 03:37:59 +08:00
|
|
|
#include <unistd.h>
|
2018-08-21 05:30:00 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "benchmark.h"
|
2018-11-30 22:37:57 +08:00
|
|
|
#include "simdjson/jsonioutil.h"
|
|
|
|
#include "simdjson/jsonminifier.h"
|
|
|
|
#include "simdjson/jsonparser.h"
|
2018-08-21 05:30:00 +08:00
|
|
|
|
|
|
|
// #define RAPIDJSON_SSE2 // bad
|
|
|
|
// #define RAPIDJSON_SSE42 // bad
|
|
|
|
#include "rapidjson/document.h"
|
|
|
|
#include "rapidjson/reader.h" // you have to check in the submodule
|
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include "rapidjson/writer.h"
|
2018-09-28 12:00:52 +08:00
|
|
|
#include "sajson.h"
|
2018-08-21 05:30:00 +08:00
|
|
|
|
2018-11-28 03:37:59 +08:00
|
|
|
|
2018-08-21 05:30:00 +08:00
|
|
|
using namespace rapidjson;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
std::string rapidstringmeInsitu(char *json) {
|
|
|
|
Document d;
|
|
|
|
d.ParseInsitu(json);
|
|
|
|
if (d.HasParseError()) {
|
|
|
|
std::cerr << "problem!" << std::endl;
|
|
|
|
return ""; // should do something
|
|
|
|
}
|
|
|
|
StringBuffer buffer;
|
|
|
|
Writer<StringBuffer> writer(buffer);
|
|
|
|
d.Accept(writer);
|
|
|
|
return buffer.GetString();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rapidstringme(char *json) {
|
|
|
|
Document d;
|
|
|
|
d.Parse(json);
|
|
|
|
if (d.HasParseError()) {
|
|
|
|
std::cerr << "problem!" << std::endl;
|
|
|
|
return ""; // should do something
|
|
|
|
}
|
|
|
|
StringBuffer buffer;
|
|
|
|
Writer<StringBuffer> writer(buffer);
|
|
|
|
d.Accept(writer);
|
|
|
|
return buffer.GetString();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-28 03:37:59 +08:00
|
|
|
int c;
|
|
|
|
bool verbose = false;
|
|
|
|
while ((c = getopt (argc, argv, "v")) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (optind >= argc) {
|
|
|
|
cerr << "Usage: " << argv[0] << " <jsonfile>" << endl;
|
2018-08-21 05:30:00 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-28 03:37:59 +08:00
|
|
|
const char * filename = argv[optind];
|
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:30:00 +08:00
|
|
|
}
|
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
|
|
|
char *buffer = allocate_aligned_buffer(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:00 +08:00
|
|
|
|
|
|
|
int repeat = 10;
|
2018-12-01 09:27:16 +08:00
|
|
|
int volume = p.size();
|
2018-08-21 05:30:00 +08:00
|
|
|
|
2018-12-01 10:31:05 +08:00
|
|
|
size_t strlength = rapidstringme((char *)p.data()).size();
|
2018-08-21 05:40:50 +08:00
|
|
|
if (verbose)
|
2018-12-01 09:27:16 +08:00
|
|
|
std::cout << "input length is " << p.size() << " stringified length is "
|
2018-08-21 05:40:50 +08:00
|
|
|
<< strlength << std::endl;
|
2018-12-01 10:31:05 +08:00
|
|
|
BEST_TIME_NOCHECK("despacing with RapidJSON", rapidstringme((char *)p.data()), , repeat, volume, true);
|
2018-10-24 09:36:32 +08:00
|
|
|
BEST_TIME_NOCHECK("despacing with RapidJSON Insitu", rapidstringmeInsitu((char *)buffer),
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
2018-08-21 05:30:00 +08:00
|
|
|
|
|
|
|
size_t outlength =
|
2018-12-01 09:27:16 +08:00
|
|
|
jsonminify((const uint8_t *)buffer, p.size(), (uint8_t *)buffer);
|
2018-08-21 05:40:50 +08:00
|
|
|
if (verbose)
|
|
|
|
std::cout << "jsonminify length is " << outlength << std::endl;
|
2018-08-21 05:30:00 +08:00
|
|
|
|
|
|
|
uint8_t *cbuffer = (uint8_t *)buffer;
|
2018-12-01 09:27:16 +08:00
|
|
|
BEST_TIME("jsonminify", jsonminify(cbuffer, p.size(), cbuffer), outlength,
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size()), repeat, volume, true);
|
2018-12-01 09:27:16 +08:00
|
|
|
printf("minisize = %zu, original size = %zu (minified down to %.2f percent of original) \n", outlength, p.size(), outlength * 100.0 / p.size());
|
2018-08-21 05:30:00 +08:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Is it worth it to minify before parsing?
|
|
|
|
***/
|
|
|
|
rapidjson::Document d;
|
2018-10-24 09:36:32 +08:00
|
|
|
BEST_TIME("RapidJSON Insitu orig", d.ParseInsitu(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:00 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
char *minibuffer = allocate_aligned_buffer(p.size() + 1);
|
2018-12-01 10:31:05 +08:00
|
|
|
size_t minisize = jsonminify((const uint8_t *)p.data(), p.size(), (uint8_t*) minibuffer);
|
2018-09-28 12:00:52 +08:00
|
|
|
minibuffer[minisize] = '\0';
|
2018-10-24 09:36:32 +08:00
|
|
|
|
|
|
|
BEST_TIME("RapidJSON Insitu despaced", d.ParseInsitu(buffer).HasParseError(), false,
|
2018-12-01 09:27:16 +08:00
|
|
|
memcpy(buffer, minibuffer, p.size()),
|
2018-08-21 05:30:00 +08:00
|
|
|
repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
size_t astbuffersize = p.size() * 2;
|
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 orig", 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-09-28 12:00:52 +08:00
|
|
|
|
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
BEST_TIME("sajson despaced", sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize), sajson::mutable_string_view(minisize, buffer)).is_valid(), true, memcpy(buffer, minibuffer, p.size()), repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
ParsedJson pj;
|
|
|
|
pj.allocateCapacity(p.size(), 1024);
|
2018-12-01 10:31:05 +08:00
|
|
|
BEST_TIME("json_parse orig", json_parse((const u8*)buffer, p.size(), pj), 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
|
|
|
ParsedJson pj2;
|
|
|
|
pj2.allocateCapacity(p.size(), 1024);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-10-24 09:36:32 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
BEST_TIME("json_parse despaced", json_parse((const u8*)buffer, minisize, pj2), true, memcpy(buffer, minibuffer, p.size()), repeat, volume, true);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-08-21 05:30:00 +08:00
|
|
|
free(buffer);
|
2018-09-28 12:00:52 +08:00
|
|
|
free(ast_buffer);
|
|
|
|
free(minibuffer);
|
|
|
|
|
|
|
|
|
2018-08-21 05:30:00 +08:00
|
|
|
}
|