From e3db494ebee3f4c86fb7b70c66bfa57e8c509cf2 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 24 Oct 2021 20:07:34 +0100 Subject: [PATCH 1/4] Significantly improved performance of ContainerFactory::clearOldContainers() on Windows Use native FilesystemIterator instead of Symfony Finder. This is because FilesystemIterator does not recurse. I observed that over 50% of the time spent in this function was just calling RecursiveDirectoryIterator::hasChildren(). Eliminating this slashes 1000ms off pre-analysis time on my laptop when the tmpdir contains 30k files (5.6sec vs 4.6sec). --- src/DependencyInjection/ContainerFactory.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index d1fa6f5746..72990bdf57 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -138,15 +138,21 @@ public function clearOldContainers(string $tempDirectory): void $configurator->setDebugMode(true); $configurator->setTempDirectory($tempDirectory); - $finder = new Finder(); - $finder->name('Container_*')->in($configurator->getContainerCacheDirectory()); + $iterator = new \FilesystemIterator($configurator->getContainerCacheDirectory(), \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS); $twoDaysAgo = time() - 24 * 60 * 60 * 2; - foreach ($finder as $containerFile) { + /** + * @var string $fileName + * @var \SplFileInfo $containerFile + */ + foreach ($iterator as $fileName => $containerFile) { $path = $containerFile->getRealPath(); if ($path === false) { continue; } + if(preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1){ + continue; + } if ($containerFile->getATime() > $twoDaysAgo) { continue; } From 5d80fb95b33bfa1e5c2bd1dcc26c396fd60c095b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 24 Oct 2021 20:17:18 +0100 Subject: [PATCH 2/4] Move getRealPath() call to where it's actually needed this shaves off an additional 1300ms in the same scenario as previous. --- src/DependencyInjection/ContainerFactory.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 72990bdf57..0a344ca2d5 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -146,10 +146,6 @@ public function clearOldContainers(string $tempDirectory): void * @var \SplFileInfo $containerFile */ foreach ($iterator as $fileName => $containerFile) { - $path = $containerFile->getRealPath(); - if ($path === false) { - continue; - } if(preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1){ continue; } @@ -160,6 +156,10 @@ public function clearOldContainers(string $tempDirectory): void continue; } + $path = $containerFile->getRealPath(); + if ($path === false) { + continue; + } @unlink($path); } } From 635c2562459efdadbb353a874aa5fd46571d2de3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 24 Oct 2021 20:35:21 +0100 Subject: [PATCH 3/4] fix CS --- src/DependencyInjection/ContainerFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 0a344ca2d5..f143c078ee 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -10,7 +10,6 @@ use PHPStan\Command\CommandHelper; use PHPStan\File\FileHelper; use PHPStan\Php\PhpVersion; -use Symfony\Component\Finder\Finder; use function sys_get_temp_dir; /** @api */ @@ -146,7 +145,7 @@ public function clearOldContainers(string $tempDirectory): void * @var \SplFileInfo $containerFile */ foreach ($iterator as $fileName => $containerFile) { - if(preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1){ + if (preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1) { continue; } if ($containerFile->getATime() > $twoDaysAgo) { From 5b784ad1019edad52a05aa422e9d994589de0c83 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 24 Oct 2021 20:48:19 +0100 Subject: [PATCH 4/4] YUUUUUUUGE performance improvement for full analysis with heavily populated cache this change reduces full analysis time of pmmp/PocketMine-MP from 33sec to 13sec (no result cache) --- src/Command/CommandHelper.php | 7 +++++-- src/Command/WorkerCommand.php | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Command/CommandHelper.php b/src/Command/CommandHelper.php index 1a5caa2dce..b215d986b5 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -48,7 +48,8 @@ public static function begin( bool $manageMemoryLimitFile = true, bool $debugEnabled = false, ?string $singleReflectionFile = null, - ?string $singleReflectionInsteadOfFile = null + ?string $singleReflectionInsteadOfFile = null, + bool $cleanupContainerCache = true ): InceptionResult { if (!$allowXdebug) { @@ -257,7 +258,9 @@ public static function begin( throw new \PHPStan\Command\InceptionNotSuccessfulException(); } - $containerFactory->clearOldContainers($tmpDir); + if ($cleanupContainerCache) { + $containerFactory->clearOldContainers($tmpDir); + } if (count($paths) === 0) { $errorOutput->writeLineFormatted('At least one path must be specified to analyse.'); diff --git a/src/Command/WorkerCommand.php b/src/Command/WorkerCommand.php index e1154d3829..54890ed488 100644 --- a/src/Command/WorkerCommand.php +++ b/src/Command/WorkerCommand.php @@ -111,7 +111,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $allowXdebug, false, false, - $singleReflectionFile + $singleReflectionFile, + null, + false ); } catch (\PHPStan\Command\InceptionNotSuccessfulException $e) { return 1;