Skip to content
Go to file

Latest commit


This PR was squashed before being merged into the 5.2-dev branch.


[Serializer] Add CompiledClassMetadataFactory

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ?
| License       | MIT
| Doc PR        | todo (issue: symfony/symfony-docs#10706)

This introduce a dumped `ClassMetadataFactoryInterface` implementation to speed up the serializer by leveraging [PHP7 immutable array](

Like for #28865, if the user have the opcache extension enabled, the compilation time will be skipped. The user will also have a performance boost when not using opcache as we are no longer fetching `ClassMetadata` from the PSR-6 cache.

This allow to speed up the normalization (without opcache) by 9-12% depending on how many objects are involved in the graph:

- [SymfonyObjectNormalizerBenchmark, 100 iterations with complexity of 1](
- [SymfonyObjectNormalizerBenchmark, 3 iterations with complexity of 60](

On the `FrameworkBundle` side, I suggest to add a `CacheWarmer` to dump the metadata array from configured class list. The list could have a _good_ default which will load the classes found in `src/Entity`.

## Dumping the `ClassMetadata`
$classMetadatas = [];

foreach([Category::class, Comment::class, Forum::class, Thread::class, User::class] as $class) {
    $classMetadatas[] = $this->classMetadataFactory->getMetadataFor($class);

file_put_contents('dumped.php', $this->classMetadataFactoryCompiler->compile($classMetadatas));

## Using the dumped `ClassMetadata`
$classMetadataFactory = new CompiledClassMetadataFactory(
    new CacheClassMetadataFactory(
        new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
        new ApcuAdapter('SymfonyMetadata')
# To do

- [x] Tests.
- [x] Cache warmer.
- [x] Documentation.
- [x] Changelog entry.


63cbf0a [Serializer] Add CompiledClassMetadataFactory


Failed to load latest commit information.
Latest commit message
Commit time

Symfony is a PHP framework for web and console applications and a set of reusable PHP components. Symfony is used by thousands of web applications (including and and most of the popular PHP projects (including Drupal and Magento).





Symfony is an Open Source, community-driven project with thousands of contributors. Join them contributing code or contributing documentation.

Security Issues

If you discover a security vulnerability within Symfony, please follow our disclosure procedure.

About Us

Symfony development is sponsored by SensioLabs, led by the Symfony Core Team and supported by Symfony contributors.

You can’t perform that action at this time.