Repository navigation
Conversation
| 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))) { |
There was a problem hiding this comment.
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.
| if (!EG(active)) { | ||
| return FAILURE; | ||
| } | ||
| if (ZEND_HAS_OOM()) { |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
I am extremely unconvinced the added complexity is worth it.
You don't provide a benchmark script nor how you performed said benchmark.
Yes, I agree; after trying to reduce the duplication and complexity, I am trying to motivate myself that this is really worth it. |
Sure, sorry for that. Here’s a standalone <?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:
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. |
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. |
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:
strnatcmpstrnatcmpstrcmpover strings