Bug #81554 [NEW]: RecursiveIteratorIterator still calls ->getChildren() when depth reaches limit

From: Date: Mon, 25 Oct 2021 15:39:45 +0000
Subject: Bug #81554 [NEW]: RecursiveIteratorIterator still calls ->getChildren() when depth reaches limit
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237363@lists.php.net to get a copy of this message
From: dktapps at pmmp dot io Operating system: Windows PHP version: 8.0.12 Package: *Directory/Filesystem functions Bug Type: Bug Bug description:RecursiveIteratorIterator still calls ->getChildren() when depth reaches limit Description: ------------ When a RecursiveIteratorIterator's depth reaches the limit, it still may call its sub-iterator's getChildren(). This manifests as performance degradation when using the below script on a directory with many thousands of files in it. This can be observed by replacing the iterators with a FilesystemIterator, which by default won't recurse anyway. As a result, it's 2 orders of magnitude faster than a RecursiveIteratorIterator with depth 0. With the target folder containing 30k files (NTFS on a PCIe Gen4 SSD): - RecursiveDirectoryIterator + maxDepth(0) takes 3.7 seconds - FilesystemIterator takes 0.03 seconds. This is most observable on Windows due to Windows' abysmal I/O performance. Test script: --------------- Slow script: <?php $iterator = new RecursiveDirectoryIterator(sys_get_temp_dir() . '/phpstan/cache/nette.configurator'); $iterator2 = new RecursiveIteratorIterator($iterator); $iterator2->setMaxDepth(0); $start = hrtime(true); foreach($iterator2 as $item){ } var_dump(number_format(hrtime(true) - $start)); -------- Fast script: <?php $iterator2 = new FilesystemIterator(sys_get_temp_dir() . '/phpstan/cache/nette.configurator'); $start = hrtime(true); foreach($iterator2 as $item){ } var_dump(number_format(hrtime(true) - $start)); Expected result: ---------------- The two scripts should be somewhere in the same order of magnitude of performance. Actual result: -------------- The fast script is more than 100x faster than the slow one. -- Edit bug report at https://bugs.php.net/bug.php?id=81554&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=81554&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=81554&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=81554&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=81554&r=needscript Try newer version: https://bugs.php.net/fix.php?id=81554&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=81554&r=support Expected behavior: https://bugs.php.net/fix.php?id=81554&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=81554&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=81554&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=81554&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=81554&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=81554&r=dst IIS Stability: https://bugs.php.net/fix.php?id=81554&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=81554&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=81554&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=81554&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=81554&r=mysqlcfg

« previous php.bugs (#237363) next »