From 76ecc214a93b763c29b924277e85f64326f9fbb2 Mon Sep 17 00:00:00 2001 From: Bastien Jaillot Date: Tue, 7 Jan 2020 22:43:22 +0100 Subject: [PATCH] [Serializer] Fix cache in MetadataAwareNameConverter `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 --- NameConverter/MetadataAwareNameConverter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NameConverter/MetadataAwareNameConverter.php b/NameConverter/MetadataAwareNameConverter.php index bbf70eaac..a3ffaa3df 100644 --- a/NameConverter/MetadataAwareNameConverter.php +++ b/NameConverter/MetadataAwareNameConverter.php @@ -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); } @@ -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); } @@ -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; } @@ -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); }