You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize argument preparation for small positional calls - #25654
zend_call_function() now has a fast path for calls with up to two positional arguments passed by value.
This reduces the cost of invoking callbacks from functions such as usort() and array_map(). Only argument preparation changes; callback execution and cleanup use the existing code.
On Linux/GCC, usort() took about 6% less time to sort 100,000 shuffled integers or floats with static fn($a, $b) => $a <=> $b.
Thanks. I triggered a realtime benchmark for this PR.
Thanks! The run reached the instance’s two-hour limit, and the last output I could identify was from the baseline ZTS + JIT build, so I don't think it reached the benchmark workloads.
I measured this on master 2550ff2 with GCC 15 and clang 21 (Ryzen 9 5950X, official Docker image flags, opcache on, JIT off). The fast path helps where the PR says, and calls that fall back to the loop pay a bit for the extra checks.
Change in CPU cycles per call against master, negative = fewer cycles. One call walks 10,000 elements:
call
master, cycles (GCC 15)
GCC 15
clang 21
usort(), ints, static fn($a, $b) => $a <=> $b
16.1M
−4.8%
−3.1%
usort() with 'strcmp'
13.8M
−4.3%
−6.0%
array_walk(), by-value callback
1.13M
−5.4%
−2.9%
array_map() with a __call callable
3.42M
−14.6%
−3.5%
array_map(), 3 arrays (falls back)
0.95M
+3.1%
+1.1%
array_walk(), function (&$v, $k) (falls back)
1.13M
+4.1%
+3.9%
Laravel /, per request
7.14M
+0.2%
+0.1%
Laravel /users, per request
47.0M
−0.3%
+0.3%
Symfony Demo /en/blog/, per request
26.1M
0%
−0.3%
The fallback cases also run 3–4% more instructions. Two cases got slower in cycles with no extra instructions: array_filter() +13% on clang and preg_replace_callback() +9% on GCC. Branch and icache misses don't change there, so I think it's code placement.
I also tried only switching the existing loop to QUICK_ARG_SHOULD_BE_SENT_BY_REF(). That gets −2.3% / +0.7% on the usort() case, so the separate path does pay off.
A differential test of 89 callback cases gives the same output as master with opcache off, on and with tracing JIT, and ASan is clean.
One small thing: goto args_prepared skips the named-params and ZEND_CALL_MAY_HAVE_UNDEF blocks. Maybe add an assert after the label, so a later change there doesn't silently miss the 1–2 argument calls. Otherwise LGTM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zend_call_function()now has a fast path for calls with up to two positional arguments passed by value.This reduces the cost of invoking callbacks from functions such as
usort()andarray_map(). Only argument preparation changes; callback execution and cleanup use the existing code.On Linux/GCC,
usort()took about 6% less time to sort 100,000 shuffled integers or floats withstatic fn($a, $b) => $a <=> $b.