Skip to content

Commit

Permalink
Switch the simdjson code to On Demand (#272)
Browse files Browse the repository at this point in the history
* Updating simdjson to 0.6 API (on demand).

* Making things safer by specifying the commit + removing unnecessary std:: qualifiers.

* Minor changes following review
  • Loading branch information
lemire committed Oct 13, 2020
1 parent f479fe3 commit e9ac736
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
2 changes: 1 addition & 1 deletion common/commands.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GCC_FLAGS := -O3 -Wall -flto -Wa,-mbranches-within-32B-boundaries
GCC_FLAGS := -O3 -march=native -Wall -flto -Wa,-mbranches-within-32B-boundaries
CLANG_FLAGS := -O3 -mbranches-within-32B-boundaries
LIBNOTIFY_FLAGS := -I../common/libnotify ../common/libnotify/target/libnotify.a
NIM_FLAGS := -d:danger --verbosity:0 --opt:speed --hints:off
Expand Down
1 change: 1 addition & 0 deletions json/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ json-scala/target/application.jar:
$(MAKE) -C json-scala target/application.jar

simdjson-dir := target/simdjson

$(simdjson-dir): | target
$(GIT_CLONE) "https://github.com/simdjson/simdjson.git" $@ \
&& cd $@/singleheader \
Expand Down
77 changes: 33 additions & 44 deletions json/test_simdjson.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <iostream>
#include <libnotify.hpp>
#include "simdjson.h"
#include <sstream>
#include <unistd.h>
#include <fstream>

using namespace simdjson;
#include "simdjson.h"

using namespace std;

struct coordinate_t {
Expand All @@ -23,63 +23,52 @@ struct coordinate_t {
}
};

string read_file(const string& filename) {
ifstream f(filename);
if (!f) {
return {};
}
return string(istreambuf_iterator<char>(f),
istreambuf_iterator<char>());
}

coordinate_t calc(const string& text) {
dom::parser pj(0);
auto allocate_error = pj.allocate(text.size()); // allocate memory for parsing up to p.size() bytes
if (allocate_error) {
cerr << allocate_error << endl;
exit(EXIT_FAILURE);
}

auto [doc, error] = pj.parse(text); // do the parsing, return 0 on success
if (error) {
cerr << error << endl;
exit(EXIT_FAILURE);
}

auto x = 0.0, y = 0.0, z = 0.0;
auto len = 0;

for (auto coord : doc["coordinates"]) {
double x_coord = coord["x"];
x += x_coord;

double y_coord = coord["y"];
y += y_coord;

double z_coord = coord["z"];
z += z_coord;
using namespace simdjson;
using namespace simdjson::builtin;

class on_demand {
public:
bool run(const padded_string &json);
coordinate_t my_point{};
size_t count{};
private:
ondemand::parser parser{};
};

len++;
bool on_demand::run(const padded_string &json) {
count = 0;
auto doc = parser.iterate(json);
for (ondemand::object point_object : doc["coordinates"]) {
my_point.x += double(point_object["x"]);
my_point.y += double(point_object["y"]);
my_point.z += double(point_object["z"]);
count++;
}
return true;
}

return coordinate_t(x / len, y / len, z / len);
coordinate_t calc(const simdjson::padded_string& json) {
on_demand reader;
reader.run(json);
reader.my_point.x /= reader.count;
reader.my_point.y /= reader.count;
reader.my_point.z /= reader.count;
return reader.my_point;
}

int main() {
auto left = calc("{\"coordinates\":[{\"x\":1.1,\"y\":2.2,\"z\":3.3}]}");
auto left = calc("{\"coordinates\":[{\"x\":1.1,\"y\":2.2,\"z\":3.3}]}"_padded); // The _padded suffix creates a simdjson::padded_string instance
auto right = coordinate_t(1.1, 2.2, 3.3);
if (left != right) {
cerr << left << " != " << right << endl;
exit(EXIT_FAILURE);
}

auto text = read_file("/tmp/1.json");

auto [text, error] = simdjson::padded_string::load("/tmp/1.json");
if(error) { cerr << "could not load file" << endl; return EXIT_FAILURE; }
stringstream ostr;
ostr << "C++/g++ (simdjson)\t" << getpid();
notify(ostr.str());

cout << calc(text) << endl;

notify("stop");
}

0 comments on commit e9ac736

Please sign in to comment.