Skip to content

Commit a1dc63f

Browse files
committed
minor #10316 [Serializer] Cache the normalizer to use when possible (dunglas, javiereguiluz)
This PR was merged into the 4.1 branch. Discussion ---------- [Serializer] Cache the normalizer to use when possible symfony/symfony#27049 Commits ------- 3e2e30f Minor reword 8d6ca70 RST 8e09eeb RST ccdf894 [Serializer] Cache the normalizer to use when possible
2 parents 37aef52 + 3e2e30f commit a1dc63f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

components/serializer.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,32 @@ 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 on the object to serialize, the
1308+
format and the context. That's why the result **is not cached** by default and
1309+
can result in a significant performance bottleneck.
1310+
1311+
However, most normalizers (and denormalizers) always return the same result when
1312+
the object's type and the format are the same, so the result can be cached. To
1313+
do so, make those normalizers (and denormalizers) implement the
1314+
:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface`
1315+
and return ``true`` when
1316+
:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod`
1317+
is called.
1318+
1319+
.. note::
1320+
1321+
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
1322+
as well the ones included in `API Platform`_ natively implement this interface.
1323+
12981324
Learn more
12991325
----------
13001326

@@ -1304,6 +1330,11 @@ Learn more
13041330

13051331
/serializer
13061332

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

13091340
A popular alternative to the Symfony Serializer Component is the third-party
@@ -1320,3 +1351,4 @@ Learn more
13201351
.. _CSV: https://tools.ietf.org/html/rfc4180
13211352
.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807
13221353
.. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object
1354+
.. _`API Platform`: https://api-platform.com

0 commit comments

Comments
 (0)