Skip to content

Zend: Optimize sorting single-element arrays - #23550

Merged
LamentXU123 merged 4 commits into
php:masterfrom
LamentXU123:packed
Sep 3, 2026
Merged

Zend: Optimize sorting single-element arrays#23550
LamentXU123 merged 4 commits into
php:masterfrom
LamentXU123:packed

Conversation

@LamentXU123

@LamentXU123 LamentXU123 commented Sep 3, 2026

Copy link
Copy Markdown
Member

Now, zend_array_sort() uses zend_array_sort_ex(), which unpacks packed arrays and temporarily increments the array refcount to protect against the comparator freeing the array.

Obviously this is useless for one-element arrays. Here, we can just use zend_hash_sort_ex instead for this case.

Some may ask what's the meaning to sort one-element array anyways. This can't be similar to what sorting zero-element array do, which is directly returning the empty array. Because the sorting functions have other things to do rather than just sort the elements.

<?php
$array = ['key' => 42];
sort($array);

var_dump($array);
// [0 => 42]

Here, we remain the original sort behavior for one-element array while making it faster because we are not actually sorting them

benchmark results:

After: 448.923 ms
Before: 905.807 ms
a roughly 50.4% enhancement in this specific case.

script:

<?php

const ITERATIONS = 20_000_000;
const RUNS = 7;

function run(int $iterations): float
{
    $array = [42];

    $start = hrtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        sort($array, SORT_NUMERIC);
    }

    return (hrtime(true) - $start) / 1e6;
}

run(100_000); // Warmup

$times = [];
for ($run = 0; $run < RUNS; $run++) {
    $times[] = run(ITERATIONS);
}

sort($times, SORT_NUMERIC);
$median = $times[intdiv(RUNS, 2)];

printf(
    "single-element sort: %.3f ms (%d iterations, median of %d)\n",
    $median,
    ITERATIONS,
    RUNS,
);

@devnexen

devnexen commented Sep 3, 2026

Copy link
Copy Markdown
Member

unless Gina beats me to it, I ll review a bit later seems good not entirely sure it s bug free

@devnexen

devnexen commented Sep 3, 2026

Copy link
Copy Markdown
Member

I know it s performance oriented and with your last change it seems ok, but try to think of a test oriented regression, e.g. making sure a refactoring does not break it.

@LamentXU123
LamentXU123 merged commit 0bf872b into php:master Sep 3, 2026
18 checks passed
@LamentXU123

Copy link
Copy Markdown
Member Author

Thanks !

@Girgias

Girgias commented Sep 3, 2026

Copy link
Copy Markdown
Member

Is this really a case worth optimising? How often does one actually try to sort a single element array?

@LamentXU123

Copy link
Copy Markdown
Member Author

Is this really a case worth optimising? How often does one actually try to sort a single element array?

It makes no sense to sort an empty array either. But we have a fast-path for it. Not mentioning sorting one-element array actually has some meaningful behaviors.

@Girgias

Girgias commented Sep 3, 2026

Copy link
Copy Markdown
Member

Is this really a case worth optimising? How often does one actually try to sort a single element array?

It makes no sense to sort an empty array either. But we have a fast-path for it. Not mentioning sorting one-element array actually has some meaningful behaviors.

yeah I don't know why we are optimising empty arrays at least that should just be something the optimizer decides it can elide and not the function handling it...

@LamentXU123

Copy link
Copy Markdown
Member Author

Is this really a case worth optimising? How often does one actually try to sort a single element array?

It makes no sense to sort an empty array either. But we have a fast-path for it. Not mentioning sorting one-element array actually has some meaningful behaviors.

yeah I don't know why we are optimising empty arrays at least that should just be something the optimizer decides it can elide and not the function handling it...

I think it is meaningful for these fast-path optimizations. It doesn't have downside anyways and AFAIK the parameters of sort is usually dynamic, in many cases it might be sorting empty or single arrays.

And yes, I think of migrating it to optimizer before. I am not familiar with it and I don't sure it can be smart enough to catch all cases. If it is this might be a good follow-up

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.

3 participants