From ccdf894324d7b40153953208f769299d923e91b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 10 Sep 2018 21:36:40 +0200 Subject: [PATCH 1/4] [Serializer] Cache the normalizer to use when possible --- components/serializer.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index 3bf6cda7f33..47631dbc3fb 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1295,6 +1295,33 @@ can use this simpler configuration: +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 of the value of the object to serialize, of the format to +use, and of the context. Consequently, **it is not cached** by default. + +Because they are called recursively, these methods are usually a huge performance bottleneck. Moreover, most +normalizers (and denormalizers) always return the same result when the object's type, and the format are +the same. + +To hint the serializer that it's safe to cache the result of these methods (and then dramatically improve the +overall performance), such normalizers (and denormalizers) should implement the +:class:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface` and return ``true`` when +:method:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface::hasCacheableSupportsMethod` +is called. + +.. note:: + + Built-in :ref:`normalizers and denormalizers `, as well the ones + included in `API Platform`_ natively implement this interface. + Learn more ---------- @@ -1304,6 +1331,11 @@ Learn more /serializer +.. seealso:: + + Normalizers for the Symfony Serializer Component supporting popular web API formats + (JSON-LD, GraphQL, HAL and JSONAPI) are available as part of the `API Platform`_ project. + .. seealso:: A popular alternative to the Symfony Serializer Component is the third-party @@ -1320,3 +1352,4 @@ Learn more .. _CSV: https://tools.ietf.org/html/rfc4180 .. _`RFC 7807`: https://tools.ietf.org/html/rfc7807 .. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object +.. _`API Platform`: https://api-platform.com From 8e09eeb9b5eceefcc7cac5ab41412f8d4d6c4951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 11 Sep 2018 00:32:16 +0200 Subject: [PATCH 2/4] RST --- components/serializer.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 47631dbc3fb..9234cb7237c 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1313,8 +1313,8 @@ the same. To hint the serializer that it's safe to cache the result of these methods (and then dramatically improve the overall performance), such normalizers (and denormalizers) should implement the -:class:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface` and return ``true`` when -:method:`Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface::hasCacheableSupportsMethod` +:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface` and return ``true`` when +:method:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface::hasCacheableSupportsMethod` is called. .. note:: From 8d6ca70dcffe468414ac34929198dcf9bf7a8a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 11 Sep 2018 00:38:18 +0200 Subject: [PATCH 3/4] RST --- components/serializer.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 9234cb7237c..6183457cd67 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1300,8 +1300,8 @@ 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`) +: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 of the value of the object to serialize, of the format to From 3e2e30f1e991cadacdab62e90441c55c79584c8a Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 12 Sep 2018 08:55:41 +0200 Subject: [PATCH 4/4] Minor reword --- components/serializer.rst | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/components/serializer.rst b/components/serializer.rst index 6183457cd67..56cd676984f 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -1304,23 +1304,22 @@ the :class:`Symfony\\Component\\Serializer\\Serializer` class will call the (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 of the value of the object to serialize, of the format to -use, and of the context. Consequently, **it is not cached** by default. - -Because they are called recursively, these methods are usually a huge performance bottleneck. Moreover, most -normalizers (and denormalizers) always return the same result when the object's type, and the format are -the same. - -To hint the serializer that it's safe to cache the result of these methods (and then dramatically improve the -overall performance), such normalizers (and denormalizers) should implement the -:class:`Symfony\\Component\\Serializer\\Normalizer\\CacheableSupportsMethodInterface` and return ``true`` when +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:: - - Built-in :ref:`normalizers and denormalizers `, as well the ones - included in `API Platform`_ natively implement this interface. + .. note:: + + All built-in :ref:`normalizers and denormalizers ` + as well the ones included in `API Platform`_ natively implement this interface. Learn more ----------