Optimization for htmlspecialchars function #18126
Open
+259
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Optimization for
htmlspecialchars
function.A dedicated
php_htmlspecialchars
function instead of the “universal”php_escape_html_entities_ex
.We work with ASCII-compatible encodings, we can employ byte-by-byte scanning and a lookup table to identify special characters. For
c < 0x80
, the lookup table is used; for potentially multi-byte characters, we continue to rely onget_next_char
.This approach provides a noticeable performance improvement for ASCII strings and some improvement for multi-byte strings due to more optimized logic.
Important!
We have changed the entity validation logic so that the maximum length of an entity name can no longer exceed
LONGEST_ENTITY_LENGTH
.Benchmarks 4k char strings:
htmlspecialchars
withUTF-8
encodinghtmlspecialchars
with lang-specific encoding. Without encoding hinthtmlspecialchars
with lang-specific encoding. With encoding hintWe may need more benchmarks.
This is not the final optimization, as
get_next_char
remains suboptimal for this function due to extra character-detection steps that aren’t required under the default flags.