Skip to content

Commit

Permalink
adds memoization to speed up collection of large dependency trees
Browse files Browse the repository at this point in the history
  • Loading branch information
filecage committed Jul 2, 2019
1 parent e182ffa commit 5eac1af
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class Dependency {
*/
private $innerDependencies;

/**
* Static memoization of class dependencies to speed up lookups for big trees in larger projects
* We're caching by class name as a class can not be overwritten
*
* @var Dependency[]
*/
static private $dependencies = [];

/**
* @param \ReflectionFunctionAbstract $reflectionFunction
* @return \Generator
Expand Down Expand Up @@ -62,11 +70,16 @@ static function createFromReflectionParameter(\ReflectionParameter $dependencyPa
// If the parameter refers to a class, we have to find it's inner dependencies
if ($type !== null && $type->isBuiltin() === false) {
$dependencyClassName = $type->getName();
if (!class_exists($dependencyClassName, false)) {

if (isset(static::$dependencies[$dependencyClassName])) {
return static::$dependencies[$dependencyClassName];
} elseif (!class_exists($dependencyClassName, false)) {
$dependency = new static(false, $parameterName, $dependencyClassName, null);
} else {
$dependency = static::createFromCreatable($parameterName, new Creatable($dependencyParameter->getClass()));
static::$dependencies[$dependencyClassName] = $dependency;
}

} else {
$dependency = new static(true, $parameterName, null, null);
}
Expand Down

0 comments on commit 5eac1af

Please sign in to comment.