Skip to content

[Serializer] Document CompiledClassMetadataFactory #10706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fbourigault opened this issue Nov 26, 2018 · 6 comments
Closed

[Serializer] Document CompiledClassMetadataFactory #10706

fbourigault opened this issue Nov 26, 2018 · 6 comments

Comments

@fbourigault
Copy link
Contributor

fbourigault commented Nov 26, 2018

I don't know how to document this feature. Should it be documented at the component level or should I wait the framework integration?

PR: symfony/symfony#29117

@xabbuh xabbuh added Serializer Waiting Code Merge Docs for features pending to be merged labels Nov 28, 2018
fabpot added a commit to symfony/symfony that referenced this issue Aug 11, 2020
…ault)

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

Discussion
----------

[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](https://blog.blackfire.io/php-7-performance-improvements-immutable-arrays.html).

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](https://blackfire.io/profiles/compare/d937a9cc-eebf-47eb-be90-c8e65cdf12b3/graph)
- [SymfonyObjectNormalizerBenchmark, 3 iterations with complexity of 60](https://blackfire.io/profiles/compare/d490542c-9a79-48a0-b7bc-1ed3ca6a9148/graph)

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`
```php
$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`
```php
$classMetadataFactory = new CompiledClassMetadataFactory(
    'dumped.php',
    new CacheClassMetadataFactory(
        new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
        new ApcuAdapter('SymfonyMetadata')
    )
);
```
# To do

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

Commits
-------

63cbf0a [Serializer] Add CompiledClassMetadataFactory
@xabbuh xabbuh removed the Waiting Code Merge Docs for features pending to be merged label Oct 16, 2020
@carsonbot
Copy link
Collaborator

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link
Collaborator

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@javiereguiluz
Copy link
Member

Don't close it

@carsonbot carsonbot removed the Stalled label Oct 31, 2021
@carsonbot
Copy link
Collaborator

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link
Collaborator

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@carsonbot
Copy link
Collaborator

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants