8000 bug #28889 [Serializer] Reduce class discriminator overhead (fbouriga… · symfony/symfony@f1795c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1795c0

Browse files
bug #28889 [Serializer] Reduce class discriminator overhead (fbourigault)
This PR was merged into the 4.1 branch. Discussion ---------- [Serializer] Reduce class discriminator overhead | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28537 | License | MIT | Doc PR | N/A This try to fix the overhead added by class discriminator feature. Here is a 4.1 vs this PR comparison: https://blackfire.io/profiles/compare/20ead249-0e98-430f-9b8d-906f464b1854/graph And a 3.4 vs this PR comparison: https://blackfire.io/profiles/compare/7e402dde-4a54-4053-a12e-d3d6891afc02/graph Commits ------- 326c267 [Serializer] Reduce class discriminator overhead
2 parents 5f253bf + 326c267 commit f1795c0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

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

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

33+
private $discriminatorCache = array();
34+
3335
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
3436
{
3537
if (!\class_exists(PropertyAccess::class)) {
@@ -110,15 +112,16 @@ protected function extractAttributes($object, $format = null, array $context = a
110112
*/
111113
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
112114
{
113-
if (null !== $this->classDiscriminatorResolver) {
114-
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
115-
116-
if (null !== $mapping && $attribute == $mapping->getTypeProperty()) {
117-
return $this->classDiscriminatorResolver->getTypeForMappedObject($object);
115+
$cacheKey = \get_class($object);
116+
if (!array_key_exists($cacheKey, $this->discriminatorCache)) {
117+
$this->discriminatorCache[$cacheKey] = null;
118+
if (null !== $this->classDiscriminatorResolver) {
119+
$mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object);
120+
$this->discriminatorCache[$cacheKey] = null === $mapping ? null : $mapping->getTypeProperty();
118121
}
119122
}
120123

121-
return $this->propertyAccessor->getValue($object, $attribute);
124+
return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) : $this->propertyAccessor->getValue($object, $attribute);
122125
}
123126

124127
/**

0 commit comments

Comments
 (0)
0