-
Notifications
You must be signed in to change notification settings - Fork 8k
Optimize JSON pretty print indentation performance #21474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
1d40408
a967ff1
9ac40b5
b6875ae
c3ed1af
cd9192c
6c5cd75
42b9ba6
aea6397
bdd8031
f478681
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,11 +53,19 @@ static inline void php_json_pretty_print_char(smart_str *buf, int options, char | |||||||||||||||
|
|
||||||||||||||||
| static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */ | ||||||||||||||||
| { | ||||||||||||||||
| if (options & PHP_JSON_PRETTY_PRINT) { | ||||||||||||||||
| for (int i = 0; i < encoder->depth; ++i) { | ||||||||||||||||
| smart_str_appendl(buf, " ", 4); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| if (options & PHP_JSON_PRETTY_PRINT) { | ||||||||||||||||
| int depth = encoder->depth; | ||||||||||||||||
| 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); | ||||||||||||||||
| } | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can the original loop can be faster? The inlined functions are almost the same: Lines 130 to 132 in d3727f4
Lines 58 to 61 in d3727f4
It the return variable the bottleneck? Or maybe use the original approach with longer spaces literal " ..." with like 64 spaces?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a guess,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So by using the original optimization approach the optimized version is 1.29x slower in depth 2 and 1.12x slower in depth 1. So yes, this pr could only optimize nested data.
This happens to be even slower, weird. Probably because of compiler's optimization, because smart_str_appendl(buf, " ", 4) allows the compiler to optimize the constant size 4 into a single 32-bit integer store, bypassing the overhead of a variable-length memcpy and branch evaluations present in the optimized block. |
||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| /* }}} */ | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||


Uh oh!
There was an error while loading. Please reload this page.