8000 [Serializer] Cache the normalizer to use when possible · symfony/symfony@c1e850f · GitHub
[go: up one dir, main page]

Skip to content

Commit c1e850f

Browse files
dunglasnicolas-grekas
authored andcommitted
[Serializer] Cache the normalizer to use when possible
1 parent e035f45 commit c1e850f

9 files changed

+55
-8
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Kévin Dunglas <dunglas@gmail.com>
3232
*/
33-
abstract class AbstractObjectNormalizer extends AbstractNormalizer
33+
abstract class AbstractObjectNormalizer extends AbstractNormalizer implements NormalizerWithCacheableSupportResultInterface
3434
{
3535
const ENABLE_MAX_DEPTH = 'enable_max_depth';
3636
const DEPTH_KEY_PATTERN = 'depth_%s::%s';

src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2323
* @author Kévin Dunglas <dunglas@gmail.com>
2424
*/
25-
class ConstraintViolationListNormalizer implements NormalizerInterface
25+
class ConstraintViolationListNormalizer implements NormalizerInterface, NormalizerWithCacheableSupportResultInterface
2626
{
2727
/**
2828
* {@inheritdoc}

src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author Jordi Boggiano <j.boggiano@seld.be>
1919
*/
20-
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
20+
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerWithCacheableSupportResultInterface
2121
{
2222
use ObjectToPopulateTrait;
2323
use SerializerAwareTrait;

src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php

Lines changed: 1 addition & 1 deletion
private static $supportedTypes = array(
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Kévin Dunglas <dunglas@gmail.com>
2525
*/
26-
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
26+
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
2727
{
2828
2929
\SplFileInfo::class => true,

src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Jérôme Parmentier <jerome@prmntr.me>
2222
*/
23-
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface
23+
class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
2424
{
2525
const FORMAT_KEY = 'dateinterval_format';
2626

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Kévin Dunglas <dunglas@gmail.com>
2222
*/
23-
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
23+
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerWithCacheableSupportResultInterface
2424
{
2525
const FORMAT_KEY = 'datetime_format';
2626
const TIMEZONE_KEY = 'datetime_timezone';

src/Symfony/Component/Serializer/Normalizer/JsonSerializableNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Fred Cox <mcfedr@gmail.com>
2121
*/
22-
class JsonSerializableNormalizer extends AbstractNormalizer
22+
class JsonSerializableNormalizer extends AbstractNormalizer implements NormalizerWithCacheableSupportResultInterface
2323
{
2424
/**
2525
* {@inheritdoc}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
/**
15+
* "supportsNormalization()" methods of normalizers implementing this interface have a cacheable return.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface NormalizerWithCacheableSupportResultInterface
20+
{
21+
}

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2727
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2828
use Symfony\Component\Serializer\Exception\LogicException;
29+
use Symfony\Component\Serializer\Normalizer\NormalizerWithCacheableSupportResultInterface;
2930

3031
/**
3132
* Serializer serializes and deserializes data.
@@ -54,6 +55,8 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
5455
*/
5556
protected $decoder;
5657

58+
private $normalizerCache = array();
59+
5760
/**
5861
* @var array
5962
*/
@@ -202,11 +205,34 @@ public function supportsDenormalization($data, $type, $format = null, array $con
202205
*/
203206
private function getNormalizer($data, $format, array $context)
204207
{
208+
$type = \is_object($data) ? 'c-'.\get_class($data) : \gettype($data);
209+
if (
210+
isset($this->normalizerCache[$type][$format]) ||
211+
(isset($this->normalizerCache[$type]) && \array_key_exists($format, $this->normalizerCache[$type]))
212+
) {
213+
return $this->normalizerCache[$type][$format];
214+
}
215+
216+
$cacheable = true;
205217
foreach ($this->normalizers as $normalizer) {
206-
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) {
218+
if (!$normalizer instanceof NormalizerInterface) {
219+
continue;
220+
}
221+
222+
$cacheable = $cacheable && $normalizer instanceof NormalizerWithCacheableSupportResultInterface;
223+
if ($normalizer->supportsNormalization($data, $format, $context)) {
224+
if ($cacheable) {
225+
$this->normalizerCache[$type][$format] = $normalizer;
226+
}
227+
207228
return $normalizer;
208229
}
209230
}
231+
232+
if ($cacheable) {
233+
// allow to cache primitive types
234+
$this->normalizerCache[$type][$format] = null;
235+
}
210236
}
211237

212238
/**

0 commit comments

Comments
 (0)
0