Skip to content

Commit b4b4347

Browse files
ext/standard: Return the input of htmlspecialchars() when nothing needs encoding
htmlspecialchars() allocated a buffer of twice the input and decoded it character by character, even when the result was the input itself. The input is now scanned first for the characters that need encoding and for invalid multi-byte sequences, with SSE2 on 16-byte blocks. When there is none, the input string is returned, as html_entity_decode() and htmlspecialchars_decode() already do since 68dc754. Otherwise, encoding starts after the part that was scanned. The @refcount 1 annotations of htmlspecialchars() and htmlentities() are removed since they can now return their argument.
1 parent e256041 commit b4b4347

8 files changed

Lines changed: 261 additions & 95 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ PHP NEWS
2121
- Standard:
2222
. Improved performance of array_splice() when inserting without removing
2323
elements. (mehmetcansahin)
24+
. Improved performance of htmlspecialchars() when nothing needs encoding.
25+
(Nicolas Grekas)
2426

2527
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

‎UPGRADING‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ PHP 8.7 UPGRADE NOTES
7474
- Standard:
7575
. Improved performance of array_splice() when inserting without removing
7676
elements.
77+
. htmlspecialchars() returns its input without copying it when nothing needs
78+
encoding.

‎Zend/Optimizer/zend_func_infos.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,6 @@ static const func_info_t func_infos[] = {
463463
F1("inet_pton", MAY_BE_STRING|MAY_BE_FALSE),
464464
F1("metaphone", MAY_BE_STRING),
465465
F1("headers_list", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING),
466-
F1("htmlspecialchars", MAY_BE_STRING),
467-
F1("htmlentities", MAY_BE_STRING),
468466
F1("get_html_translation_table", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING),
469467
F1("bin2hex", MAY_BE_STRING),
470468
F1("hex2bin", MAY_BE_STRING|MAY_BE_FALSE),

‎ext/standard/basic_functions.stub.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,14 +2267,12 @@ function headers_list(): array {}
22672267

22682268
/* {{{ html.c */
22692269

2270-
/** @refcount 1 */
22712270
function htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string {}
22722271

22732272
function htmlspecialchars_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string {}
22742273

22752274
function html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string {}
22762275

2277-
/** @refcount 1 */
22782276
function htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string {}
22792277

22802278
/**

‎ext/standard/basic_functions_arginfo.h‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/standard/basic_functions_decl.h‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)