Bug #71153 [NEW]: Performance Degradation in ArrayIterator with large arrays
| From: | kontakt at beberlei dot de | Date: | Thu, 17 Dec 2015 19:28:07 +0000 |
| Subject: | Bug #71153 [NEW]: Performance Degradation in ArrayIterator with large arrays | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-197973@lists.php.net to get a copy of this message | ||
From: kontakt at beberlei dot de
Operating system: Linux/Ubuntu 15.04
PHP version: 7.0.1
Package: *General Issues
Bug Type: Bug
Bug description:Performance Degradation in ArrayIterator with large arrays
Description:
------------
When creating array iterators with large array, iterating over them will
be *much* slower than just iterating over the array itself. The larger
the array gets the exponentially slower the iteration gets. Memory usage
increases as well.
For the same script PHP 5 doesn't have the problem, it is pretty fast on
any size of array iterator.
This problem was found in Twig, see my explanation comment:
https://github.com/twigphp/Twig/issues/1810
Test script:
---------------
<?php
class Node implements IteratorAggregate
{
private $nodes;
public function __construct(array $nodes)
{
$this->nodes = $nodes;
}
public function getIterator()
{
return new ArrayIterator($this->nodes);
}
public function getNodes()
{
return $this->nodes;
}
public function visitIterator()
{
foreach ($this as $node) {
$node->visitIterator();
}
}
public function visitNodes()
{
foreach ($this->getNodes() as $node) {
$node->visitNodes();
}
}
}
gc_disable();
$sizes = [1000, 10000, 20000, 65000, 66000, 100000];
foreach ($sizes as $size) {
echo "Array Size $size\n";
$nodes = [];
for ($i = 0; $i < $size; $i++) {
$nodes[] = new Node([]);
}
$node = new Node($nodes);
$time = microtime(true);
$node->visitNodes();
echo "Array: " . number_format(microtime(true) - $time , 4) . " " .
memory_get_peak_usage() . "\n";
$time = microtime(true);
$node->visitIterator();
echo "Iterator: " . number_format(microtime(true) - $time , 4) . " "
. memory_get_peak_usage() . "\n";
echo "\n";
}
Expected result:
----------------
PHP 7 being faster than PHP 5 on every size of ArrayIterator, not just
with size 1000.
Actual result:
--------------
Array Size 1000
Array: 0.0001 695232
Iterator: 0.0009 730208
Array Size 10000
Array: 0.0019 2464128
Iterator: 0.0607 3003872
Array Size 20000
Array: 0.0027 5898752
Iterator: 0.5189 5898752
Array Size 65000
Array: 0.0130 14741656
Iterator: 5.0322 14741656
Array Size 66000
Array: 0.0107 24079792
Iterator: 12.1315 24079792
Array Size 100000
Array: 0.0141 32198240
Iterator: 34.8082 32198240
--
Edit bug report at https://bugs.php.net/bug.php?id=71153&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71153&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71153&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71153&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71153&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71153&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71153&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71153&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71153&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71153&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71153&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71153&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71153&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71153&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71153&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71153&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71153&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71153&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71153&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71153&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71153&r=mysqlcfg