From f620d74919956105d880c47865ce5f12872b61e3 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 22 Nov 2016 07:26:11 +0100 Subject: [PATCH] :zap: added performance fixes (#365) --- src/json.hpp | 37 ++++++++++++++++++++++--------------- src/json.hpp.re2c | 37 ++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index e71ffc4a..a4a3fef5 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8734,10 +8734,10 @@ basic_json_parser_66: { // copy unprocessed characters to line buffer m_line_buffer.clear(); - for (m_cursor = m_start; m_cursor != m_limit; ++m_cursor) - { - m_line_buffer.append(1, static_cast(*m_cursor)); - } + m_line_buffer.append( + reinterpret_cast(m_start), + static_cast(m_limit - m_start)); + m_cursor = m_limit; } // append n characters to make sure that there is sufficient @@ -8750,10 +8750,12 @@ basic_json_parser_66: // delete processed characters from line buffer m_line_buffer.erase(0, static_cast(offset_start)); // read next line from input stream - std::string line; - std::getline(*m_stream, line, '\n'); + m_line_buffer_tmp.clear(); + std::getline(*m_stream, m_line_buffer_tmp, '\n'); + // add line with newline symbol to the line buffer - m_line_buffer += line + "\n"; + m_line_buffer += m_line_buffer_tmp; + m_line_buffer.push_back('\n'); } // set pointers @@ -8840,9 +8842,18 @@ basic_json_parser_66: // iterate the result between the quotes for (const lexer_char_t* i = m_start + 1; i < m_cursor - 1; ++i) { - // process escaped characters - if (*i == '\\') + // number of non-escaped characters + const size_t n = static_cast(std::find(i, m_cursor - 1, '\\') - i); + + if (n != 0) { + result.append(reinterpret_cast(i), n); + i += n - 1; // -1 because will ++i + } + else + { + // processing escaped character + // read next character ++i; @@ -8929,12 +8940,6 @@ basic_json_parser_66: } } } - else - { - // all other characters are just copied to the end of the - // string - result.append(1, static_cast(*i)); - } } return result; @@ -9118,6 +9123,8 @@ basic_json_parser_66: std::istream* m_stream = nullptr; /// line buffer buffer for m_stream string_t m_line_buffer {}; + /// used for filling m_line_buffer + string_t m_line_buffer_tmp {}; /// the buffer pointer const lexer_char_t* m_content = nullptr; /// pointer to the beginning of the current symbol diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 9eccc144..cb70535a 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -7883,10 +7883,10 @@ class basic_json { // copy unprocessed characters to line buffer m_line_buffer.clear(); - for (m_cursor = m_start; m_cursor != m_limit; ++m_cursor) - { - m_line_buffer.append(1, static_cast(*m_cursor)); - } + m_line_buffer.append( + reinterpret_cast(m_start), + static_cast(m_limit - m_start)); + m_cursor = m_limit; } // append n characters to make sure that there is sufficient @@ -7899,10 +7899,12 @@ class basic_json // delete processed characters from line buffer m_line_buffer.erase(0, static_cast(offset_start)); // read next line from input stream - std::string line; - std::getline(*m_stream, line, '\n'); + m_line_buffer_tmp.clear(); + std::getline(*m_stream, m_line_buffer_tmp, '\n'); + // add line with newline symbol to the line buffer - m_line_buffer += line + "\n"; + m_line_buffer += m_line_buffer_tmp; + m_line_buffer.push_back('\n'); } // set pointers @@ -7989,9 +7991,18 @@ class basic_json // iterate the result between the quotes for (const lexer_char_t* i = m_start + 1; i < m_cursor - 1; ++i) { - // process escaped characters - if (*i == '\\') + // number of non-escaped characters + const size_t n = static_cast(std::find(i, m_cursor - 1, '\\') - i); + + if (n != 0) { + result.append(reinterpret_cast(i), n); + i += n - 1; // -1 because will ++i + } + else + { + // processing escaped character + // read next character ++i; @@ -8078,12 +8089,6 @@ class basic_json } } } - else - { - // all other characters are just copied to the end of the - // string - result.append(1, static_cast(*i)); - } } return result; @@ -8267,6 +8272,8 @@ class basic_json std::istream* m_stream = nullptr; /// line buffer buffer for m_stream string_t m_line_buffer {}; + /// used for filling m_line_buffer + string_t m_line_buffer_tmp {}; /// the buffer pointer const lexer_char_t* m_content = nullptr; /// pointer to the beginning of the current symbol