More micro-optimisations.

This commit is contained in:
Adrian Conlon 2021-10-18 20:40:13 +01:00
parent 357e51c09a
commit dc0fc262a2
7 changed files with 36 additions and 29 deletions

View File

@ -31,9 +31,10 @@ cycle_t::cycle_t(simdjson::dom::element input) noexcept
: cycle_t(input.get_array()) {}
cycle_t::cycle_t(simdjson::dom::array input) noexcept
: m_address((uint16_t)(uint64_t)input.at(0)),
m_value((uint8_t)(uint64_t)input.at(1)),
m_action(to_action((std::string)input.at(2))) {
: m_iterator(input.begin()),
m_address((uint16_t)(uint64_t)*m_iterator),
m_value((uint8_t)(uint64_t)*++m_iterator),
m_action(to_action((std::string)*++m_iterator)) {
assert(input.size() == 3);
}
@ -41,13 +42,13 @@ cycle_t::cycle_t(simdjson::dom::array input) noexcept
#ifdef USE_BOOST_JSON
cycle_t::cycle_t(const boost::json::value& input)
: cycle_t(input.as_array()) {}
cycle_t::cycle_t(const boost::json::value& input) noexcept
: cycle_t(input.get_array()) {}
cycle_t::cycle_t(const boost::json::array& input)
: m_address((uint16_t)input.at(0).as_int64()),
m_value((uint8_t)input.at(1).as_int64()),
m_action(to_action((std::string)input.at(2).as_string())) {
cycle_t::cycle_t(const boost::json::array& input) noexcept
: m_address((uint16_t)input[0].get_int64()),
m_value((uint8_t)input[1].get_int64()),
m_action(to_action((std::string)input[2].get_string())) {
assert(input.size() == 3);
};
@ -55,7 +56,7 @@ cycle_t::cycle_t(const boost::json::array& input)
#ifdef USE_NLOHMANN_JSON
cycle_t::cycle_t(const nlohmann::json& input)
cycle_t::cycle_t(const nlohmann::json& input) noexcept
: m_address(input[0].get<uint16_t>()),
m_value(input[1].get<uint8_t>()),
m_action(to_action(input[2].get<std::string>())) {

View File

@ -24,6 +24,9 @@ public:
enum class action_t { read, write, unknown };
private:
#ifdef USE_SIMDJSON_JSON
simdjson::dom::array::iterator m_iterator;
#endif
uint16_t m_address = 0xffff;
uint8_t m_value = 0xff;
action_t m_action = action_t::unknown;
@ -40,12 +43,12 @@ public:
#endif
#ifdef USE_BOOST_JSON
cycle_t(const boost::json::value& input);
cycle_t(const boost::json::array& input);
cycle_t(const boost::json::value& input) noexcept;
cycle_t(const boost::json::array& input) noexcept;
#endif
#ifdef USE_NLOHMANN_JSON
cycle_t(const nlohmann::json& input);
cycle_t(const nlohmann::json& input) noexcept;
#endif
#ifdef USE_JSONCPP_JSON

View File

@ -24,7 +24,7 @@ cycles_t::cycles_t(simdjson::dom::array input) {
#ifdef USE_BOOST_JSON
cycles_t::cycles_t(const boost::json::value& input)
: cycles_t(input.as_array()) {}
: cycles_t(input.get_array()) {}
cycles_t::cycles_t(const boost::json::array& input) {
assert(m_cycles.empty());

View File

@ -9,8 +9,9 @@ ram_t::ram_t(simdjson::dom::array input) {
assert(byte.is_array());
const auto ram_entry_array = byte.get_array();
assert(byte.size() == 2);
const auto address = (uint16_t)(uint64_t)byte.at(0);
const auto value = (uint8_t)(uint64_t)byte.at(1);
auto iterator = byte.begin();
const auto address = (uint16_t)(uint64_t)*iterator;
const auto value = (uint8_t)(uint64_t)*++iterator;
m_bytes.push_back({ address, value });
}
}
@ -22,16 +23,16 @@ ram_t::ram_t(simdjson::dom::array input) {
ram_t::ram_t(const boost::json::array& input) {
m_bytes.reserve(input.size());
for (const auto& byte : input) {
const auto& ram_entry_array = byte.as_array();
const auto& ram_entry_array = byte.get_array();
assert(ram_entry_array.size() == 2);
const auto address = (uint16_t)ram_entry_array.at(0).as_int64();
const auto value = (uint8_t)ram_entry_array.at(1).as_int64();
const auto address = (uint16_t)ram_entry_array[0].get_int64();
const auto value = (uint8_t)ram_entry_array[1].get_int64();
m_bytes.push_back({ address, value });
}
}
ram_t::ram_t(const boost::json::value& input)
: ram_t(input.as_array()) {}
: ram_t(input.get_array()) {}
#endif
@ -39,6 +40,7 @@ ram_t::ram_t(const boost::json::value& input)
ram_t::ram_t(const nlohmann::json& input) {
assert(input.is_array());
m_bytes.reserve(input.size());
for (const auto& byte : input) {
assert(byte.is_array());
assert(byte.size() == 2);
@ -54,6 +56,7 @@ ram_t::ram_t(const nlohmann::json& input) {
ram_t::ram_t(const Json::Value& input) {
assert(input.isArray());
m_bytes.reserve(input.size());
for (const auto& byte : input) {
assert(byte.isArray());
assert(byte.size() == 2);

View File

@ -18,12 +18,12 @@ state_t::state_t(const simdjson::dom::element serialised)
#ifdef USE_BOOST_JSON
state_t::state_t(const boost::json::object& serialised)
: m_pc((uint16_t)serialised.at("pc").as_int64()),
m_s((uint8_t)serialised.at("s").as_int64()),
m_a((uint8_t)serialised.at("a").as_int64()),
m_x((uint8_t)serialised.at("x").as_int64()),
m_y((uint8_t)serialised.at("y").as_int64()),
m_p((uint8_t)serialised.at("p").as_int64()),
: m_pc((uint16_t)serialised.at("pc").get_int64()),
m_s((uint8_t)serialised.at("s").get_int64()),
m_a((uint8_t)serialised.at("a").get_int64()),
m_x((uint8_t)serialised.at("x").get_int64()),
m_y((uint8_t)serialised.at("y").get_int64()),
m_p((uint8_t)serialised.at("p").get_int64()),
m_ram(serialised.at("ram")) {}
state_t::state_t(const boost::json::value& serialised)

View File

@ -15,13 +15,13 @@ test_t::test_t(const simdjson::dom::element serialised)
#ifdef USE_BOOST_JSON
test_t::test_t(const boost::json::object& serialised)
: m_name(serialised.at("name").as_string()),
: m_name(serialised.at("name").get_string()),
m_initial_state(serialised.at("initial")),
m_final_state(serialised.at("final")),
m_cycles(serialised.at("cycles")) {}
test_t::test_t(const boost::json::value& serialised)
: test_t(serialised.as_object()) {}
: test_t(serialised.get_object()) {}
#endif

View File

@ -27,7 +27,7 @@ int main() {
const auto opcode_test_array = opcode.raw().get_array();
#endif
#ifdef USE_BOOST_JSON
const auto& opcode_test_array = opcode.raw().as_array();
const auto& opcode_test_array = opcode.raw().get_array();
#endif
#ifdef USE_NLOHMANN_JSON
const auto& opcode_test_array = opcode.raw();