Skip to content
Open
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ PHP NEWS
especially UTF-8. (Nicolas Grekas)
. Improved performance of htmlspecialchars() when nothing needs encoding.
(Nicolas Grekas)
. Improved performance of array_unshift() on packed arrays without holes.
(mehmetcansahin)

- URI:
. Fix casing of enum cases in UriHostType and UrlHostType to match the RFC
Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ PHP 8.7 UPGRADE NOTES
. Improved performance of array_chunk() when not preserving keys.
. Improved performance of array_splice() when inserting without removing
elements.
. Improved performance of array_unshift() on packed arrays without holes.
. Improved performance of str_rot13().
. Improved performance of pack() for large strings using the a, A and Z
formats.
Expand Down
63 changes: 42 additions & 21 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3696,9 +3696,30 @@ PHP_FUNCTION(array_unshift)
Z_PARAM_VARIADIC('+', args, argc)
ZEND_PARSE_PARAMETERS_END();

zend_hash_init(&new_hash, zend_hash_num_elements(Z_ARRVAL_P(stack)) + argc, NULL, ZVAL_PTR_DTOR, 0);
HashTable *ht = Z_ARRVAL_P(stack);
uint32_t new_count = zend_hash_num_elements(ht) + argc;

if (HT_IS_PACKED(Z_ARRVAL_P(stack))) {
/* The rebuild below allocates the smallest power of two that fits new_count,
* but at least HT_MIN_SIZE slots. If the current table has that size, a
* packed array without holes can be shifted in place instead. */
bool same_table_size = new_count <= ht->nTableSize
&& (new_count > ht->nTableSize / 2 || ht->nTableSize == HT_MIN_SIZE);

if (HT_IS_PACKED(ht) && HT_IS_WITHOUT_HOLES(ht) && !HT_HAS_ITERATORS(ht) && same_table_size) {
memmove(ht->arPacked + argc, ht->arPacked, ht->nNumUsed * sizeof(zval));
for (uint32_t i = 0; i < argc; i++) {
ZVAL_COPY(&ht->arPacked[i], &args[i]);
}
ht->nNumUsed = new_count;
ht->nNumOfElements = new_count;
ht->nNextFreeElement = new_count;
ht->nInternalPointer = 0;
RETURN_LONG(new_count);
}

zend_hash_init(&new_hash, new_count, NULL, ZVAL_PTR_DTOR, 0);

if (HT_IS_PACKED(ht)) {
zend_hash_real_init_packed(&new_hash);

ZEND_HASH_FILL_PACKED(&new_hash) {
Expand All @@ -3707,7 +3728,7 @@ PHP_FUNCTION(array_unshift)
ZEND_HASH_FILL_ADD(&args[i]);
}

ZEND_HASH_PACKED_FOREACH_VAL(Z_ARRVAL_P(stack), value) {
ZEND_HASH_PACKED_FOREACH_VAL(ht, value) {
ZEND_HASH_FILL_ADD(value);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FILL_END();
Expand All @@ -3717,7 +3738,7 @@ PHP_FUNCTION(array_unshift)
zend_hash_next_index_insert_new(&new_hash, &args[i]);
}

ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(stack), key, value) {
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht, key, value) {
if (key) {
zend_hash_add_new(&new_hash, key, value);
} else {
Expand All @@ -3726,29 +3747,29 @@ PHP_FUNCTION(array_unshift)
} ZEND_HASH_FOREACH_END();
}

if (UNEXPECTED(HT_HAS_ITERATORS(Z_ARRVAL_P(stack)))) {
zend_hash_iterators_advance(Z_ARRVAL_P(stack), argc);
HT_SET_ITERATORS_COUNT(&new_hash, HT_ITERATORS_COUNT(Z_ARRVAL_P(stack)));
HT_SET_ITERATORS_COUNT(Z_ARRVAL_P(stack), 0);
if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
zend_hash_iterators_advance(ht, argc);
HT_SET_ITERATORS_COUNT(&new_hash, HT_ITERATORS_COUNT(ht));
HT_SET_ITERATORS_COUNT(ht, 0);
}

/* replace HashTable data */
Z_ARRVAL_P(stack)->pDestructor = NULL;
zend_hash_destroy(Z_ARRVAL_P(stack));

HT_FLAGS(Z_ARRVAL_P(stack)) = HT_FLAGS(&new_hash);
Z_ARRVAL_P(stack)->nTableSize = new_hash.nTableSize;
Z_ARRVAL_P(stack)->nTableMask = new_hash.nTableMask;
Z_ARRVAL_P(stack)->nNumUsed = new_hash.nNumUsed;
Z_ARRVAL_P(stack)->nNumOfElements = new_hash.nNumOfElements;
Z_ARRVAL_P(stack)->nNextFreeElement = new_hash.nNextFreeElement;
Z_ARRVAL_P(stack)->arData = new_hash.arData;
Z_ARRVAL_P(stack)->pDestructor = new_hash.pDestructor;
ht->pDestructor = NULL;
zend_hash_destroy(ht);

zend_hash_internal_pointer_reset(Z_ARRVAL_P(stack));
HT_FLAGS(ht) = HT_FLAGS(&new_hash);
ht->nTableSize = new_hash.nTableSize;
ht->nTableMask = new_hash.nTableMask;
ht->nNumUsed = new_hash.nNumUsed;
ht->nNumOfElements = new_hash.nNumOfElements;
ht->nNextFreeElement = new_hash.nNextFreeElement;
ht->arData = new_hash.arData;
ht->pDestructor = new_hash.pDestructor;

zend_hash_internal_pointer_reset(ht);

/* Clean up and return the number of elements in the stack */
RETVAL_LONG(zend_hash_num_elements(Z_ARRVAL_P(stack)));
RETVAL_LONG(zend_hash_num_elements(ht));
}
/* }}} */

Expand Down
102 changes: 102 additions & 0 deletions ext/standard/tests/array/array_unshift_packed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
--TEST--
array_unshift() on packed arrays
--FILE--
<?php
function dump(array $a): void {
echo json_encode($a), ' key: ', var_export(key($a), true), "\n";
}

// The values fill the remaining slots of the 16-slot table exactly
$a = range(1, 14);
end($a);
var_dump(array_unshift($a, -1, 0));
dump($a);

// The 16-slot table has no slot left for the value
$a = range(1, 16);
end($a);
var_dump(array_unshift($a, 0));
dump($a);

// A hole in the middle
$a = [0, 1, 2, 3];
unset($a[1]);
var_dump(array_unshift($a, -1));
dump($a);

// The next index left by removed last elements is reset
$a = [0, 1, 2, 3, 4];
unset($a[4], $a[3]);
var_dump(array_unshift($a, -1));
$a[] = 'next';
dump($a);

// Without values, only the internal pointer and the next index are reset
$a = [0, 1, 2];
unset($a[2]);
end($a);
var_dump(array_unshift($a));
$a[] = 'next';
dump($a);

// An emptied packed array keeps its table
$a = [1];
array_pop($a);
var_dump(array_unshift($a, 1, 2));
$a[] = 'next';
dump($a);

// A foreach by reference keeps its position
$a = [1, 2, 3];
foreach ($a as &$v) {
echo $v, "\n";
if ($v == 2) {
array_unshift($a, 0);
}
}
unset($v);
dump($a);

// References stay shared and a copy is not modified
$x = 1;
$a = [&$x, 2];
$copy = $a;
array_unshift($a, 0);
$x = 10;
dump($a);
dump($copy);

// The array holds its own reference to the values
class Dtor {
public function __destruct() {
echo "destroyed\n";
}
}
$o = new Dtor();
$a = [1];
array_unshift($a, $o);
unset($o);
echo "unshifted\n";
$a = null;
?>
--EXPECT--
int(16)
[-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] key: 0
int(17)
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] key: 0
int(4)
[-1,0,2,3] key: 0
int(4)
[-1,0,1,2,"next"] key: 0
int(2)
[0,1,"next"] key: 0
int(2)
[1,2,"next"] key: 0
1
2
3
[0,1,2,3] key: 0
[0,10,2] key: 0
[10,2] key: 0
unshifted
destroyed
Loading