Skip to content

[Serializer] Add a way to disable ClassDiscriminatorResolver #28537

Closed
@franek

Description

@franek

Symfony version(s) affected: 4.1.4

Description

I had a performance issue on migrating my project from Symfony3.4 to Symfony4.1.
I mainly use Serializer to serialize data (through ObjectNormalizer).
After doing several blackfire analyses, I have found that problem was due to ClassDiscrimintatorResolver (#24375) which is enable by default whereas I don't need it.

To fix the problem, I had to create a Custom ObjectNormalizer and set $this->classDiscriminatorResolver to null. Not very elegant but I had a huge boost with this modification and SF4 serializer (now 90ms, 120ms without this modification) is now faster than SF3.4 (97ms).

class ObjectNormalizer extends CoreObjectNormalizer implements CacheableSupportsMethodInterface
{

    public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null,
        PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null)
    {
        parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor, $classDiscriminatorResolver);

        /**
         * Disable classDiscriminatorResolver for ObjectNormalizer. It is initialized into 
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php#L59
         */
        $this->classDiscriminatorResolver = null;
    }

Possible Solution

A possible solution should be to disable classDiscriminatorResolver if framework.serialiazer.discriminator_class_mapping is not set.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions