Repository navigation
Conversation
784cc00 to
dbdfa74
Compare
dbdfa74 to
27d9c1c
Compare
|
I measured this on current master (bd6c637) with four compilers, the same way as #23682. Correctness. On current master it needs one change to build: Numbers. Cycles / instructions per
With #24283 on top. #24283 decodes valid two- and three-byte UTF-8 inline in this function. Merged onto this PR (the one call in
So together nothing non-ASCII stays slower than master; the one row left above 1.00 is a string made only of quotes with GCC (1.10–1.18). Merging the two needs only that one call changed. |
27d9c1c to
9487997
Compare
9487997 to
b387b4f
Compare
|
So my plan is to first get #24283 merged and then look into this one. |
b387b4f to
cd18248
Compare
|
@bukka I rebased this after you merged #24283. Re-measured with my benchmark script (speed-up against current master):
Note for review: the diff looks big, but a lot of it is old code that I moved into new functions to reuse them. Main idea: the SIMD check does more than answer "does this block need escaping?". It builds a map of all 16 bytes that shows which bytes are clean and which are dirty (may need special handling). So we get a benefit from each check, even if the block has dirty bytes:
So one SIMD check is enough for the whole block, even with several dirty bytes in it. Clean bytes are copied later in one piece. Wider instruction sets such as AVX2 could be tried in a follow-up. This PR only uses SSE2/NEON. CI failure (windows) is unrelated. |
json_encode()walks every string byte-by-byte inphp_json_escape_string(), even for the common case of plain ASCII needing no escaping. This adds a vectorized (SSE2/NEON) fast path that scans 16 bytes at a time, finds every character that needs escaping in one pass, and bulk-copies the clean runs in between. No output or behavior change — purely a speed improvement, with wins across clean ASCII, HTML-heavy, and URL-heavy content and no meaningful regression anywhere measured.On a 16-byte chunk, the fast path finds every character that needs escaping in one vector comparison and handles all of them before moving to the next chunk, rather than dropping into a slower per-byte loop as soon as it hits one. It also only compares against characters actually being escaped under the active encoder options (so plain
</>/&/'don't cost anything when the matching flags aren't set), skips the vector setup for strings shorter than one chunk, and uses one shared implementation across x86 and ARM.Benchmarks
Speedup range across small/medium/large corpus sizes, per CPU,
TRIALS=7:(Only one cell out of 42 measured regresses (
non_ascii, AMD, 0.97×) — within noise. Everything else is flat-to-positive.Related Work
af112f6— SSE2url_encode(), closest prior art. Similar SSE2 precedent instripslashes(),strtr(),mb_strlen().php_json_escape_string().