[php-src] PR #11330: Implement iteration cache, item cache and length cache for node list iteration
| From: | nielsdos | Date: | Sat, 27 May 2023 22:35:55 +0000 |
| Subject: | [php-src] PR #11330: Implement iteration cache, item cache and length cache for node list iteration | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-22603@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/11330
Author: nielsdos
The current implementation follows the spec requirement that the list must be "live". This
means that changes in the document must be reflected in the existing node lists without requiring
the user to refetch the node list.
The consequence is that getting any item, or the length of the list, always starts searching from
the root element of the node list. This results in O(n) time to get any item or the length. If
there's a for loop over the node list, this means the iterations will take O(n²) time in
total. This causes real-world performance issues with potential for downtime (see GH-11308 and its
references for details).
We fix this by introducing a caching strategy. We cache the last iterated object in the iterator,
the last requested item in the node list, and the last length computation. To invalidate the cache,
we simply count the number of modifications made to the containing document. If the modification
number does not match what the number was during caching, we know the document has been modified and
the cache is invalid. If this ever overflows, we saturate the modification number and don't do
any caching anymore. Note that we don't check for overflow on 64-bit systems because it would
take hundreds of years to overflow.
Fixes GH-11308.