Skip to content

Commit

Permalink
Updated Rector to commit 50407e2f2d1c2eb7f67c1f59e8db777e6f0bd081
Browse files Browse the repository at this point in the history
rectorphp/rector-src@50407e2 [Performance] Ensure call $reflector->reflectAllClasses() once on Worker take 3 (#5878)
  • Loading branch information
TomasVotruba committed May 14, 2024
1 parent 89f91b6 commit efe89cf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
namespace Rector\Application;

use RectorPrefix202405\Nette\Utils\FileSystem as UtilsFileSystem;
use Rector\Caching\Cache;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Caching\Enum\CacheKey;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Configuration\VendorMissAnalyseGuard;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Parallel\Application\ParallelFileProcessor;
use Rector\Provider\CurrentFileProvider;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
Expand Down Expand Up @@ -77,6 +80,16 @@ final class ApplicationFileProcessor
* @var \Rector\Configuration\VendorMissAnalyseGuard
*/
private $vendorMissAnalyseGuard;
/**
* @readonly
* @var \Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider
*/
private $dynamicSourceLocatorProvider;
/**
* @readonly
* @var \Rector\Caching\Cache
*/
private $cache;
/**
* @var string
*/
Expand All @@ -85,7 +98,7 @@ final class ApplicationFileProcessor
* @var SystemError[]
*/
private $systemErrors = [];
public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory, ParallelFileProcessor $parallelFileProcessor, ScheduleFactory $scheduleFactory, CpuCoreCountProvider $cpuCoreCountProvider, ChangedFilesDetector $changedFilesDetector, CurrentFileProvider $currentFileProvider, \Rector\Application\FileProcessor $fileProcessor, ArrayParametersMerger $arrayParametersMerger, VendorMissAnalyseGuard $vendorMissAnalyseGuard)
public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory, ParallelFileProcessor $parallelFileProcessor, ScheduleFactory $scheduleFactory, CpuCoreCountProvider $cpuCoreCountProvider, ChangedFilesDetector $changedFilesDetector, CurrentFileProvider $currentFileProvider, \Rector\Application\FileProcessor $fileProcessor, ArrayParametersMerger $arrayParametersMerger, VendorMissAnalyseGuard $vendorMissAnalyseGuard, DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Cache $cache)
{
$this->symfonyStyle = $symfonyStyle;
$this->fileFactory = $fileFactory;
Expand All @@ -97,6 +110,8 @@ public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory
$this->fileProcessor = $fileProcessor;
$this->arrayParametersMerger = $arrayParametersMerger;
$this->vendorMissAnalyseGuard = $vendorMissAnalyseGuard;
$this->dynamicSourceLocatorProvider = $dynamicSourceLocatorProvider;
$this->cache = $cache;
}
public function run(Configuration $configuration, InputInterface $input) : ProcessResult
{
Expand All @@ -109,6 +124,9 @@ public function run(Configuration $configuration, InputInterface $input) : Proce
if ($filePaths === []) {
return new ProcessResult([], []);
}
// ensure clear classnames collection caches on repetitive call
$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->dynamicSourceLocatorProvider->getCacheClassNameKey();
$this->cache->clean($key);
$this->configureCustomErrorHandler();
/**
* Mimic @see https://github.com/phpstan/phpstan-src/blob/ab154e1da54d42fec751e17a1199b3e07591e85e/src/Command/AnalyseApplication.php#L188C23-L244
Expand All @@ -132,6 +150,8 @@ public function run(Configuration $configuration, InputInterface $input) : Proce
} else {
$preFileCallback = null;
}
// trigger cache class names collection
$this->dynamicSourceLocatorProvider->provide();
if ($configuration->isParallel()) {
$processResult = $this->runParallel($filePaths, $configuration, $input, $postFileCallback);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'a5bf80cb2fdd49d64777cf13250e7205ddd0a3be';
public const PACKAGE_VERSION = '50407e2f2d1c2eb7f67c1f59e8db777e6f0bd081';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-05-14 20:46:32';
public const RELEASE_DATE = '2024-05-15 02:22:10';
/**
* @var int
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Caching/Enum/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ final class CacheKey
* @var string
*/
public const FILE_HASH_KEY = 'file_hash';
/**
* @var string
*/
public const CLASSNAMES_HASH_KEY = 'classnames_hash';
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
use Rector\StaticTypeMapper\PhpParser\StringNodeMapper;
use Rector\StaticTypeMapper\PhpParser\UnionTypeNodeMapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\Util\FileHasher;
use Rector\Utils\Command\MissingInSetCommand;
use Rector\Utils\Command\OutsideAnySetCommand;
use RectorPrefix202405\Symfony\Component\Console\Application;
Expand Down Expand Up @@ -290,7 +291,7 @@ public function create() : RectorConfig
return $phpStanServicesFactory->createDynamicSourceLocatorProvider();
});
$rectorConfig->afterResolving(DynamicSourceLocatorProvider::class, static function (DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Container $container) : void {
$dynamicSourceLocatorProvider->autowire($container->make(ReflectionProvider::class));
$dynamicSourceLocatorProvider->autowire($container->make(ReflectionProvider::class), $container->make(Cache::class), $container->make(FileHasher::class));
});
// resetables
$rectorConfig->tag(DynamicSourceLocatorProvider::class, ResetableInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Caching\Cache;
use Rector\Caching\Enum\CacheKey;
use Rector\Contract\DependencyInjection\ResetableInterface;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Rector\Util\FileHasher;
/**
* @api phpstan external
*/
Expand Down Expand Up @@ -46,19 +49,41 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
/**
* @var \Rector\Caching\Cache
*/
private $cache;
/**
* @var \Rector\Util\FileHasher
*/
private $fileHasher;
public function __construct(FileNodesFetcher $fileNodesFetcher, OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory)
{
$this->fileNodesFetcher = $fileNodesFetcher;
$this->optimizedDirectorySourceLocatorFactory = $optimizedDirectorySourceLocatorFactory;
}
public function autowire(ReflectionProvider $reflectionProvider) : void
public function autowire(ReflectionProvider $reflectionProvider, Cache $cache, FileHasher $fileHasher) : void
{
$this->reflectionProvider = $reflectionProvider;
$this->cache = $cache;
$this->fileHasher = $fileHasher;
}
public function setFilePath(string $filePath) : void
{
$this->filePaths = [$filePath];
}
public function getCacheClassNameKey() : string
{
$paths = [];
foreach ($this->filePaths as $filePath) {
$paths[] = (string) \realpath($filePath);
}
foreach ($this->directories as $directory) {
$paths[] = (string) \realpath($directory);
}
$paths = \array_filter($paths);
return CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->fileHasher->hash((string) \json_encode($paths));
}
/**
* @param string[] $files
*/
Expand Down Expand Up @@ -104,6 +129,18 @@ public function reset() : void
$this->directories = [];
$this->aggregateSourceLocator = null;
}
/**
* @param class-string[] $classNamesCache
*/
private function locateCachedClassNames(array $classNamesCache) : void
{
foreach ($classNamesCache as $classNameCache) {
try {
$this->reflectionProvider->getClass($classNameCache);
} catch (ClassNotFoundException $exception) {
}
}
}
/**
* @param OptimizedSingleFileSourceLocator[]|NewOptimizedDirectorySourceLocator[] $sourceLocators
*/
Expand All @@ -116,18 +153,32 @@ private function collectClasses(AggregateSourceLocator $aggregateSourceLocator,
if (\count($sourceLocators) === 1 && $sourceLocators[0] instanceof OptimizedSingleFileSourceLocator) {
return;
}
$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->getCacheClassNameKey();
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
if (\is_string($classNamesCache)) {
$classNamesCache = \json_decode($classNamesCache);
if (\is_array($classNamesCache)) {
$this->locateCachedClassNames($classNamesCache);
return;
}
}
$reflector = new DefaultReflector($aggregateSourceLocator);
$classNames = [];
// trigger collect "classes" on get class on locate identifier
try {
$reflections = $reflector->reflectAllClasses();
foreach ($reflections as $reflection) {
$className = $reflection->getName();
// make 'classes' collection
try {
$this->reflectionProvider->getClass($reflection->getName());
$this->reflectionProvider->getClass($className);
} catch (ClassNotFoundException $exception) {
continue;
}
$classNames[] = $className;
}
} catch (CouldNotReadFileException $exception) {
}
$this->cache->save($key, CacheKey::CLASSNAMES_HASH_KEY, \json_encode($classNames));
}
}

0 comments on commit efe89cf

Please sign in to comment.