simdjson/benchmark/find_tweet/rapidjson.h

51 lines
1.6 KiB
C
Raw Normal View History

2021-01-05 07:22:35 +08:00
#pragma once
#ifdef SIMDJSON_COMPETITION_RAPIDJSON
#include "find_tweet.h"
namespace find_tweet {
using namespace rapidjson;
struct rapidjson_base {
2021-01-05 07:22:35 +08:00
Document doc{};
2021-01-06 03:31:34 +08:00
bool run(Document &root, uint64_t find_id, std::string_view &result) {
2021-01-05 07:22:35 +08:00
if (root.HasParseError() || !root.IsObject()) { return false; }
auto statuses = root.FindMember("statuses");
if (statuses == root.MemberEnd() || !statuses->value.IsArray()) { return false; }
for (auto &tweet : statuses->value.GetArray()) {
if (!tweet.IsObject()) { return false; }
auto id = tweet.FindMember("id");
if (id == tweet.MemberEnd() || !id->value.IsUint64()) { return false; }
if (id->value.GetUint64() == find_id) {
2021-01-06 03:31:34 +08:00
auto text = tweet.FindMember("text");
if (text == tweet.MemberEnd() || !text->value.IsString()) { return false; }
result = { text->value.GetString(), text->value.GetStringLength() };
2021-01-05 07:22:35 +08:00
return true;
}
}
return false;
}
};
2021-01-06 03:41:07 +08:00
struct rapidjson : rapidjson_base {
2021-01-06 03:31:34 +08:00
bool run(simdjson::padded_string &json, uint64_t find_id, std::string_view &result) {
return rapidjson_base::run(doc.Parse<kParseValidateEncodingFlag>(json.data()), find_id, result);
}
};
2021-01-06 02:45:49 +08:00
BENCHMARK_TEMPLATE(find_tweet, rapidjson)->UseManualTime();
2021-01-05 07:22:35 +08:00
2021-01-06 03:41:07 +08:00
struct rapidjson_insitu : rapidjson_base {
2021-01-06 03:31:34 +08:00
bool run(simdjson::padded_string &json, uint64_t find_id, std::string_view &result) {
return rapidjson_base::run(doc.ParseInsitu<kParseValidateEncodingFlag>(json.data()), find_id, result);
}
};
2021-01-06 02:45:49 +08:00
BENCHMARK_TEMPLATE(find_tweet, rapidjson_insitu)->UseManualTime();
2021-01-05 07:22:35 +08:00
} // namespace partial_tweets
#endif // SIMDJSON_COMPETITION_RAPIDJSON