Skip to content

Commit c1db7a4

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 c13fabf commit c1db7a4

8 files changed

Lines changed: 308 additions & 95 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ PHP NEWS
114114
elements. (mehmetcansahin)
115115
. Enforce max_filter_count: limit the number of filters that can be chained
116116
in a php://filter URL. (Sjoerd Langkemper)
117+
. Improved performance of htmlspecialchars() when nothing needs encoding.
118+
(Nicolas Grekas)
117119

118120
- URI:
119121
. Fix casing of enum cases in UriHostType and UrlHostType to match the RFC

‎UPGRADING‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ PHP 8.7 UPGRADE NOTES
100100
. Improved performance of str_rot13().
101101
. Improved performance of pack() for large strings using the a, A and Z
102102
formats.
103+
. htmlspecialchars() returns its input without copying it when nothing needs
104+
encoding.
103105

104106
- MBString:
105107
. Improved performance of mb_strlen for UTF-8 strings.

‎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)