Repository navigation
Commit df365c9
committed
ext/standard: Add a fast path for ASCII in htmlspecialchars()
ASCII is a single byte in every supported charset, so get_next_char() is
only called for bytes >= 0x80, and is inlined there. htmlspecialchars()
never changes ASCII other than & " ' < >, so after the verbatim prefix
such bytes are copied in a loop of their own instead of being looked up
in the entity table one by one.
Output is unchanged. CPU cycles per htmlspecialchars() call, i7-13700H,
GCC 13.3, GCC 14.2, clang 18.1, clang 20.1:
English text, 1-64 KB 45-84% fewer
HTML, 1-64 KB 41-80% fewer
Markdown, JavaScript, 4 KB 64-89% fewer
40-byte title with & " < > 41-65% fewer
only & or " 7-27% fewer
htmlentities(): 12-26% fewer cycles on titles and English text, from 3%
more to 23% fewer on HTML and Russian text. Strings with nothing to
encode, which are returned as they are, run the same instructions; their
cycles move by -8% to +15% with code placement.1 parent 1c9242e commit df365c9
1 file changed
Lines changed: 32 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| |||
1172 | 1172 | | |
1173 | 1173 | | |
1174 | 1174 | | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
1175 | 1181 | | |
1176 | 1182 | | |
1177 | 1183 | | |
| |||
1332 | 1338 | | |
1333 | 1339 | | |
1334 | 1340 | | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
1335 | 1344 | | |
1336 | 1345 | | |
1337 | 1346 | | |
1338 | 1347 | | |
1339 | 1348 | | |
1340 | | - | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
1341 | 1371 | | |
1342 | 1372 | | |
1343 | 1373 | | |
| |||
0 commit comments