Skip to content

Commit ad2ef3d

Browse files
committed
Add only_integer_keys option to zend_hash_reindex
This allows to either do a full reindex, or only reindex the integer keys.
1 parent f485c84 commit ad2ef3d

File tree

3 files changed

+9
-30
lines changed

3 files changed

+9
-30
lines changed

Zend/zend_hash.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ ZEND_API int zend_hash_rehash(HashTable *ht)
480480
return SUCCESS;
481481
}
482482

483-
ZEND_API void zend_hash_reindex(HashTable *ht) {
483+
ZEND_API void zend_hash_reindex(HashTable *ht, zend_bool only_integer_keys) {
484484
Bucket *p;
485485
uint nIndex;
486486
ulong offset = 0;
@@ -492,8 +492,9 @@ ZEND_API void zend_hash_reindex(HashTable *ht) {
492492

493493
memset(ht->arBuckets, 0, ht->nTableSize * sizeof(Bucket *));
494494
for (p = ht->pListHead; p != NULL; p = p->pListNext) {
495-
if (p->nKeyLength == 0) {
495+
if (!only_integer_keys || p->nKeyLength == 0) {
496496
p->h = offset++;
497+
p->nKeyLength = 0;
497498
}
498499

499500
nIndex = p->h & ht->nTableMask;
@@ -1353,7 +1354,7 @@ ZEND_API void _zend_hash_splice(HashTable *ht, uint nDataSize, copy_ctor_func_t
13531354
ZEND_HASH_IF_FULL_DO_RESIZE(ht);
13541355
}
13551356

1356-
zend_hash_reindex(ht);
1357+
zend_hash_reindex(ht, 1);
13571358
}
13581359
/* }}} */
13591360

@@ -1403,15 +1404,7 @@ ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func,
14031404
HANDLE_UNBLOCK_INTERRUPTIONS();
14041405

14051406
if (renumber) {
1406-
p = ht->pListHead;
1407-
i=0;
1408-
while (p != NULL) {
1409-
p->nKeyLength = 0;
1410-
p->h = i++;
1411-
p = p->pListNext;
1412-
}
1413-
ht->nNextFreeElement = i;
1414-
zend_hash_rehash(ht);
1407+
zend_hash_reindex(ht, 0);
14151408
}
14161409
return SUCCESS;
14171410
}

Zend/zend_hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ ZEND_API int zend_hash_minmax(const HashTable *ht, compare_func_t compar, int fl
227227
ZEND_API int zend_hash_num_elements(const HashTable *ht);
228228

229229
ZEND_API int zend_hash_rehash(HashTable *ht);
230-
ZEND_API void zend_hash_reindex(HashTable *ht);
230+
ZEND_API void zend_hash_reindex(HashTable *ht, zend_bool only_integer_keys);
231231

232232
ZEND_API void _zend_hash_splice(HashTable *ht, uint nDataSize, copy_ctor_func_t pCopyConstructor, uint offset, uint length, void **list, uint list_count, HashTable *removed ZEND_FILE_LINE_DC);
233233
#define zend_hash_splice(ht, nDataSize, pCopyConstructor, offset, length, list, list_count, removed) \

ext/standard/array.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,13 +1786,7 @@ static void php_array_data_shuffle(zval *array TSRMLS_DC) /* {{{ */
17861786
}
17871787
temp = hash->pListHead;
17881788
j = 0;
1789-
while (temp != NULL) {
1790-
temp->nKeyLength = 0;
1791-
temp->h = j++;
1792-
temp = temp->pListNext;
1793-
}
1794-
hash->nNextFreeElement = n_elems;
1795-
zend_hash_rehash(hash);
1789+
zend_hash_reindex(hash, 0);
17961790
HANDLE_UNBLOCK_INTERRUPTIONS();
17971791

17981792
efree(elems);
@@ -1897,7 +1891,7 @@ static void _phpi_pop(INTERNAL_FUNCTION_PARAMETERS, int off_the_end)
18971891

18981892
/* If we did a shift... re-index like it did before */
18991893
if (!off_the_end) {
1900-
zend_hash_reindex(Z_ARRVAL_P(stack));
1894+
zend_hash_reindex(Z_ARRVAL_P(stack), 1);
19011895
} else if (!key_len && index >= Z_ARRVAL_P(stack)->nNextFreeElement - 1) {
19021896
Z_ARRVAL_P(stack)->nNextFreeElement = Z_ARRVAL_P(stack)->nNextFreeElement - 1;
19031897
}
@@ -3848,15 +3842,7 @@ PHP_FUNCTION(array_multisort)
38483842
hash->pListTail = indirect[k][i];
38493843
}
38503844

3851-
p = hash->pListHead;
3852-
k = 0;
3853-
while (p != NULL) {
3854-
if (p->nKeyLength == 0)
3855-
p->h = k++;
3856-
p = p->pListNext;
3857-
}
3858-
hash->nNextFreeElement = array_size;
3859-
zend_hash_rehash(hash);
3845+
zend_hash_reindex(hash, 1);
38603846
}
38613847
HANDLE_UNBLOCK_INTERRUPTIONS();
38623848

0 commit comments

Comments
 (0)