From f4ba6015a8ed381301001023079129a4066d0169 Mon Sep 17 00:00:00 2001 From: Wilhelm Schuermann Date: Sat, 24 Oct 2015 10:47:34 +0200 Subject: [PATCH] JSON: Improve json::decode() performance - Improves "load" time by ~28% across all performance tests. - Improves the "add" performance test by ~20% since "load" takes most of the time there. --- src/JSON.cpp | 55 +++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/JSON.cpp b/src/JSON.cpp index 33bd48538..353fca868 100644 --- a/src/JSON.cpp +++ b/src/JSON.cpp @@ -405,38 +405,45 @@ std::string json::encode (const std::string& input) std::string json::decode (const std::string& input) { std::string output; - for (unsigned int i = 0; i < input.length (); ++i) + size_t pos = 0; + + while (pos < input.length ()) { - if (input[i] == '\\') + if (input[pos] == '\\') { - ++i; - switch (input[i]) + ++pos; + switch (input[pos]) { - // Simple translations. - case '"': output += '"'; break; - case '\\': output += '\\'; break; - case '/': output += '/'; break; - case 'b': output += '\b'; break; - case 'f': output += '\f'; break; - case 'n': output += '\n'; break; - case 'r': output += '\r'; break; - case 't': output += '\t'; break; + // Simple translations. + case '"': output += '"'; break; + case '\\': output += '\\'; break; + case '/': output += '/'; break; + case 'b': output += '\b'; break; + case 'f': output += '\f'; break; + case 'n': output += '\n'; break; + case 'r': output += '\r'; break; + case 't': output += '\t'; break; - // Compose a UTF8 unicode character. - case 'u': - output += utf8_character (utf8_codepoint (input.substr (++i))); - i += 3; - break; + // Compose a UTF8 unicode character. + case 'u': + output += utf8_character (utf8_codepoint (input.substr (++pos))); + pos += 3; + break; - // If it is an unrecognized sequence, do nothing. - default: - output += '\\'; - output += input[i]; - break; + // If it is an unrecognized sequence, do nothing. + default: + output += '\\'; + output += input[pos]; + break; } + ++pos; } else - output += input[i]; + { + size_t next_backslash = input.find ('\\', pos); + output.append (input, pos, next_backslash - pos); + pos = next_backslash; + } } return output;