[php-src] PR #22492: Add json_encode/json_decode micro-benchmarks and optimize Jkey writing
| From: | Squareys | Date: | Sat, 27 Jun 2026 19:32:19 +0000 |
| Subject: | [php-src] PR #22492: Add json_encode/json_decode micro-benchmarks and optimize Jkey writing | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36701@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22492
Author: Squareys
Hi @bukka,
This PR aims to be a first small step to get my feet wet optimizing some of the most used parts of
the runtime. JSON encoding/decoding is used in pretty much every web application, and optimized
parsing is something I've done lots of for Wonderland Engine in the past, so it feels like a
good match.
## In this PR
This PR does the following (each in a small commit):
1. Adds an encode/decode of various data structures to micro_bench.php, to be able to measure impact
of this PR and future PRs.
2. Optimizes JSON
"<key>" output as the most used primitive in JSON
output. The idea is fairly simple: most key strings will be trivial identifiers like
"id", "name", "value",
"list" etc. and we don't need to escape the string at all. We detect
this case to do a single alloc + simple memcpy instead.
3. Another frequent case is PHP identifiers as JSON keys for which we can use a specialized, slim
form of string escaping. It is used especially when encoding class objects to JSON.
4. Finally, I realized that even if we fail the fast-path detection, we can reuse the information of
where it failed and fast-path the trivial prefix, which avoids pessimizing strings with long trivial
ASCII prefixes that contain ASCII characters.
I know the string encoding work looks like it overlaps with https://github.com/php/php-src/pull/17734,
which optimizes the encoding itself (especially for long strings), but in the spirit of "the
fastest code is that which does not run at all", this optimization cuts past the code that the
other PR would optimize.
## Results
From callgrind:
| State | json_encoder.c insns | vs baseline |
|----------------------------|---------------------|-------------|
| Baseline | 248,600,000 | |
| + String fast path | 226,300,000 | -9.0% |
| + Identifier fast path | 197,000,000 | -20.8% |
| + Prefix flush | 209,600,000 | -15.7% |
I wanted to run some WordPress/Symfony benchmarks to measure the impact there, but the results were
a bit too noisy to be presentable. The "regression" in the prefix flush is because the
benchmarks contain only the trivial case strings so far, but it's necessary to avoid
pessimization mentioned above.
## LLM usage disclosure
I **did** use coding agents/LLMs for the following tasks:
* Running and "typing out" of the benchmarks
* Implementing optimization ideas
* Writing commit messages
* Search open PRs for existing overlapping work
More importantly, I did **not** use LLMs for ideas on optimization approaches, those were
exclusively generated by myself. Also, reading the code, and the process of optimization and strict
reviews of the LLM's generated code as well as critical evaluation of the benchmark results are
my own work.. This PR description is hand-written.