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
Summary
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.Solution
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.Testing
masteracross all corpora, 15 encoder-flag combinations, and boundary lengths around the 16-byte chunk edge.json_encode_sse2_boundary.phpt,json_encode_sse2_options.phpt,json_encode_sse2_fuzz.phpt. Fullext/jsonsuite passes.Why this doesn't need an RFC
Per CONTRIBUTING.md, RFCs are for feature requests and behavior-changing proposals. This changes no API, no ini directive, and no observable output — only speed.
Alternatives
simdjson_php, decode-only; a fork adds encoding) exists but isn't in core, re-implements the encoder, and can drift from php-src's exact escaping semantics.Related Work
af112f6— SSE2url_encode(), closest prior art. Similar SSE2 precedent instripslashes(),strtr(),mb_strlen().php_json_escape_string().