From b6c01b346439dee888f94eca2052d076b2ce906b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 29 Aug 2020 10:46:04 +1000 Subject: [PATCH] Have jsonfindptrs cache std::to_string(i) On a mid-range x86_64 laptop, processing the 181 MiB citylots.json file from github.com/zemirco/sf-city-lots-json: $ time gen/bin/example-jsonfindptrs < citylots.json > /dev/null Before this commit: real 0m8.822s After: real 0m7.550s Ratio: 1.17x --- example/jsonfindptrs/jsonfindptrs.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/example/jsonfindptrs/jsonfindptrs.cc b/example/jsonfindptrs/jsonfindptrs.cc index 861e6954..1e03d7f6 100644 --- a/example/jsonfindptrs/jsonfindptrs.cc +++ b/example/jsonfindptrs/jsonfindptrs.cc @@ -191,6 +191,9 @@ std::vector g_quirks; std::string g_dst; +// g_to_string_cache[i] caches the result of std::to_string(i). +std::vector g_to_string_cache; + struct { int remaining_argc; char** remaining_argv; @@ -362,7 +365,10 @@ print_json_pointers(JsonThing& jt, uint32_t depth) { case JsonThing::Kind::Array: g_dst += "/"; for (size_t i = 0; i < jt.value.a.size(); i++) { - g_dst += std::to_string(i); + if (i >= g_to_string_cache.size()) { + g_to_string_cache.push_back(std::to_string(i)); + } + g_dst += g_to_string_cache[i]; TRY(print_json_pointers(jt.value.a[i], depth)); g_dst.resize(n + 1); }