Skip to content

Commit

Permalink
⚡ added performance fixes (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Nov 22, 2016
1 parent 869f4c6 commit f620d74
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
37 changes: 22 additions & 15 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8734,10 +8734,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<const char>(*m_cursor));
}
m_line_buffer.append(
reinterpret_cast<const typename string_t::value_type*>(m_start),
static_cast<size_t>(m_limit - m_start));
m_cursor = m_limit;
}

// append n characters to make sure that there is sufficient
Expand All @@ -8750,10 +8750,12 @@ class basic_json
// delete processed characters from line buffer
m_line_buffer.erase(0, static_cast<size_t>(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
Expand Down Expand Up @@ -8840,9 +8842,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<size_t>(std::find(i, m_cursor - 1, '\\') - i);

if (n != 0)
{
result.append(reinterpret_cast<const typename string_t::value_type*>(i), n);
i += n - 1; // -1 because will ++i
}
else
{
// processing escaped character

// read next character
++i;

Expand Down Expand Up @@ -8929,12 +8940,6 @@ class basic_json
}
}
}
else
{
// all other characters are just copied to the end of the
// string
result.append(1, static_cast<typename string_t::value_type>(*i));
}
}

return result;
Expand Down Expand Up @@ -9118,6 +9123,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
Expand Down
37 changes: 22 additions & 15 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char>(*m_cursor));
}
m_line_buffer.append(
reinterpret_cast<const typename string_t::value_type*>(m_start),
static_cast<size_t>(m_limit - m_start));
m_cursor = m_limit;
}

// append n characters to make sure that there is sufficient
Expand All @@ -7899,10 +7899,12 @@ class basic_json
// delete processed characters from line buffer
m_line_buffer.erase(0, static_cast<size_t>(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
Expand Down Expand Up @@ -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<size_t>(std::find(i, m_cursor - 1, '\\') - i);

if (n != 0)
{
result.append(reinterpret_cast<const typename string_t::value_type*>(i), n);
i += n - 1; // -1 because will ++i
}
else
{
// processing escaped character

// read next character
++i;

Expand Down Expand Up @@ -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<typename string_t::value_type>(*i));
}
}

return result;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f620d74

Please sign in to comment.