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
diff --git a/example/jsonfindptrs/jsonfindptrs.cc b/example/jsonfindptrs/jsonfindptrs.cc
index 861e695..1e03d7f 100644
--- a/example/jsonfindptrs/jsonfindptrs.cc
+++ b/example/jsonfindptrs/jsonfindptrs.cc
@@ -191,6 +191,9 @@
std::string g_dst;
+// g_to_string_cache[i] caches the result of std::to_string(i).
+std::vector<std::string> g_to_string_cache;
+
struct {
int remaining_argc;
char** remaining_argv;
@@ -362,7 +365,10 @@
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);
}