@@ -126,41 +126,32 @@ If you're not using ``autoconfigure``, you have to tag the service with
126
126
;
127
127
};
128
128
129
- Performance of Normalizers/Denormalizers
130
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
-
132
- To figure which normalizer (or denormalizer) must be used to handle an object,
133
- the :class: `Symfony\\ Component\\ Serializer\\ Serializer ` class will call the
134
- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface::supportsNormalization `
135
- (or :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface::supportsDenormalization `)
136
- of all registered normalizers (or denormalizers) in a loop.
137
-
138
- Additionally, both
139
- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface `
140
- and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface `
141
- contain the ``getSupportedTypes() `` method. This method allows normalizers or
142
- denormalizers to declare the type of objects they can handle, and whether they
143
- are cacheable. With this info, even if the ``supports*() `` call is not cacheable,
144
- the Serializer can skip a ton of method calls to ``supports*() `` improving
145
- performance substantially in some cases.
129
+ Improving Performance of Normalizers/Denormalizers
130
+ --------------------------------------------------
131
+
132
+ Both :class: Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface
133
+ and :class: Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface
134
+ define a ``getSupportedTypes() `` method to declare which types they support and
135
+ whether their ``supports*() `` result can be cached.
136
+
137
+ This **does not ** cache the actual normalization or denormalization result. It
138
+ only **caches the decision ** of whether a normalizer supports a given type, allowing
139
+ the Serializer to skip unnecessary ``supports*() `` calls and improve performance.
146
140
147
141
The ``getSupportedTypes() `` method should return an array where the keys
148
- represent the supported types, and the values indicate whether the result of
149
- the ``supports*() `` method call can be cached or not. The format of the
150
- returned array is as follows:
142
+ represent the supported types, and the values indicate whether the result of the
143
+ corresponding ``supports*() `` call can be cached. The array format is as follows:
151
144
152
145
#. The special key ``object `` can be used to indicate that the normalizer or
153
146
denormalizer supports any classes or interfaces.
154
147
#. The special key ``* `` can be used to indicate that the normalizer or
155
- denormalizer might support any types.
156
- #. The other keys in the array should correspond to specific types that the
157
- normalizer or denormalizer supports.
158
- #. The values associated with each type should be a boolean indicating if the
159
- result of the ``supports*() `` method call for that type can be cached or not.
160
- A value of ``true `` means that the result is cacheable, while ``false `` means
161
- that the result is not cacheable.
162
- #. A ``null `` value for a type means that the normalizer or denormalizer does
163
- not support that type.
148
+ denormalizer might support any type.
149
+ #. Other keys should correspond to specific types that the normalizer or
150
+ denormalizer supports.
151
+ #. The values should be booleans indicating whether the result of the
152
+ ``supports*() `` call for that type is cacheable. Use ``true `` if the result
153
+ can be cached, ``false `` if it cannot.
154
+ #. A ``null `` value means the normalizer or denormalizer does not support that type.
164
155
165
156
Here is an example of how to use the ``getSupportedTypes() `` method::
166
157
@@ -173,9 +164,9 @@ Here is an example of how to use the ``getSupportedTypes()`` method::
173
164
public function getSupportedTypes(?string $format): array
174
165
{
175
166
return [
176
- 'object' => null, // Doesn 't support any classes or interfaces
177
- '*' => false, // Supports any other types, but the result is not cacheable
178
- MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
167
+ 'object' => null, // doesn 't support any classes or interfaces
168
+ '*' => false, // supports any other types, but the decision is not cacheable
169
+ MyCustomClass::class => true, // supports MyCustomClass and decision is cacheable
179
170
];
180
171
}
181
172
}
0 commit comments