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
This commit is contained in:
Nigel Tao committed 2020-08-29 10:46:04 +10:00
1 parent 8098d96113
commit b6c01b3464
1 file changed
+7 -1
+7 -1
View File
@@ -191,6 +191,9 @@ std::vector<uint32_t> g_quirks;
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 @@ 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);
}