From 07df4054da304e8079aa258dd4db31def0ea9363 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 18 Jul 2022 15:12:12 +0200 Subject: [PATCH] Util: Massively speedup serialization, remove all ifs from loop --- src/util/json/serializer.cpp | 96 ++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/src/util/json/serializer.cpp b/src/util/json/serializer.cpp index 2bf681a..9f1ce1d 100644 --- a/src/util/json/serializer.cpp +++ b/src/util/json/serializer.cpp @@ -67,66 +67,88 @@ void Serializer::dumpHelper(const Value& value, const uint32_t indentLevel) void Serializer::dumpArray(const Value& value, const uint32_t indentLevel) { - // Append [ - m_output += "["; if (m_indent > 0) { + m_output += '['; m_output += '\n'; } - auto values = value.m_value.array->elements(); - auto last = values.end(); - for (auto it = values.begin(); it != last; ++it) { - m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter); - dumpHelper(*it, indentLevel + 1); + // Empty Array early return + if (value.m_value.array->empty()) { + m_output += ']'; + return; + } - // Add comma, except after the last element - if (std::next(it) != last) { - m_output += ","; - } - if (m_indent > 0) { - m_output += '\n'; + size_t i = 0; + auto it = value.m_value.array->elements().cbegin(); + if (!m_indent) { + for (; i < value.m_value.array->size() - 1; ++i, ++it) { + dumpHelper(*it, indentLevel + 1); + m_output += ','; } + dumpHelper(*it, indentLevel + 1); } + else { + std::string indentation = std::string(m_indent * (indentLevel + 1), m_indentCharacter); - // Append indentation - m_output += std::string(m_indent * indentLevel, m_indentCharacter); + for (; i < value.m_value.array->size() - 1; ++i, ++it) { + m_output += indentation; + dumpHelper(*it, indentLevel + 1); + m_output += ",\n"; + } + m_output += indentation; + dumpHelper(*it, indentLevel + 1); + m_output += '\n'; + + // Append indentation + m_output += std::string(m_indent * indentLevel, m_indentCharacter); + } - // Append ] m_output += "]"; } void Serializer::dumpObject(const Value& value, const uint32_t indentLevel) { - // Append { - m_output += "{"; if (m_indent > 0) { + m_output += '{'; m_output += '\n'; } - auto members = value.m_value.object->members(); - auto last = members.end(); - for (auto it = members.begin(); it != last; ++it) { - m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter); - m_output += "\"" + it->first + "\":"; - if (m_indent > 0) { - m_output += ' '; - } - dumpHelper(it->second, indentLevel + 1); + // Empty Object early return + if (value.m_value.object->empty()) { + m_output += '}'; + return; + } - // Add comma, except after the last element - if (std::next(it) != last) { - m_output += ","; - } - if (m_indent > 0) { - m_output += '\n'; + size_t i = 0; + auto it = value.m_value.object->members().cbegin(); + if (!m_indent) { + for (; i < value.m_value.object->size() - 1; ++i, ++it) { + m_output += '"' + it->first + "\":"; + dumpHelper(it->second, indentLevel + 1); + m_output += ','; } + m_output += '"' + it->first + "\":"; + dumpHelper(it->second, indentLevel + 1); } + else { + std::string indentation = std::string(m_indent * (indentLevel + 1), m_indentCharacter); + + for (; i < value.m_value.object->size() - 1; ++i, ++it) { + m_output += indentation; + m_output += '"' + it->first + "\": "; + dumpHelper(it->second, indentLevel + 1); + m_output += ",\n"; + } + m_output += indentation; + m_output += '"' + it->first + "\": "; + dumpHelper(it->second, indentLevel + 1); + m_output += '\n'; - // Append indentation - m_output += std::string(m_indent * indentLevel, m_indentCharacter); + // Append indentation + m_output += std::string(m_indent * indentLevel, m_indentCharacter); + } - // Append } - m_output += "}"; + m_output += '}'; } } // namespace Json