Skip to content

Optimize repeated user callbacks in array sorting - #25496

Closed
drealecs wants to merge 1 commit into
php:masterfrom
drealecs:optimize-usort-prepared-callbacks
Closed

drealecs wants to merge 1 commit into
php:masterfrom
drealecs:optimize-usort-prepared-callbacks

Conversation

@drealecs

Copy link
Copy Markdown
Contributor

This reduces callback-dispatch overhead in usort(), uasort(), uksort() and some callback-based diff/intersect functions.
No userland behavior changes are intended.

To achieve this, I added specialized PHP/native call helpers for two by-value arguments. Eligibility is checked once per comparator.

Paired micro-benchmarks on Apple Silicon/macOS, using matching NTS release builds of master and this branch, showed the following elapsed-time reductions for 128-entry sorting array:

Comparator Reduction
Named PHP function 13%
Static arrow 14%
Instance method 12%
strnatcmp 12%
First-class strnatcmp 10%
strcmp over strings 23%

Comment thread ext/standard/array.c
static zend_always_inline void php_array_call_user_compare(zval *args, zval *retval)
{
const zend_function *func = BG(user_compare_fci_cache).function_handler;
if (EXPECTED(func != NULL && func == BG(user_compare_known_func))) {

@drealecs drealecs Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eligibility decision is cached, but the shared callback state can still change. There is backup/restore handling for ordinary nested calls, however, array_udiff_assoc() / array_uintersect_assoc() currently overwrite that state without restoring it, and also fiber switches can resume another operation before restoration happens.

#24453 and #24426 address those underlying issues. Once both are merged, and the fiber save/restore also includes user_compare_known_func, we can remove this identity check.
Generic fallback would still be needed for ineligible callbacks.

Comment thread Zend/zend_execute_API.c
if (!EG(active)) {
return FAILURE;
}
if (ZEND_HAS_OOM()) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of code duplication between this and zend_call_function(), and I think we need to extract some inlinable functions form both, to keep it maintainable on the long run.

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am extremely unconvinced the added complexity is worth it.

You don't provide a benchmark script nor how you performed said benchmark.

@drealecs

Copy link
Copy Markdown
Contributor Author

I am extremely unconvinced the added complexity is worth it.

Yes, I agree; after trying to reduce the duplication and complexity, I am trying to motivate myself that this is really worth it.

@drealecs

Copy link
Copy Markdown
Contributor Author

You don't provide a benchmark script nor how you performed said benchmark.

Sure, sorry for that. Here’s a standalone strcmp benchmark script.

<?php

$expected = [];
for ($i = 0; $i < 100_000; $i++) {
    $expected[] = sprintf('item-%05d', $i);
}
$input = (new Random\Randomizer(new Random\Engine\Mt19937()))->shuffleArray($expected);

$sorts = 40;
$start = hrtime(true);
for ($i = 0; $i < $sorts; $i++) {
    $items = $input;
    usort($items, 'strcmp');
}
$elapsed = hrtime(true) - $start;

printf("strcmp: %.3f ms/sort (%d sorts)\n", $elapsed / $sorts / 1e6, $sorts);

I compared matching NTS builds. Here’s how the run looks like:

$ opts=(-n -d opcache.enable_cli=1 -d opcache.jit=disable -d opcache.file_update_protection=0)
$ ./base/php "${opts[@]}" usort-strings.php
strcmp: 62.412 ms/sort (40 sorts)
$ ./pr/php "${opts[@]}" usort-strings.php
strcmp: 54.069 ms/sort (40 sorts)

I just reran 20 pairs on a Linux VM and on my local machine, alternating them. These are the mean times per sort:

Platform Base PR Reduction in mean time
Linux x86-64, GCC 14 60.51 ms 51.57 ms 14.8%
macOS ARM64, Apple Clang 21 41.91 ms 37.25 ms 11.1%

This is a case where the comparator is cheap and called many times. I wouldn’t expect the same percentage for a comparator that does more work.

@drealecs

Copy link
Copy Markdown
Contributor Author

I am extremely unconvinced the added complexity is worth it.

Yes, I agree; after trying to reduce the duplication and complexity, I am trying to motivate myself that this is really worth it.

I explored a bit more, and found a simpler solution, that brings a bit less value, but there is still reasonable performance improvement: #25654

I'm closing this PR, as the solution is too complex to maintain on the long run.

@drealecs drealecs closed this Nov 22, 2026
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.

2 participants