micro-optimization of dump()

A lot of small changes to avoid memory allocations:

- The locale is only queried once rather than with every number
serialization.
- The indentation string is recycled between different calls.
- The string escape function avoids a copy if no escaping is necessary.
- The string escape and the space function use a complete switch case
instead of cascaded ifs.

Cachegrind measures some 15% performance improvement.
parent fc48b8ac
This diff is collapsed.
This diff is collapsed.
...@@ -49,44 +49,53 @@ TEST_CASE("convenience functions") ...@@ -49,44 +49,53 @@ TEST_CASE("convenience functions")
SECTION("string escape") SECTION("string escape")
{ {
CHECK(json::serializer::escape_string("\"") == "\\\""); const auto check_escaped = [](const char* original,
CHECK(json::serializer::escape_string("\\") == "\\\\"); const char* escaped)
CHECK(json::serializer::escape_string("\b") == "\\b"); {
CHECK(json::serializer::escape_string("\f") == "\\f"); std::stringstream ss;
CHECK(json::serializer::escape_string("\n") == "\\n"); json::serializer s(ss);
CHECK(json::serializer::escape_string("\r") == "\\r"); s.dump_escaped(original);
CHECK(json::serializer::escape_string("\t") == "\\t"); CHECK(ss.str() == escaped);
};
CHECK(json::serializer::escape_string("\x01") == "\\u0001"); check_escaped("\"", "\\\"");
CHECK(json::serializer::escape_string("\x02") == "\\u0002"); check_escaped("\\", "\\\\");
CHECK(json::serializer::escape_string("\x03") == "\\u0003"); check_escaped("\b", "\\b");
CHECK(json::serializer::escape_string("\x04") == "\\u0004"); check_escaped("\f", "\\f");
CHECK(json::serializer::escape_string("\x05") == "\\u0005"); check_escaped("\n", "\\n");
CHECK(json::serializer::escape_string("\x06") == "\\u0006"); check_escaped("\r", "\\r");
CHECK(json::serializer::escape_string("\x07") == "\\u0007"); check_escaped("\t", "\\t");
CHECK(json::serializer::escape_string("\x08") == "\\b");
CHECK(json::serializer::escape_string("\x09") == "\\t"); check_escaped("\x01", "\\u0001");
CHECK(json::serializer::escape_string("\x0a") == "\\n"); check_escaped("\x02", "\\u0002");
CHECK(json::serializer::escape_string("\x0b") == "\\u000b"); check_escaped("\x03", "\\u0003");
CHECK(json::serializer::escape_string("\x0c") == "\\f"); check_escaped("\x04", "\\u0004");
CHECK(json::serializer::escape_string("\x0d") == "\\r"); check_escaped("\x05", "\\u0005");
CHECK(json::serializer::escape_string("\x0e") == "\\u000e"); check_escaped("\x06", "\\u0006");
CHECK(json::serializer::escape_string("\x0f") == "\\u000f"); check_escaped("\x07", "\\u0007");
CHECK(json::serializer::escape_string("\x10") == "\\u0010"); check_escaped("\x08", "\\b");
CHECK(json::serializer::escape_string("\x11") == "\\u0011"); check_escaped("\x09", "\\t");
CHECK(json::serializer::escape_string("\x12") == "\\u0012"); check_escaped("\x0a", "\\n");
CHECK(json::serializer::escape_string("\x13") == "\\u0013"); check_escaped("\x0b", "\\u000b");
CHECK(json::serializer::escape_string("\x14") == "\\u0014"); check_escaped("\x0c", "\\f");
CHECK(json::serializer::escape_string("\x15") == "\\u0015"); check_escaped("\x0d", "\\r");
CHECK(json::serializer::escape_string("\x16") == "\\u0016"); check_escaped("\x0e", "\\u000e");
CHECK(json::serializer::escape_string("\x17") == "\\u0017"); check_escaped("\x0f", "\\u000f");
CHECK(json::serializer::escape_string("\x18") == "\\u0018"); check_escaped("\x10", "\\u0010");
CHECK(json::serializer::escape_string("\x19") == "\\u0019"); check_escaped("\x11", "\\u0011");
CHECK(json::serializer::escape_string("\x1a") == "\\u001a"); check_escaped("\x12", "\\u0012");
CHECK(json::serializer::escape_string("\x1b") == "\\u001b"); check_escaped("\x13", "\\u0013");
CHECK(json::serializer::escape_string("\x1c") == "\\u001c"); check_escaped("\x14", "\\u0014");
CHECK(json::serializer::escape_string("\x1d") == "\\u001d"); check_escaped("\x15", "\\u0015");
CHECK(json::serializer::escape_string("\x1e") == "\\u001e"); check_escaped("\x16", "\\u0016");
CHECK(json::serializer::escape_string("\x1f") == "\\u001f"); check_escaped("\x17", "\\u0017");
check_escaped("\x18", "\\u0018");
check_escaped("\x19", "\\u0019");
check_escaped("\x1a", "\\u001a");
check_escaped("\x1b", "\\u001b");
check_escaped("\x1c", "\\u001c");
check_escaped("\x1d", "\\u001d");
check_escaped("\x1e", "\\u001e");
check_escaped("\x1f", "\\u001f");
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment