Skip to content

Commit a8cc04d

Browse files
authored
Optimize JSON pretty print indentation performance (GH-21474)
Grow the string buffer in advance, avoiding repeated reallocations for each indentation level.
1 parent 674473d commit a8cc04d

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ PHP 8.6 UPGRADE NOTES
252252

253253
- JSON:
254254
. Improve performance of encoding arrays and objects.
255+
. Improved performance of indentation generation in json_encode()
256+
when using PHP_JSON_PRETTY_PRINT.
255257

256258
- Standard:
257259
. Improved performance of array_fill_keys().

ext/json/json_encoder.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static inline void php_json_pretty_print_char(smart_str *buf, int options, char
5454
static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */
5555
{
5656
if (options & PHP_JSON_PRETTY_PRINT) {
57+
smart_str_alloc(buf, encoder->depth * 4, 0);
5758
for (int i = 0; i < encoder->depth; ++i) {
5859
smart_str_appendl(buf, " ", 4);
5960
}

0 commit comments

Comments
 (0)