Skip to content

Optimize argument preparation for small positional calls - #25654

Open
drealecs wants to merge 1 commit into
php:masterfrom
drealecs:optimize-usort-callback-calls
Open

drealecs wants to merge 1 commit into
php:masterfrom
drealecs:optimize-usort-callback-calls

Conversation

@drealecs

Copy link
Copy Markdown
Contributor

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.

@TimWolla

Copy link
Copy Markdown
Member

Thanks. I triggered a realtime benchmark for this PR.

@drealecs
drealecs force-pushed the optimize-usort-callback-calls branch from 39cfe84 to 9ceccb6 Compare November 23, 2026 13:34
@drealecs
drealecs force-pushed the optimize-usort-callback-calls branch from 9ceccb6 to fed7a21 Compare November 23, 2026 16:01
@drealecs

Copy link
Copy Markdown
Contributor Author

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.

Would you mind triggering it again? Thanks!

@kocsismate

Copy link
Copy Markdown
Member

I've just triggered a new run: https://github.com/php/php-src/actions/runs/45216733497 I really hope this will now finish.

@github-actions

Copy link
Copy Markdown

AWS x86_64 (c6id.metal)

Attribute Value
Environment aws
Instance type c6id.metal
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz, 64 cores @ 2900 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 251 GB
Kernel 6.18.56-125.164.amzn2023.x86_64
OS Amazon Linux 2023.12.20261026
GCC 14.2.1
Binary layout strategy bolt-align
Time 2026-11-23 16:27:52 UTC
Job details https://github.com/php/php-src/actions/runs/45216733497 (Artifacts)
Changeset https://github.com/php/php-src/compare/39d8e1420a..fed7a21c0a

Laravel 12.11.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.39729 0.39960 0.00044 0.11% 0.39790 0.00% 0.39781 0.00% 1.935 0.000 1.000 31.82 MB
PHP - optimize-usort-callback-calls (ZTS) 0.39720 0.39837 0.00026 0.07% 0.39771 -0.05% 0.39768 -0.03% 0.364 2.220 0.026 31.81 MB

Symfony 2.8.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.70174 0.70610 0.00111 0.16% 0.70324 0.00% 0.70302 0.00% 0.524 0.000 1.000 31.85 MB
PHP - optimize-usort-callback-calls (ZTS) 0.70362 0.70628 0.00067 0.10% 0.70446 0.17% 0.70421 0.17% 1.297 -5.346 0.000 31.83 MB

Wordpress 6.9 main page - 50 iterations, 20 warmups, 20 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.60172 0.60412 0.00060 0.10% 0.60256 0.00% 0.60240 0.00% 0.628 0.000 1.000 31.94 MB
PHP - optimize-usort-callback-calls (ZTS) 0.60105 0.60234 0.00032 0.05% 0.60162 -0.15% 0.60163 -0.13% 0.347 7.511 0.000 31.87 MB

bench.php - 50 iterations, 20 warmups, 2 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.48018 0.50057 0.00446 0.92% 0.48289 0.00% 0.48169 0.00% 3.573 0.000 1.000 31.94 MB
PHP - optimize-usort-callback-calls (ZTS) 0.48007 0.50398 0.00525 1.09% 0.48299 0.02% 0.48143 -0.05% 3.134 1.875 0.061 31.87 MB

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

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.

@github-actions

Copy link
Copy Markdown

AWS x86_64 (c6id.metal)

Attribute Value
Environment aws
Instance type c6id.metal
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz, 64 cores @ 2900 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 251 GB
Kernel 6.18.56-125.164.amzn2023.x86_64
OS Amazon Linux 2023.12.20261026
GCC 14.2.1
Binary layout strategy none
Time 2026-11-24 05:37:42 UTC
Job details https://github.com/php/php-src/actions/runs/45334609906 (Artifacts)
Changeset https://github.com/php/php-src/compare/39d8e1420a..fed7a21c0a

Laravel 12.11.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.38980 0.39815 0.00114 0.29% 0.39063 0.00% 0.39040 0.00% 6.096 0.000 1.000 26.33 MB
PHP - baseline@39d8e14 (JIT, ZTS) 0.36153 0.36553 0.00064 0.18% 0.36238 -7.23% 0.36226 -7.21% 3.054 8.614 0.000 26.35 MB
PHP - optimize-usort-callback-calls (ZTS) 0.38942 0.39098 0.00035 0.09% 0.38993 -0.18% 0.38987 -0.13% 1.084 6.597 0.000 26.33 MB
PHP - optimize-usort-callback-calls (JIT, ZTS) 0.36148 0.36369 0.00044 0.12% 0.36199 -7.33% 0.36181 -7.32% 1.976 8.614 0.000 26.35 MB

Symfony 2.8.0 demo app - 50 iterations, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.68779 0.69457 0.00099 0.14% 0.68858 0.00% 0.68831 0.00% 4.736 0.000 1.000 26.76 MB
PHP - baseline@39d8e14 (JIT, ZTS) 0.65923 0.66077 0.00036 0.05% 0.65980 -4.18% 0.65967 -4.16% 0.850 8.614 0.000 26.79 MB
PHP - optimize-usort-callback-calls (ZTS) 0.68350 0.68543 0.00042 0.06% 0.68413 -0.65% 0.68401 -0.62% 1.446 8.614 0.000 26.70 MB
PHP - optimize-usort-callback-calls (JIT, ZTS) 0.65789 0.66358 0.00096 0.15% 0.65877 -4.33% 0.65845 -4.34% 3.066 8.614 0.000 26.79 MB

Wordpress 6.9 main page - 50 iterations, 20 warmups, 20 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.59131 0.59515 0.00086 0.15% 0.59208 0.00% 0.59185 0.00% 2.469 0.000 1.000 26.82 MB
PHP - baseline@39d8e14 (JIT, ZTS) 0.51923 0.52034 0.00028 0.05% 0.51970 -12.22% 0.51965 -12.20% 0.586 8.614 0.000 26.92 MB
PHP - optimize-usort-callback-calls (ZTS) 0.59210 0.59623 0.00090 0.15% 0.59297 0.15% 0.59270 0.14% 2.296 -6.553 0.000 26.83 MB
PHP - optimize-usort-callback-calls (JIT, ZTS) 0.51971 0.52302 0.00072 0.14% 0.52062 -12.07% 0.52045 -12.06% 2.052 8.614 0.000 26.92 MB

bench.php - 50 iterations, 20 warmups, 2 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 0.46550 0.46770 0.00044 0.09% 0.46663 0.00% 0.46663 0.00% 0.011 0.000 1.000 26.82 MB
PHP - baseline@39d8e14 (JIT, ZTS) 0.14554 0.14798 0.00034 0.23% 0.14593 -68.73% 0.14588 -68.74% 4.819 8.614 0.000 26.92 MB
PHP - optimize-usort-callback-calls (ZTS) 0.46607 0.46821 0.00051 0.11% 0.46690 0.06% 0.46689 0.06% 0.701 -2.520 0.012 26.83 MB
PHP - optimize-usort-callback-calls (JIT, ZTS) 0.14548 0.14838 0.00060 0.41% 0.14611 -68.69% 0.14596 -68.72% 2.607 8.614 0.000 26.92 MB

micro_bench.php - 50 iterations, 5 warmups, 1 request (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skewness Z-stat P-value Memory
PHP - baseline@39d8e14 (ZTS) 1.31661 1.34405 0.00611 0.46% 1.32854 0.00% 1.32774 0.00% 0.588 0.000 1.000 26.82 MB
PHP - baseline@39d8e14 (JIT, ZTS) 0.60353 0.68581 0.01935 3.12% 0.62093 -53.26% 0.61343 -53.80% 1.728 8.614 0.000 26.92 MB
PHP - optimize-usort-callback-calls (ZTS) 1.31582 1.37134 0.01088 0.82% 1.32986 0.10% 1.32812 0.03% 1.725 -0.003 0.997 26.83 MB
PHP - optimize-usort-callback-calls (JIT, ZTS) 0.59627 0.62316 0.00543 0.89% 0.60963 -54.11% 0.60961 -54.09% 0.198 8.614 0.000 26.92 MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants