Skip to content

Commit

Permalink
Updated Rector to commit 7dcd5f6503afa3ee7ab086cbfe6afb71cbd6d6ec
Browse files Browse the repository at this point in the history
rectorphp/rector-src@7dcd5f6 Performance: Improve AstResolver caching (#3485)
  • Loading branch information
TomasVotruba committed Mar 20, 2023
1 parent 1af1cd5 commit 5acf36b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 72 deletions.
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 = '5371408babdacb17ccbbe89b4c74281dcb4825c1';
public const PACKAGE_VERSION = '7dcd5f6503afa3ee7ab086cbfe6afb71cbd6d6ec';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-03-19 21:23:49';
public const RELEASE_DATE = '2023-03-20 15:25:39';
/**
* @var int
*/
Expand Down
85 changes: 25 additions & 60 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,11 @@ final class AstResolver
{
/**
* Parsing files is very heavy performance, so this will help to leverage it
* The value can be also null, as the method might not exist in the class.
* The value can be also null, when no statements could be parsed from the file.
*
* @var array<class-string, array<string, ClassMethod|null>>
* @var array<string, Stmt[]|null>
*/
private $classMethodsByClassAndMethod = [];
/**
* Parsing files is very heavy performance, so this will help to leverage it
* The value can be also null, as the method might not exist in the class.
*
* @var array<string, Function_|null>>
*/
private $functionsByName = [];
private $parsedFileNodes = [];
/**
* @readonly
* @var \Rector\PhpDocParser\PhpParser\SmartPhpParser
Expand Down Expand Up @@ -121,13 +114,6 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR
$classReflection = $methodReflection->getDeclaringClass();
$classLikeName = $classReflection->getName();
$methodName = $methodReflection->getName();
if (isset($this->classMethodsByClassAndMethod[$classLikeName][$methodName])) {
return $this->classMethodsByClassAndMethod[$classLikeName][$methodName];
}
// saved as null data
if (\array_key_exists($classLikeName, $this->classMethodsByClassAndMethod) && \array_key_exists($methodName, $this->classMethodsByClassAndMethod[$classLikeName])) {
return null;
}
$fileName = $classReflection->getFileName();
// probably native PHP method → un-parseable
if ($fileName === null) {
Expand All @@ -137,21 +123,13 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR
if ($nodes === null) {
return null;
}
/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($nodes, ClassLike::class);
foreach ($classLikes as $classLike) {
if (!$this->nodeNameResolver->isName($classLike, $classLikeName)) {
continue;
}
$classMethod = $classLike->getMethod($methodName);
if (!$classMethod instanceof ClassMethod) {
continue;
}
$this->classMethodsByClassAndMethod[$classLikeName][$methodName] = $classMethod;
return $classMethod;
/** @var ClassLike|null $classLike */
$classLike = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($classLikeName, $methodName) : bool {
return $node instanceof ClassLike && $this->nodeNameResolver->isName($node, $classLikeName) && $node->getMethod($methodName) instanceof ClassMethod;
});
if ($classLike instanceof ClassLike && ($method = $classLike->getMethod($methodName)) instanceof ClassMethod) {
return $method;
}
// avoids looking for a class in a file where is not present
$this->classMethodsByClassAndMethod[$classLikeName][$methodName] = null;
return null;
}
/**
Expand All @@ -168,13 +146,6 @@ public function resolveClassMethodOrFunctionFromCall($call, Scope $scope)
public function resolveFunctionFromFunctionReflection(FunctionReflection $functionReflection) : ?Function_
{
$functionName = $functionReflection->getName();
if (isset($this->functionsByName[$functionName])) {
return $this->functionsByName[$functionName];
}
// saved as null data
if (\array_key_exists($functionName, $this->functionsByName)) {
return null;
}
$fileName = $functionReflection->getFileName();
if ($fileName === null) {
return null;
Expand All @@ -183,19 +154,11 @@ public function resolveFunctionFromFunctionReflection(FunctionReflection $functi
if ($nodes === null) {
return null;
}
/** @var Function_[] $functions */
$functions = $this->betterNodeFinder->findInstanceOf($nodes, Function_::class);
foreach ($functions as $function) {
if (!$this->nodeNameResolver->isName($function, $functionName)) {
continue;
}
// to avoid parsing missing function again
$this->functionsByName[$functionName] = $function;
return $function;
}
// to avoid parsing missing function again
$this->functionsByName[$functionName] = null;
return null;
/** @var Function_|null $function */
$function = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($functionName) : bool {
return $node instanceof Function_ && $this->nodeNameResolver->isName($node, $functionName);
});
return $function;
}
/**
* @param class-string $className
Expand Down Expand Up @@ -282,12 +245,12 @@ public function resolvePropertyFromPropertyReflection(PhpPropertyReflection $php
}
$nativeReflectionProperty = $phpPropertyReflection->getNativeReflection();
$desiredPropertyName = $nativeReflectionProperty->getName();
/** @var Property[] $properties */
$properties = $this->betterNodeFinder->findInstanceOf($nodes, Property::class);
foreach ($properties as $property) {
if ($this->nodeNameResolver->isName($property, $desiredPropertyName)) {
return $property;
}
/** @var Property|null $property */
$property = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($desiredPropertyName) : bool {
return $node instanceof Property && $this->nodeNameResolver->isName($node, $desiredPropertyName);
});
if ($property instanceof Property) {
return $property;
}
// promoted property
return $this->findPromotedPropertyByName($nodes, $desiredPropertyName);
Expand All @@ -300,7 +263,6 @@ private function locateClassMethodInTrait(string $methodName, MethodReflection $
$classMethod = $this->betterNodeFinder->findFirst($traits, function (Node $node) use($methodName) : bool {
return $node instanceof ClassMethod && $this->nodeNameResolver->isName($node, $methodName);
});
$this->classMethodsByClassAndMethod[$classReflection->getName()][$methodName] = $classMethod;
if ($classMethod instanceof ClassMethod) {
return $classMethod;
}
Expand All @@ -311,12 +273,15 @@ private function locateClassMethodInTrait(string $methodName, MethodReflection $
*/
private function parseFileNameToDecoratedNodes(string $fileName) : ?array
{
if (isset($this->parsedFileNodes[$fileName])) {
return $this->parsedFileNodes[$fileName];
}
$stmts = $this->smartPhpParser->parseFile($fileName);
if ($stmts === []) {
return null;
return $this->parsedFileNodes[$fileName] = null;
}
$file = new File($fileName, FileSystem::read($fileName));
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
return $this->parsedFileNodes[$fileName] = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
}
/**
* @param Stmt[] $stmts
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e::getLoader();
return ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67::getLoader();
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e
class ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInitb7db68037830ca516c20ec2b65c9251e
class ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -3127,9 +3127,9 @@ class ComposerStaticInitb7db68037830ca516c20ec2b65c9251e
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$classMap;

}, null, ClassLoader::class);
}
Expand Down

0 comments on commit 5acf36b

Please sign in to comment.