Skip to content

Commit 9d14b9b

Browse files
Speed up zend_hash_iterator_add() when iterators are nested
zend_hash_iterator_add() scanned EG(ht_iterators) from slot 0 on every call, so each nested foreach by reference, foreach over object properties, ArrayIterator or array_walk() paid one probe per iterator already alive. EG(ht_iterators_free_hint) tracks that all slots below it are in use: zend_hash_iterator_add() starts looking from there and zend_hash_iterator_del() lowers it. The returned slot is still the lowest free one, so slot indexes and EG(ht_iterators_used) are the same as before.
1 parent 6c0388f commit 9d14b9b

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎Zend/zend_execute_API.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ void init_executor(void) /* {{{ */
182182

183183
EG(ht_iterators_count) = sizeof(EG(ht_iterators_slots)) / sizeof(HashTableIterator);
184184
EG(ht_iterators_used) = 0;
185+
EG(ht_iterators_free_hint) = 0;
185186
EG(ht_iterators) = EG(ht_iterators_slots);
186187
memset(EG(ht_iterators), 0, sizeof(EG(ht_iterators_slots)));
187188

‎Zend/zend_globals.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct _zend_executor_globals {
282282

283283
uint32_t ht_iterators_count; /* number of allocated slots */
284284
uint32_t ht_iterators_used; /* number of used slots */
285+
uint32_t ht_iterators_free_hint; /* no free slot below this one */
285286
HashTableIterator *ht_iterators;
286287
HashTableIterator ht_iterators_slots[16];
287288

‎Zend/zend_hash.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -689,25 +689,26 @@ static void zend_hash_remove_iterator_copies(uint32_t idx) {
689689

690690
ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos)
691691
{
692-
HashTableIterator *iter = EG(ht_iterators);
693-
HashTableIterator *end = iter + EG(ht_iterators_count);
694-
uint32_t idx;
692+
/* Slots below the hint are in use, start looking for a free one from there */
693+
uint32_t idx = EG(ht_iterators_free_hint);
694+
HashTableIterator *iter;
695695

696696
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
697697
HT_INC_ITERATORS_COUNT(ht);
698698
}
699-
while (iter != end) {
699+
while (idx < EG(ht_iterators_count)) {
700+
iter = EG(ht_iterators) + idx;
700701
if (iter->ht == NULL) {
701702
iter->ht = ht;
702703
iter->pos = pos;
703-
idx = iter - EG(ht_iterators);
704704
iter->next_copy = idx;
705+
EG(ht_iterators_free_hint) = idx + 1;
705706
if (idx + 1 > EG(ht_iterators_used)) {
706707
EG(ht_iterators_used) = idx + 1;
707708
}
708709
return idx;
709710
}
710-
iter++;
711+
idx++;
711712
}
712713
if (EG(ht_iterators) == EG(ht_iterators_slots)) {
713714
EG(ht_iterators) = emalloc(sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
@@ -723,6 +724,7 @@ ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPositi
723724
idx = iter - EG(ht_iterators);
724725
iter->next_copy = idx;
725726
EG(ht_iterators_used) = idx + 1;
727+
EG(ht_iterators_free_hint) = idx + 1;
726728
return idx;
727729
}
728730

@@ -818,6 +820,9 @@ ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx)
818820
HT_DEC_ITERATORS_COUNT(iter->ht);
819821
}
820822
iter->ht = NULL;
823+
if (idx < EG(ht_iterators_free_hint)) {
824+
EG(ht_iterators_free_hint) = idx;
825+
}
821826

822827
if (UNEXPECTED(iter->next_copy != idx)) {
823828
zend_hash_remove_iterator_copies(idx);

0 commit comments

Comments
 (0)