Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccdf894

Browse files
committed
[Serializer] Cache the normalizer to use when possible
1 parent 87737b1 commit ccdf894

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

components/serializer.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,33 @@ can use this simpler configuration:
12951295
</class>
12961296
</serializer>
12971297
1298+
Performance
1299+
-----------
1300+
1301+
To figure which normalizer (or denormalizer) must be used to handle an object,
1302+
the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the
1303+
:method:`Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization`
1304+
(or :method:`Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization`)
1305+
of all registered normalizers (or denormalizers) in a loop.
1306+
1307+
The result of these methods can vary depending of the value of the object to serialize, of the format to
1308+
use, and of the context. Consequently, **it is not cached** by default.
1309+
1310+
Because they are called recursively, these methods are usually a huge performance bottleneck. Moreover, most
1311+
normalizers (and denormalizers) always return the same result when the object's type, and the format are
1312+
the same.
1313+
1314+
To hint the serializer that it's safe to cache the result of these methods (and then dramatically improve the
1315+
overall performance), such normalizers (and denormalizers) should implement the
1316+
:class:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface` and return ``true`` when
1317+
:method:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
1318+
is called.
1319+
1320+
.. note::
1321+
1322+
Built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`, as well the ones
1323+
included in `API Platform`_ natively implement this interface.
1324+
12981325
Learn more
12991326
----------
13001327

@@ -1304,6 +1331,11 @@ Learn more
13041331

13051332
/serializer
13061333

1334+
.. seealso::
1335+
1336+
Normalizers for the Symfony Serializer Component supporting popular web API formats
1337+
(JSON-LD, GraphQL, HAL and JSONAPI) are available as part of the `API Platform`_ project.
1338+
13071339
.. seealso::
13081340

13091341
A popular alternative to the Symfony Serializer Component is the third-party
@@ -1320,3 +1352,4 @@ Learn more
13201352
.. _CSV: https://tools.ietf.org/html/rfc4180
13211353
.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807
13221354
.. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object
1355+
.. _`API Platform`: https://api-platform.com

0 commit comments

Comments
 (0)