Skip to content

Commit

Permalink
minor symfony#14328 [Serializer] add note on Custom Normalizer page a…
Browse files Browse the repository at this point in the history
…bout cacheable performance (maxhelias)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] add note on Custom Normalizer page about cacheable performance

Fix symfony#12061

Commits
-------

6d51c6c [Serializer] move note on Custom Normalizer page about cacheable performance
  • Loading branch information
javiereguiluz committed Jan 22, 2021
2 parents fbdd52f + 6d51c6c commit b556f4f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
26 changes: 0 additions & 26 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1524,32 +1524,6 @@ Once configured, the serializer uses the mapping to pick the correct class::
$repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
// instanceof GitHubCodeRepository

Performance
-----------

To figure which normalizer (or denormalizer) must be used to handle an object,
the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the
:method:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface::supportsNormalization`
(or :method:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface::supportsDenormalization`)
of all registered normalizers (or denormalizers) in a loop.

The result of these methods can vary depending on the object to serialize, the
format and the context. That's why the result **is not cached** by default and
can result in a significant performance bottleneck.

However, most normalizers (and denormalizers) always return the same result when
the object's type and the format are the same, so the result can be cached. To
do so, make those normalizers (and denormalizers) implement the
:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface`
and return ``true`` when
:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
is called.

.. note::

All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
as well the ones included in `API Platform`_ natively implement this interface.

Learn more
----------

Expand Down
29 changes: 29 additions & 0 deletions serializer/custom_normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,32 @@ Before using this normalizer in a Symfony application it must be registered as
a service and :doc:`tagged </service_container/tags>` with ``serializer.normalizer``.
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
this is done automatically!

Performance
-----------

To figure which normalizer (or denormalizer) must be used to handle an object,
the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the
:method:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface::supportsNormalization`
(or :method:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface::supportsDenormalization`)
of all registered normalizers (or denormalizers) in a loop.

The result of these methods can vary depending on the object to serialize, the
format and the context. That's why the result **is not cached** by default and
can result in a significant performance bottleneck.

However, most normalizers (and denormalizers) always return the same result when
the object's type and the format are the same, so the result can be cached. To
do so, make those normalizers (and denormalizers) implement the
:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface`
and return ``true`` when
:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
is called.

.. note::

All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
as well the ones included in `API Platform`_ natively implement this interface.

.. _`API Platform`: https://api-platform.com

0 comments on commit b556f4f

Please sign in to comment.