From cd0cd9c148d3bdd3f7d8a91fc88d2cff998d2484 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Fri, 17 May 2024 15:02:50 +0200 Subject: [PATCH] [Serializer] Cache readability/writability computation --- Normalizer/ObjectNormalizer.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Normalizer/ObjectNormalizer.php b/Normalizer/ObjectNormalizer.php index 45f27203..f8fd93f3 100644 --- a/Normalizer/ObjectNormalizer.php +++ b/Normalizer/ObjectNormalizer.php @@ -35,6 +35,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer { private static $reflectionCache = []; + private static $isReadableCache = []; + private static $isWritableCache = []; protected $propertyAccessor; protected $propertyInfoExtractor; @@ -185,21 +187,23 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) { return false; } + $class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject; if ($context['_read_attributes'] ?? true) { - return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute); - } + if (!isset(self::$isReadableCache[$class.$attribute])) { + self::$isReadableCache[$class.$attribute] = $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute); + } - if ($this->propertyInfoExtractor->isWritable($class, $attribute)) { - return true; + return self::$isReadableCache[$class.$attribute]; } - if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) { - return true; + if (!isset(self::$isWritableCache[$class.$attribute])) { + self::$isWritableCache[$class.$attribute] = $this->propertyInfoExtractor->isWritable($class, $attribute) + || (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()); } - return false; + return self::$isWritableCache[$class.$attribute]; } private function hasAttributeAccessorMethod(string $class, string $attribute): bool