From dcba8efc837f42a6cb6e8c40f9abab741f6f8bf4 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Tue, 12 Oct 2021 10:10:45 +0100 Subject: [PATCH] All experimentation on the effect of JSON parser reuser. Signed-off-by: Adrian Conlon --- M6502/HarteTest_6502/opcode_test_suite_t.cpp | 15 +++++++++++---- M6502/HarteTest_6502/opcode_test_suite_t.h | 11 ++++++++++- M6502/HarteTest_6502/stdafx.h | 2 ++ M6502/HarteTest_6502/tests.cpp | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/M6502/HarteTest_6502/opcode_test_suite_t.cpp b/M6502/HarteTest_6502/opcode_test_suite_t.cpp index a109a38..8e9ed14 100644 --- a/M6502/HarteTest_6502/opcode_test_suite_t.cpp +++ b/M6502/HarteTest_6502/opcode_test_suite_t.cpp @@ -6,11 +6,13 @@ #include #include +#ifdef JSON_PREFER_REUSE_OF_PARSER #ifdef USE_JSONCPP_JSON std::unique_ptr opcode_test_suite_t::m_parser; #endif #ifdef USE_SIMDJSON_JSON -std::unique_ptr 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 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(); const auto contents = read(path()); - m_raw = m_parser->parse(contents); + m_raw = m_parser.parse(contents); } #endif diff --git a/M6502/HarteTest_6502/opcode_test_suite_t.h b/M6502/HarteTest_6502/opcode_test_suite_t.h index 0e2ba26..5413867 100644 --- a/M6502/HarteTest_6502/opcode_test_suite_t.h +++ b/M6502/HarteTest_6502/opcode_test_suite_t.h @@ -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 m_parser; #endif #ifdef USE_SIMDJSON_JSON - static std::unique_ptr 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: diff --git a/M6502/HarteTest_6502/stdafx.h b/M6502/HarteTest_6502/stdafx.h index 2233df9..8415d3b 100644 --- a/M6502/HarteTest_6502/stdafx.h +++ b/M6502/HarteTest_6502/stdafx.h @@ -27,10 +27,12 @@ #endif #ifdef USE_JSONCPP_JSON +# define JSON_PREFER_REUSE_OF_PARSER # include #endif #ifdef USE_SIMDJSON_JSON # define JSON_PREFER_PASS_BY_VALUE +# define JSON_PREFER_REUSE_OF_PARSER # include "simdjson/simdjson.h" #endif diff --git a/M6502/HarteTest_6502/tests.cpp b/M6502/HarteTest_6502/tests.cpp index 2745f11..b6bfa7c 100644 --- a/M6502/HarteTest_6502/tests.cpp +++ b/M6502/HarteTest_6502/tests.cpp @@ -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;