Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #19507 [FrameworkBundle] Introduce a cache warmer for Seriali…
…zer based on PhpArrayAdapter (tgalopin) This PR was merged into the 3.2-dev branch. Discussion ---------- [FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Following the cache warmer for annotations (#18533) and for the validator (#19485), this PR introduces a cache warmer for the Serializer YAML and XML metadata configuration (mainly groups). Based on the PhpArrayAdapter, it uses the naming conventions (Resources/config/serialization) to find the files and compile them into a single PHP file stored in the cache directory. This file uses shared memory on PHP 7. The benefit of this PR are the same than the ones of the previous PR: - serialization metadata cache can be warmed up offline - on PHP 7, there is no need for user extension to get maximum performances (ie. if you use this PR and the other one, you probably won't need to enable APCu to have great performances) - on PHP 7 again, we are not sensitive to APCu memory fragmentation last but not least, global performance is slightly better (I get 30us per class gain in Blackfire) As previous work on the Serializer cache system introduced issues (see 96e418a), it would be interesting to pay careful attention to the backward compatibility during the review (ping @Ener-Getick). Commits ------- 810f469 [FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter
- Loading branch information
Showing
12 changed files
with
333 additions
and
28 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\Cache\Adapter\ProxyAdapter; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory; | ||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; | ||
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain; | ||
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; | ||
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; | ||
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; | ||
|
||
/** | ||
* Warms up XML and YAML serializer metadata. | ||
* | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
*/ | ||
class SerializerCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private $loaders; | ||
private $phpArrayFile; | ||
private $fallbackPool; | ||
|
||
/** | ||
* @param LoaderInterface[] $loaders The serializer metadata loaders. | ||
* @param string $phpArrayFile The PHP file where metadata are cached. | ||
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached. | ||
*/ | ||
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool) | ||
{ | ||
$this->loaders = $loaders; | ||
$this->phpArrayFile = $phpArrayFile; | ||
if (!$fallbackPool instanceof AdapterInterface) { | ||
$fallbackPool = new ProxyAdapter($fallbackPool); | ||
} | ||
$this->fallbackPool = $fallbackPool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function warmUp($cacheDir) | ||
{ | ||
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) { | ||
return; | ||
} | ||
|
||
$adapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool); | ||
$arrayPool = new ArrayAdapter(0, false); | ||
|
||
$metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayPool); | ||
|
||
foreach ($this->extractSupportedLoaders($this->loaders) as $loader) { | ||
foreach ($loader->getMappedClasses() as $mappedClass) { | ||
$metadataFactory->getMetadataFor($mappedClass); | ||
} | ||
} | ||
|
||
$values = $arrayPool->getValues(); | ||
$adapter->warmUp($values); | ||
|
||
foreach ($values as $k => $v) { | ||
$item = $this->fallbackPool->getItem($k); | ||
$this->fallbackPool->saveDeferred($item->set($v)); | ||
} | ||
$this->fallbackPool->commit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isOptional() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param LoaderInterface[] $loaders | ||
* | ||
* @return XmlFileLoader[]|YamlFileLoader[] | ||
*/ | ||
private function extractSupportedLoaders(array $loaders) | ||
{ | ||
$supportedLoaders = array(); | ||
|
||
foreach ($loaders as $loader) { | ||
if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) { | ||
$supportedLoaders[] = $loader; | ||
} elseif ($loader instanceof LoaderChain) { | ||
$supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getDelegatedLoaders())); | ||
} | ||
} | ||
|
||
return $supportedLoaders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer; | ||
|
||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory; | ||
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; | ||
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; | ||
|
||
class SerializerCacheWarmerTest extends TestCase | ||
{ | ||
public function testWarmUp() | ||
{ | ||
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) { | ||
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.'); | ||
} | ||
|
||
$loaders = array( | ||
new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'), | ||
new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'), | ||
); | ||
|
||
$file = sys_get_temp_dir().'/cache-serializer.php'; | ||
@unlink($file); | ||
|
||
$fallbackPool = new ArrayAdapter(); | ||
|
||
$warmer = new SerializerCacheWarmer($loaders, $file, $fallbackPool); | ||
$warmer->warmUp(dirname($file)); | ||
|
||
$this->assertFileExists($file); | ||
|
||
$values = require $file; | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(2, $values); | ||
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person', $values); | ||
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author', $values); | ||
|
||
$values = $fallbackPool->getValues(); | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(2, $values); | ||
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person', $values); | ||
$this->assertArrayHasKey('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author', $values); | ||
} | ||
|
||
public function testWarmUpWithoutLoader() | ||
{ | ||
if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) { | ||
$this->markTestSkipped('The Serializer default cache warmer has been introduced in the Serializer Component version 3.2.'); | ||
} | ||
|
||
$file = sys_get_temp_dir().'/cache-serializer-without-loader.php'; | ||
@unlink($file); | ||
|
||
$fallbackPool = new ArrayAdapter(); | ||
|
||
$warmer = new SerializerCacheWarmer(array(), $file, $fallbackPool); | ||
$warmer->warmUp(dirname($file)); | ||
|
||
$this->assertFileExists($file); | ||
|
||
$values = require $file; | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(0, $values); | ||
|
||
$values = $fallbackPool->getValues(); | ||
|
||
$this->assertInternalType('array', $values); | ||
$this->assertCount(0, $values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Serialization/Author.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization; | ||
|
||
class Author | ||
{ | ||
public $gender; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Serialization/Person.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization; | ||
|
||
class Person | ||
{ | ||
public $gender; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Serialization/Resources/author.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization\Author: | ||
attributes: | ||
gender: | ||
groups: ['group1', 'group2'] |
13 changes: 13 additions & 0 deletions
13
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Serialization/Resources/person.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" ?> | ||
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping | ||
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" | ||
> | ||
<class name="Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serialization\Person"> | ||
<attribute name="gender"> | ||
<group>group1</group> | ||
<group>group2</group> | ||
</attribute> | ||
</class> | ||
</serializer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.