Repository navigation
Speed up zend_hash_iterator_add() when iterators are nested - #23947
Open
nicolas-grekas wants to merge 1 commit into
Open
nicolas-grekas wants to merge 1 commit into
nicolas-grekas wants to merge 1 commit into
Conversation
nicolas-grekas
force-pushed
the
hash-iterator-add
branch
2 times, most recently
from
October 2, 2026 10:36
25bf9be to
f425ddb
Compare
nicolas-grekas
force-pushed
the
hash-iterator-add
branch
from
October 14, 2026 05:09
f425ddb to
bc762a8
Compare
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.
nicolas-grekas
force-pushed
the
hash-iterator-add
branch
from
November 12, 2026 17:24
bc762a8 to
9d14b9b
Compare
Contributor
Author
|
Rebased. I also removed the NEWS and UPGRADING entries, since they were the only cause of the conflicts. @ndossche @iluuu1994 would one of you have time for a look? Nothing changes when a single iterator is alive. When loops are nested, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zend_hash_iterator_add() looks for a free slot in EG(ht_iterators) starting from slot 0 every time, so a foreach by reference, a foreach over object properties, an ArrayIterator or array_walk() pays one probe per iterator that's already alive.
This adds EG(ht_iterators_free_hint): 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) don't change. 51451ab tried scanning backwards instead, and was reverted the next day by ceb77b6; keeping first-fit avoids changing which slot gets reused.
Found while profiling #23945: a recursive walk of 17.6k Twig nodes, each iterated through
new ArrayIterator($this->nodes), spends 2.4M instructions in zend_hash_iterator_add()/del(). That's 1.5M with this patch, -1.2% on the whole walk (callgrind).Instructions per
foreach ($a as &$v) {}over a 1-element array, nested in d-1 other loops by reference: 364 -> 365 at d=1, 389 -> 370 at d=5, 464 -> 369 at d=20. Same savings per loop for ArrayIterator, foreach over properties and array_walk().