Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PropertyInfo] Use a single cache item per method #30525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions 35 src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, Prop
{
private $propertyInfoExtractor;
private $cacheItemPool;

/**
* A cache of property information, first keyed by the method called and
* then by the serialized method arguments.
*
* @var array
*/
private $arrayCache = [];

public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, CacheItemPoolInterface $cacheItemPool)
Expand Down Expand Up @@ -103,22 +110,34 @@ private function extract(string $method, array $arguments)
}

// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
$key = rawurlencode($method.'.'.$serializedArguments);

if (\array_key_exists($key, $this->arrayCache)) {
return $this->arrayCache[$key];
$encodedMethod = \rawurlencode($method);
if (\array_key_exists($encodedMethod, $this->arrayCache) && \array_key_exists($serializedArguments, $this->arrayCache[$encodedMethod])) {
return $this->arrayCache[$encodedMethod][$serializedArguments];
}

$item = $this->cacheItemPool->getItem($key);
$item = $this->cacheItemPool->getItem($encodedMethod);

$data = $item->get();
if ($item->isHit()) {
return $this->arrayCache[$key] = $item->get();
$this->arrayCache[$encodedMethod] = $data[$encodedMethod];
// 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);
$item->set($value);
$data[$encodedMethod][$serializedArguments] = $value;
$this->arrayCache[$encodedMethod][$serializedArguments] = $value;
$item->set($data);
$this->cacheItemPool->save($item);

return $this->arrayCache[$key] = $value;
return $this->arrayCache[$encodedMethod][$serializedArguments];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.