From 1d40408734e004e2636fea4cda179d14cdc85807 Mon Sep 17 00:00:00 2001 From: lamentxu <1372449351@qq.com> Date: Fri, 20 Mar 2026 18:57:09 +0800 Subject: [PATCH 1/3] Update json_encoder.c --- ext/json/json_encoder.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index 186485c05c6f4..c20fd9da4e5a9 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -54,8 +54,17 @@ 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); + static const char spaces[] = + " "; + size_t remaining = (size_t) encoder->depth * 4; + + while (remaining >= sizeof(spaces) - 1) { + smart_str_appendl(buf, spaces, sizeof(spaces) - 1); + remaining -= sizeof(spaces) - 1; + } + + if (remaining) { + smart_str_appendl(buf, spaces, remaining); } } } From a967ff1ecab7b28eb195a917a2780bfd0f252433 Mon Sep 17 00:00:00 2001 From: lamentxu <1372449351@qq.com> Date: Fri, 20 Mar 2026 19:56:48 +0800 Subject: [PATCH 2/3] Update UPGRADING --- UPGRADING | 2 ++ 1 file changed, 2 insertions(+) diff --git a/UPGRADING b/UPGRADING index 113258ebbd3eb..b7b160820bb2a 100644 --- a/UPGRADING +++ b/UPGRADING @@ -252,6 +252,8 @@ PHP 8.6 UPGRADE NOTES - JSON: . Improve performance of encoding arrays and objects. + . Improved performance of indentation generation in json_encode() + when using PHP_JSON_PRETTY_PRINT. - Standard: . Improved performance of array_fill_keys(). From 9ac40b5092c2600d173554615eebf9bf839c893c Mon Sep 17 00:00:00 2001 From: lamentxu <1372449351@qq.com> Date: Fri, 20 Mar 2026 20:49:16 +0800 Subject: [PATCH 3/3] apply changes from reviews --- ext/json/json_encoder.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index c20fd9da4e5a9..a0f24d6fb8dfa 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -54,17 +54,10 @@ 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) { - static const char spaces[] = - " "; size_t remaining = (size_t) encoder->depth * 4; - - while (remaining >= sizeof(spaces) - 1) { - smart_str_appendl(buf, spaces, sizeof(spaces) - 1); - remaining -= sizeof(spaces) - 1; - } - if (remaining) { - smart_str_appendl(buf, spaces, remaining); + char *dst = smart_str_extend(buf, remaining); + memset(dst, ' ', remaining); } } }