Skip to content

Commit 5f7ee32

Browse files
committed
bug symfony#54971 [Serializer] Cache readability/writability computation (mtarld)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Cache readability/writability computation | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#54865 | License | MIT Add in memory cache to reduce the number of times we check whether a property is readable or writable. Commits ------- 94fd291 [Serializer] Cache readability/writability computation
2 parents 3e67099 + 94fd291 commit 5f7ee32

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
class ObjectNormalizer extends AbstractObjectNormalizer
3636
{
3737
private static $reflectionCache = [];
38+
private static $isReadableCache = [];
39+
private static $isWritableCache = [];
3840

3941
protected $propertyAccessor;
4042
protected $propertyInfoExtractor;
@@ -185,21 +187,23 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
185187
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
186188
return false;
187189
}
190+
188191
$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;
189192

190193
if ($context['_read_attributes'] ?? true) {
191-
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
192-
}
194+
if (!isset(self::$isReadableCache[$class.$attribute])) {
195+
self::$isReadableCache[$class.$attribute] = $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
196+
}
193197

194-
if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
195-
return true;
198+
return self::$isReadableCache[$class.$attribute];
196199
}
197200

198-
if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
199-
return true;
201+
if (!isset(self::$isWritableCache[$class.$attribute])) {
202+
self::$isWritableCache[$class.$attribute] = $this->propertyInfoExtractor->isWritable($class, $attribute)
203+
|| (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType());
200204
}
201205

202-
return false;
206+
return self::$isWritableCache[$class.$attribute];
203207
}
204208

205209
private function hasAttributeAccessorMethod(string $class, string $attribute): bool

0 commit comments

Comments
 (0)