From 6d51c6c80038ef53d5948837681fd2b46a25c595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Sun, 4 Oct 2020 04:06:32 +0200 Subject: [PATCH] [Serializer] move note on Custom Normalizer page about cacheable performance --- components/serializer.rst | 26 -------------------------- serializer/custom_normalizer.rst | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 345642c9770..515773d18c3 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1499,32 +1499,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 ` - as well the ones included in `API Platform`_ natively implement this interface. - Learn more ---------- diff --git a/serializer/custom_normalizer.rst b/serializer/custom_normalizer.rst index 9f2e50fdcf1..2c7c7eedf88 100644 --- a/serializer/custom_normalizer.rst +++ b/serializer/custom_normalizer.rst @@ -60,3 +60,32 @@ Before using this normalizer in a Symfony application it must be registered as a service and :doc:`tagged ` with ``serializer.normalizer``. If you're using the :ref:`default services.yaml configuration `, 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 ` + as well the ones included in `API Platform`_ natively implement this interface. + +.. _`API Platform`: https://api-platform.com +