Optimize JSON pretty print indentation performance#21474
Optimize JSON pretty print indentation performance#21474LamentXU123 wants to merge 5 commits intophp:masterfrom
Conversation
Could you give some before/after numbers? |
Sure do, but maybe tomorrow. I think this is a pretty obvious optimization so I don't do this initially |
|
A short benchmark would be appreciated. |
|
So you should probably raise threshold in that condition ( |
Well when depth > 2 the performance of the optimized version start to be better than the original one, and the benefits become bigger when depth increase. I think the threshold can be increased to 8, because since than it provides a 1.10x optimization which is useful enough |
| if (depth <= 8) { | ||
| int i; | ||
| for (i = 0; i < depth; i++) { | ||
| smart_str_appendl(buf, " ", 4); | ||
| } | ||
| } else { | ||
| size_t remaining = (size_t) depth * 4; | ||
| char *dst = smart_str_extend(buf, remaining); | ||
| memset(dst, ' ', remaining); | ||
| } |
There was a problem hiding this comment.
There was a problem hiding this comment.
How can the original loop can be faster?
I am wondering this too, probably because memset is causing extra cost... But I think thats ok the loss can be ignored when depth goes bigger.
I will run benchmarks to test the original approach later (but I highly doubt if it could be better than memset)
There was a problem hiding this comment.
Just a guess, memset with an arbitrary length may be overspecialized for large sizes. In that case, I don't see a big point of this PR. 50 levels of nesting seem pretty artificial. Nevertheless, I'm not code owner so I'll keep that decision up to those who are.
There was a problem hiding this comment.
50 levels of nesting seem pretty artificial.
The original approach (by defining space constant) should work in simple json structures I guess. I will send in the test results later in this thread




This PR optimizes the
php_json_pretty_print_indentfunction in the JSON extension to improve performance when encoding structures withJSON_PRETTY_PRINT.When I was reading this, I found that now, the indentation logic used a for loop to append spaces in increments of 4 characters per depth level. For a JSON structure with a depth of N, this resulted in N consecutive calls to
smart_str_appendl. This approach introduces unnecessary overhead due to repeated function calls.So I think I would introduce this space-time optimization to add a static constant string of pre-allocated spaces. For almost all typical JSON depths, this reduces the number of
smart_str_appendlcalls from O(N) to exactly 1. This significantly reduces function call overhead and improves CPU performance duringjson_encodewithJSON_PRETTY_PRINT, especially for deeply nested data.