Skip to content

Commit 29cc05f

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 c65321c commit 29cc05f

8 files changed

Lines changed: 300 additions & 95 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ PHP NEWS
125125
in a php://filter URL. (Sjoerd Langkemper)
126126
. Improved performance of array_chunk() when not preserving keys.
127127
(mehmetcansahin)
128+
. Improved performance of htmlspecialchars() when nothing needs encoding.
129+
(Nicolas Grekas)
128130

129131
- URI:
130132
. 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
@@ -105,6 +105,8 @@ PHP 8.7 UPGRADE NOTES
105105
. Improved performance of str_rot13().
106106
. Improved performance of pack() for large strings using the a, A and Z
107107
formats.
108+
. htmlspecialchars() returns its input without copying it when nothing needs
109+
encoding.
108110

109111
- MBString:
110112
. 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
@@ -2273,14 +2273,12 @@ function headers_list(): array {}
22732273

22742274
/* {{{ html.c */
22752275

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

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

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

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

22862284
/**

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