Skip to content

Commit

Permalink
[Serializer] Fix cache in MetadataAwareNameConverter
Browse files Browse the repository at this point in the history
`isset` is used to test existence of values that is
`null` by default, which result to always bypass the cache
and force to do the calculate all the time.

This is a critical perf improvement in prod mode for an api.

Ref #35085
  • Loading branch information
bastnic committed Jan 7, 2020
1 parent 0a86745 commit 76ecc21
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions NameConverter/MetadataAwareNameConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function normalize($propertyName, string $class = null, string $format =
return $this->normalizeFallback($propertyName, $class, $format, $context);
}

if (!isset(self::$normalizeCache[$class][$propertyName])) {
if (!\array_key_exists($class, self::$normalizeCache) || !\array_key_exists($propertyName, self::$normalizeCache[$class])) {
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
}

Expand All @@ -64,7 +64,7 @@ public function denormalize($propertyName, string $class = null, string $format
}

$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$denormalizeCache[$cacheKey][$propertyName])) {
if (!\array_key_exists($cacheKey, self::$denormalizeCache) || !\array_key_exists($propertyName, self::$denormalizeCache[$cacheKey])) {
self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
}

Expand All @@ -78,7 +78,7 @@ private function getCacheValueForNormalization(string $propertyName, string $cla
}

$attributesMetadata = $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata();
if (!isset($attributesMetadata[$propertyName])) {
if (!\array_key_exists($propertyName, $attributesMetadata)) {
return null;
}

Expand All @@ -93,7 +93,7 @@ private function normalizeFallback(string $propertyName, string $class = null, s
private function getCacheValueForDenormalization(string $propertyName, string $class, array $context): ?string
{
$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$attributesMetadataCache[$cacheKey])) {
if (!\array_key_exists($cacheKey, self::$attributesMetadataCache)) {
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context);
}

Expand Down

0 comments on commit 76ecc21

Please # to comment.