Skip to content

Commit 3e4c1dd

Browse files
committed
[Serializer] add note on Custom Normalizer page about cacheable performance
1 parent 8a7d32e commit 3e4c1dd

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

components/serializer.rst

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,31 +1499,7 @@ Once configured, the serializer uses the mapping to pick the correct class::
14991499
$repository = $serializer->deserialize($serialized, CodeRepository::class, 'json');
15001500
// instanceof GitHubCodeRepository
15011501

1502-
Performance
1503-
-----------
1504-
1505-
To figure which normalizer (or denormalizer) must be used to handle an object,
1506-
the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the
1507-
:method:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface::supportsNormalization`
1508-
(or :method:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface::supportsDenormalization`)
1509-
of all registered normalizers (or denormalizers) in a loop.
1510-
1511-
The result of these methods can vary depending on the object to serialize, the
1512-
format and the context. That's why the result **is not cached** by default and
1513-
can result in a significant performance bottleneck.
1514-
1515-
However, most normalizers (and denormalizers) always return the same result when
1516-
the object's type and the format are the same, so the result can be cached. To
1517-
do so, make those normalizers (and denormalizers) implement the
1518-
:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface`
1519-
and return ``true`` when
1520-
:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
1521-
is called.
1522-
1523-
.. note::
1524-
1525-
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
1526-
as well the ones included in `API Platform`_ natively implement this interface.
1502+
.. include:: /serializer/_performance_tip.rst.inc
15271503

15281504
Learn more
15291505
----------

serializer/_performance_tip.rst.inc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Performance
2+
-----------
3+
4+
To figure which normalizer (or denormalizer) must be used to handle an object,
5+
the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the
6+
:method:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface::supportsNormalization`
7+
(or :method:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface::supportsDenormalization`)
8+
of all registered normalizers (or denormalizers) in a loop.
9+
10+
The result of these methods can vary depending on the object to serialize, the
11+
format and the context. That's why the result **is not cached** by default and
12+
can result in a significant performance bottleneck.
13+
14+
However, most normalizers (and denormalizers) always return the same result when
15+
the object's type and the format are the same, so the result can be cached. To
16+
do so, make those normalizers (and denormalizers) implement the
17+
:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface`
18+
and return ``true`` when
19+
:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
20+
is called.
21+
22+
.. note::
23+
24+
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
25+
as well the ones included in `API Platform`_ natively implement this interface.
26+
27+
.. _`API Platform`: https://api-platform.com

serializer/custom_normalizer.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ Before using this normalizer in a Symfony application it must be registered as
6060
a service and :doc:`tagged </service_container/tags>` with ``serializer.normalizer``.
6161
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
6262
this is done automatically!
63+
64+
.. include:: /serializer/_performance_tip.rst.inc

0 commit comments

Comments
 (0)