Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch the simdjson code to On Demand #272

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion json/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,16 @@ json-scala/target/application.jar:
$(MAKE) -C json-scala target/application.jar

simdjson-dir := target/simdjson

# We specify the simdjson commit to ensure reproducible builds.
$(simdjson-dir): | target
$(GIT_CLONE) "https://github.com/simdjson/simdjson.git" $@ \
&& cd $@/singleheader \
&& git checkout bb2bc98a22b68471f193a3f3a60effd9d15183e2 \
lemire marked this conversation as resolved.
Show resolved Hide resolved
&& ./amalgamate.sh

target/json_simdjson_cpp: test_simdjson.cpp | $(simdjson-dir) libnotify
$(GCC_CPP_BUILD) $(simdjson-dir)/singleheader/simdjson.cpp \
$(GCC_CPP_BUILD) -march=native $(simdjson-dir)/singleheader/simdjson.cpp \
lemire marked this conversation as resolved.
Show resolved Hide resolved
-I$(simdjson-dir)/singleheader/

target/json_v_gcc: test.v | $(v_fmt)
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);
lemire marked this conversation as resolved.
Show resolved Hide resolved
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");
}