All experimentation on the effect of JSON parser reuser.

Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2021-10-12 10:10:45 +01:00
parent 31c3a57485
commit dcba8efc83
4 changed files with 24 additions and 6 deletions

View File

@ -6,11 +6,13 @@
#include <fstream>
#include <filesystem>
#ifdef JSON_PREFER_REUSE_OF_PARSER
#ifdef USE_JSONCPP_JSON
std::unique_ptr<Json::CharReader> opcode_test_suite_t::m_parser;
#endif
#ifdef USE_SIMDJSON_JSON
std::unique_ptr<simdjson::dom::parser> opcode_test_suite_t::m_parser;
simdjson::dom::parser opcode_test_suite_t::m_parser;
#endif
#endif
std::string opcode_test_suite_t::read(std::string path) {
@ -50,11 +52,18 @@ void opcode_test_suite_t::load() {
#ifdef USE_JSONCPP_JSON
void opcode_test_suite_t::load() {
#ifdef JSON_PREFER_REUSE_OF_PARSER
if (m_parser == nullptr)
m_parser.reset(Json::CharReaderBuilder().newCharReader());
const auto contents = read(path());
if (!m_parser->parse(contents.data(), contents.data() + contents.size(), &m_raw, nullptr))
throw std::runtime_error("Unable to parse tests");
#else
const auto contents = read(path());
std::unique_ptr<Json::CharReader> parser(Json::CharReaderBuilder().newCharReader());
if (!parser->parse(contents.data(), contents.data() + contents.size(), &m_raw, nullptr))
throw std::runtime_error("Unable to parse tests");
#endif
}
#endif
@ -62,10 +71,8 @@ void opcode_test_suite_t::load() {
#ifdef USE_SIMDJSON_JSON
void opcode_test_suite_t::load() {
if (m_parser == nullptr)
m_parser = std::make_unique<simdjson::dom::parser>();
const auto contents = read(path());
m_raw = m_parser->parse(contents);
m_raw = m_parser.parse(contents);
}
#endif

View File

@ -21,11 +21,16 @@
class opcode_test_suite_t final {
private:
#ifdef JSON_PREFER_REUSE_OF_PARSER
#ifdef USE_JSONCPP_JSON
static std::unique_ptr<Json::CharReader> m_parser;
#endif
#ifdef USE_SIMDJSON_JSON
static std::unique_ptr<simdjson::dom::parser> m_parser;
// N.B.
// The parser must be kept for the lifetime of any parsed data.
// Therefore, it can only be used for one document at a time.
static simdjson::dom::parser m_parser;
#endif
#endif
[[nodiscard]] static std::string read(std::string path);
@ -42,6 +47,10 @@ private:
#endif
#ifdef USE_SIMDJSON_JSON
simdjson::dom::element m_raw;
#ifndef JSON_PREFER_REUSE_OF_PARSER
// N.B. The parser must be kept for the lifetime of any parsed data.
simdjson::dom::parser m_parser;
#endif
#endif
public:

View File

@ -27,10 +27,12 @@
#endif
#ifdef USE_JSONCPP_JSON
# define JSON_PREFER_REUSE_OF_PARSER
# include <json/json.h>
#endif
#ifdef USE_SIMDJSON_JSON
# define JSON_PREFER_PASS_BY_VALUE
# define JSON_PREFER_REUSE_OF_PARSER
# include "simdjson/simdjson.h"
#endif

View File

@ -35,7 +35,7 @@ int main() {
#endif
#ifdef USE_SIMDJSON_JSON
assert(opcode.raw().is_array());
const auto& opcode_test_array = opcode.raw().get_array();
const auto opcode_test_array = opcode.raw().get_array();
#endif
bool opcode_bad = false;