Skip to content
Permalink
Browse files

minor #35079 [Serializer] add array cache in front of serializer cach…

…e (bastnic)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Serializer] add array cache in front of serializer cache

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | performance
| New feature?  | ...
| Deprecations? | no
| Tickets       | #35041
| License       | MIT
| Doc PR        |

This is another implementation of #35041, after #35046.

The DI solution is cleaner, but 10ms slower on my scenario (13 vs 3) and this data is not supposed to change at all (even in the original method there is a local cache).

Diff from master (without warmup):

![image](https://user-images.githubusercontent.com/84887/71313592-2cb5be00-243b-11ea-8983-b30abb7d0698.png)

Diff form master (with warmup):

![image](https://user-images.githubusercontent.com/84887/71313633-8e762800-243b-11ea-8044-003c810d3c0b.png)

Diff from #35046:

![image](https://user-images.githubusercontent.com/84887/71313668-312ea680-243c-11ea-9768-8531f4f33f3c.png)

Ping @nicolas-grekas

Commits
-------

6ee306b [Cache] add array cache in front of serializer cache
  • Loading branch information
nicolas-grekas committed Dec 26, 2019
2 parents 4e5b153 + 6ee306b commit cdaebf6fb852d8906db824dc60a2b44008a18fac
Showing with 9 additions and 2 deletions.
  1. +9 −2 src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php
@@ -32,6 +32,8 @@ class CacheClassMetadataFactory implements ClassMetadataFactoryInterface
*/
private $cacheItemPool;

private $loadedClasses = [];

public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemPoolInterface $cacheItemPool)
{
$this->decorated = $decorated;
@@ -44,18 +46,23 @@ public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemP
public function getMetadataFor($value)
{
$class = $this->getClass($value);

if (isset($this->loadedClasses[$class])) {
return $this->loadedClasses[$class];
}

// Key cannot contain backslashes according to PSR-6
$key = strtr($class, '\\', '_');

$item = $this->cacheItemPool->getItem($key);
if ($item->isHit()) {
return $item->get();
return $this->loadedClasses[$class] = $item->get();
}

$metadata = $this->decorated->getMetadataFor($value);
$this->cacheItemPool->save($item->set($metadata));

return $metadata;
return $this->loadedClasses[$class] = $metadata;
}

/**

0 comments on commit cdaebf6

Please sign in to comment.
You can’t perform that action at this time.