Skip to content

Commit

Permalink
minor #32188 [PropertyInfo] add static cache to ContextFactory (bastnic)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[PropertyInfo] add static cache to ContextFactory

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no (performance...)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32073, api-platform/api-platform#1159
| License       | MIT
| Doc PR        | no

The issue is very very well described here #32073, and was also discussed a few weeks ago with @dunglas here api-platform/api-platform#1159.

`ContextFactory::createFromReflector` is heavy, and it's called redundanlty
by `Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor` for each property
and methods. Avoid that by parsing it only once and then use static cache

This is a quite big performance problem.

![Deepin Capture-écran_zone de sélection _20190626142041](https://user-images.githubusercontent.com/84887/60179692-8471c480-981e-11e9-9e3c-3f9c0b83b01b.png)

Commits
-------

063e880 [PropertyInfo] add static cache to ContextFactory
  • Loading branch information
fabpot committed Jul 3, 2019
2 parents df13b50 + 063e880 commit 2452ef8
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
Expand Up @@ -14,6 +14,7 @@
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\ContextFactory;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
Expand All @@ -38,6 +39,11 @@ class PhpDocExtractor implements PropertyDescriptionExtractorInterface, Property
*/
private $docBlocks = [];

/**
* @var Context[]
*/
private $contexts = [];

private $docBlockFactory;
private $contextFactory;
private $phpDocTypeHelper;
Expand Down Expand Up @@ -191,7 +197,7 @@ private function getDocBlockFromProperty(string $class, string $property): ?DocB
}

try {
return $this->docBlockFactory->create($reflectionProperty, $this->contextFactory->createFromReflector($reflectionProperty->getDeclaringClass()));
return $this->docBlockFactory->create($reflectionProperty, $this->createFromReflector($reflectionProperty->getDeclaringClass()));
} catch (\InvalidArgumentException $e) {
return null;
}
Expand Down Expand Up @@ -227,9 +233,25 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
}

try {
return [$this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix];
return [$this->docBlockFactory->create($reflectionMethod, $this->createFromReflector($reflectionMethod->getDeclaringClass())), $prefix];
} catch (\InvalidArgumentException $e) {
return null;
}
}

/**
* Prevents a lot of redundant calls to ContextFactory::createForNamespace().
*/
private function createFromReflector(\ReflectionClass $reflector): Context
{
$cacheKey = $reflector->getNamespaceName().':'.$reflector->getFileName();

if (isset($this->contexts[$cacheKey])) {
return $this->contexts[$cacheKey];
}

$this->contexts[$cacheKey] = $this->contextFactory->createFromReflector($reflector);

return $this->contexts[$cacheKey];
}
}

0 comments on commit 2452ef8

Please sign in to comment.