revert #30525 due to performance penalty

This commit is contained in:
Ben Davies 2019-06-25 10:08:34 +01:00
parent 67af93f3b0
commit 3d37cc98f6

View File

@ -24,13 +24,6 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, Prop
{ {
private $propertyInfoExtractor; private $propertyInfoExtractor;
private $cacheItemPool; private $cacheItemPool;
/**
* A cache of property information, first keyed by the method called and
* then by the serialized method arguments.
*
* @var array
*/
private $arrayCache = []; private $arrayCache = [];
public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, CacheItemPoolInterface $cacheItemPool) public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, CacheItemPoolInterface $cacheItemPool)
@ -110,34 +103,22 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, Prop
} }
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys // Calling rawurlencode escapes special characters not allowed in PSR-6's keys
$encodedMethod = rawurlencode($method); $key = rawurlencode($method.'.'.$serializedArguments);
if (\array_key_exists($encodedMethod, $this->arrayCache) && \array_key_exists($serializedArguments, $this->arrayCache[$encodedMethod])) {
return $this->arrayCache[$encodedMethod][$serializedArguments]; if (\array_key_exists($key, $this->arrayCache)) {
return $this->arrayCache[$key];
} }
$item = $this->cacheItemPool->getItem($encodedMethod); $item = $this->cacheItemPool->getItem($key);
$data = $item->get();
if ($item->isHit()) { if ($item->isHit()) {
$this->arrayCache[$encodedMethod] = $data[$encodedMethod]; return $this->arrayCache[$key] = $item->get();
// Only match if the specific arguments have been cached.
if (\array_key_exists($serializedArguments, $data[$encodedMethod])) {
return $this->arrayCache[$encodedMethod][$serializedArguments];
}
}
// It's possible that the method has been called, but with different
// arguments, in which case $data will already be initialized.
if (!$data) {
$data = [];
} }
$value = $this->propertyInfoExtractor->{$method}(...$arguments); $value = $this->propertyInfoExtractor->{$method}(...$arguments);
$data[$encodedMethod][$serializedArguments] = $value; $item->set($value);
$this->arrayCache[$encodedMethod][$serializedArguments] = $value;
$item->set($data);
$this->cacheItemPool->save($item); $this->cacheItemPool->save($item);
return $this->arrayCache[$encodedMethod][$serializedArguments]; return $this->arrayCache[$key] = $value;
} }
} }