10000 [Serializer] Reduce class discriminator overhead v2 · symfony/symfony@0bd95ec · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bd95ec

Browse files
committed
[Serializer] Reduce class discriminator overhead v2
1 parent 90413e1 commit 0bd95ec

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
3030
{
3131
protected $propertyAccessor;
3232

33-
private $isDiscriminatorAttributeCache = array();
33+
private $discriminatorCache;
3434

3535
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
3636
{
@@ -41,6 +41,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4141
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver);
4242

4343
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
44+
$this->discriminatorCache = new \SplObjectStorage();
4445
}
4546

4647
/**
@@ -112,20 +113,18 @@ protected function extractAttributes($object, $format = null, array $context = a
112113
*/
113114
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
114115
{
115-
$cacheKey = \get_class($object).'::'.$attribute;
116-
117-
if (!isset($this->isDiscriminatorAttributeCache[$cacheKey])) {
118-
$this->isDiscriminatorAttributeCache[$cacheKey] = false;
116+
if (!$this->discriminatorCache->offsetExists($object)) {
117+
$this->discriminatorCache->offsetSet($object, null);
119118
if (null !== $this->classDiscriminatorResolver) {
120119
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
121120

122121
if (null !== $mapping && $attribute == $mapping->getTypeProperty()) {
123-
$this->isDiscriminatorAttributeCache[$cacheKey] = true;
122+
$this->discriminatorCache->offsetSet($object, $attribute);
124123
}
125124
}
126125
}
127126

128-
return $this->isDiscriminatorAttributeCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
127+
return $this->discriminatorCache[$object] === $attribute ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
129128
}
130129

131130
/**

0 commit comments

Comments
 (0)
0