Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ PHP 8.6 UPGRADE NOTES
. Reduced temporary allocations when iterating Phar directories.

- Standard:
. Improved performance of sorting single-element arrays.
. Improved performance of array_fill_keys().
. Improved performance of array_intersect().
. Improved performance of array_map() with multiple arrays passed.
Expand Down
14 changes: 11 additions & 3 deletions Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2997,9 +2997,17 @@ static void zend_hash_sort_internal(HashTable *ht, sort_func_t sort, bucket_comp

IS_CONSISTENT(ht);

if (!(ht->nNumOfElements>1) && !(renumber && ht->nNumOfElements>0)) {
/* Doesn't require sorting */
return;
if (ht->nNumOfElements <= 1) {
if (!renumber || ht->nNumOfElements == 0) {
/* Doesn't require sorting */
return;
}
if (sort == zend_sort && HT_IS_PACKED(ht) && HT_IS_WITHOUT_HOLES(ht)) {
/* The single element already has the expected index. */
ht->nInternalPointer = 0;
ht->nNextFreeElement = 1;
return;
}
}

if (HT_IS_PACKED(ht)) {
Expand Down
7 changes: 6 additions & 1 deletion Zend/zend_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ static zend_always_inline void ZEND_FASTCALL zend_hash_sort(HashTable *ht, bucke
* trigger user code. It will ensure the user code cannot free the array during
* sorting. */
static zend_always_inline void zend_array_sort(HashTable *ht, bucket_compare_func_t compare_func, bool renumber) {
zend_array_sort_ex(ht, zend_sort, compare_func, renumber);
/* zend_sort() cannot invoke the comparator for at most one element. */
if (ht->nNumOfElements <= 1) {
zend_hash_sort_ex(ht, zend_sort, compare_func, renumber);
} else {
zend_array_sort_ex(ht, zend_sort, compare_func, renumber);
}
}

static zend_always_inline uint32_t zend_hash_num_elements(const HashTable *ht) {
Expand Down
57 changes: 57 additions & 0 deletions ext/standard/tests/array/sort/sort_single_element.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
Sorting single-element arrays does not invoke the comparison function
--FILE--
<?php

$calls = 0;
$compare = static function ($a, $b) use (&$calls) {
$calls++;
return $a <=> $b;
};

$array = [42];
// Keep the array packed and without holes, but leave the next free index at 11.
$array[10] = 99;
unset($array[10]);
next($array);
var_dump(usort($array, $compare));
var_dump($array, key($array));
$array[] = 43;
var_dump($array);

$array = ['answer' => 42];
next($array);
var_dump(usort($array, $compare));
var_dump($array, key($array));
$array[] = 43;
var_dump($array);

var_dump($calls);

?>
--EXPECT--
bool(true)
array(1) {
[0]=>
int(42)
}
int(0)
array(2) {
[0]=>
int(42)
[1]=>
int(43)
}
bool(true)
array(1) {
[0]=>
int(42)
}
int(0)
array(2) {
[0]=>
int(42)
[1]=>
int(43)
}
int(0)
Loading