[php-src] PR #22932: Improve performance of json_encode() for long ASCII strings
| From: | ptqa | Date: | Thu, 30 Jul 2026 10:37:47 +0000 |
| Subject: | [php-src] PR #22932: Improve performance of json_encode() for long ASCII strings | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-37354@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22932
Author: ptqa
## Summary
Speed up
json_encode() for long ASCII strings using zend_simd.h.
The SIMD scanner finds the first byte requiring special handling. The safe prefix
is appended in one operation, then the existing scalar encoder resumes at the
special byte. Short strings, unsupported architectures, and
JSON_UNESCAPED_UNICODE keep the existing path.
No API or behavior changes.
## Benchmarks
Ryzen 9 5900X, GCC 13.3, -O2, CPU-pinned. Median of three alternating runs;
each run uses seven samples.
| Workload | Before | After | Change |
|---|---:|---:|---:|
| ASCII, 256 B | 177.9 ns | 77.9 ns | 2.28× faster |
| ASCII, 4 KiB | 3098.5 ns | 1457.8 ns | 2.13× faster |
| ASCII, 64 KiB | 49191.9 ns | 22270.8 ns | 2.21× faster |
| Long-ASCII record payload | 28171.8 ns | 22217.4 ns | 1.27× faster |
| Sparse-escape record payload | 28483.7 ns | 22551.4 ns | 1.26× faster |
| UTF-8 record payload | 78133.0 ns | 78578.1 ns | Flat |
The reproducible harness is included as ext/json/bench.php.
## Validation
- Debug suite: 13,444 passed, 0 failed.
- Debug+ZTS JSON suite: 101 passed, 0 failed.
- Added SIMD-boundary, escaping-option, Unicode, and invalid UTF-8 coverage.
> Disclosure: This PR was created with LLM assistance and reviewed by
> the contributor.