Repository navigation
Expand file tree
/
Copy pathminifiercompetition.cpp
More file actions
141 lines (117 loc) · 4.4 KB
/
Copy pathminifiercompetition.cpp
File metadata and controls
141 lines (117 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include "benchmark.h"
#include "jsonparser/jsonioutil.h"
#include "jsonparser/jsonminifier.h"
#include "jsonparser/jsonparser.h"
// #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"
#include "sajson.h"
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[]) {
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <jsonfile>\n";
cerr << "Or " << argv[0] << " -v <jsonfile>\n";
exit(1);
}
bool verbose = false;
if (argc > 2) {
if (strcmp(argv[1], "-v"))
verbose = true;
}
pair<u8 *, size_t> p = get_corpus(argv[argc - 1]);
if (verbose) {
std::cout << "Input has ";
if (p.second > 1024 * 1024)
std::cout << p.second / (1024 * 1024) << " MB ";
else if (p.second > 1024)
std::cout << p.second / 1024 << " KB ";
else
std::cout << p.second << " B ";
std::cout << std::endl;
}
char *buffer = (char *)malloc(p.second + 1);
memcpy(buffer, p.first, p.second);
buffer[p.second] = '\0';
int repeat = 10;
int volume = p.second;
size_t strlength = rapidstringme((char *)p.first).size();
if (verbose)
std::cout << "input length is " << p.second << " stringified length is "
<< strlength << std::endl;
BEST_TIME_NOCHECK(rapidstringme((char *)p.first), , repeat, volume, true);
BEST_TIME_NOCHECK(rapidstringmeInsitu((char *)buffer),
memcpy(buffer, p.first, p.second), repeat, volume, true);
memcpy(buffer, p.first, p.second);
size_t outlength =
jsonminify((const uint8_t *)buffer, p.second, (uint8_t *)buffer);
if (verbose)
std::cout << "jsonminify length is " << outlength << std::endl;
uint8_t *cbuffer = (uint8_t *)buffer;
BEST_TIME(jsonminify(cbuffer, p.second, cbuffer), outlength,
memcpy(buffer, p.first, p.second), repeat, volume, true);
/***
* Is it worth it to minify before parsing?
***/
rapidjson::Document d;
printf("\n");
printf("parsing with RapidJSON before despacing:\n");
BEST_TIME(d.ParseInsitu(buffer).HasParseError(), false,
memcpy(buffer, p.first, p.second), repeat, volume, true);
printf("parsing with RapidJSON after despacing:\n");
char *minibuffer = (char *)malloc(p.second + 1);
size_t minisize = jsonminify((const uint8_t *)p.first, p.second, (uint8_t*) minibuffer);
minibuffer[minisize] = '\0';
BEST_TIME(d.ParseInsitu(buffer).HasParseError(), false,
memcpy(buffer, minibuffer, p.second),
repeat, volume, true);
printf("\n");
size_t astbuffersize = p.second * 2;
size_t * ast_buffer = (size_t *) malloc(astbuffersize * sizeof(size_t));
printf("parsing with sajson before despacing:\n");
BEST_TIME(sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize), sajson::mutable_string_view(p.second, buffer)).is_valid(), true, memcpy(buffer, p.first, p.second), repeat, volume, true);
printf("parsing with sajson after despacing:\n");
BEST_TIME(sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize), sajson::mutable_string_view(minisize, buffer)).is_valid(), true, memcpy(buffer, minibuffer, p.second), repeat, volume, true);
printf("parsing before despacing:\n");
ParsedJson *pj_ptr = allocate_ParsedJson(p.second);
ParsedJson &pj(*pj_ptr);
BEST_TIME(json_parse(p.first, p.second, pj), true, , repeat, volume, true);
printf("parsing after despacing:\n");
ParsedJson *pj_ptr2 = allocate_ParsedJson(minisize);
ParsedJson &pj2(*pj_ptr2);
BEST_TIME(json_parse((const u8*)minibuffer, minisize, pj2), true, , repeat, volume, true);
free(buffer);
free(p.first);
free(ast_buffer);
free(minibuffer);
deallocate_ParsedJson(pj_ptr);
deallocate_ParsedJson(pj_ptr2);
}