Skip to content

Hal Item Normalizer: add a local cache for classMetadataFactory #2010

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

Merged
merged 1 commit into from
Jun 11, 2018
Merged

Hal Item Normalizer: add a local cache for classMetadataFactory #2010

merged 1 commit into from
Jun 11, 2018

Conversation

bendavies
Copy link
Contributor

@bendavies bendavies commented Jun 8, 2018

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

here: #1971 i accidentally created a performance issue.

I didn't spot this because the (prod) cached metadata factory does not have a local lookup:
https://github.com/symfony/symfony/blob/1b2bd8f41971f7903fd04d898801a1874a0427dc/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php#L46

while the (dev) uncached one does:
https://github.com/symfony/symfony/blob/1b2bd8f41971f7903fd04d898801a1874a0427dc/src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php#L44-L48

This means that in prod mode, many many cache lookups are done, which is pretty slow.
This PR adds a local cache to fix that excessive lookups.

@@ -169,7 +170,11 @@ private function getComponents($object, string $format = null, array $context)
private function populateRelation(array $data, $object, string $format = null, array $context, array $components, string $type): array
{
$class = \get_class($object);
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($object)->getAttributesMetadata() : null;
$cacheKey = $class.':attribute-metadata';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you declare a const CACHE_SUFFIX with this constatnt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache suffix isn't needed. removed it.


$attributesMetadata = \array_key_exists($cacheKey, $this->attributesMetadataCache) ?
$this->attributesMetadataCache[$cacheKey] :
$this->attributesMetadataCache[$cacheKey] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($object)->getAttributesMetadata() : null;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not $attributesMetadata = $this->attributesMetadataCache[$cacheKey] ?? this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($object)->getAttributesMetadata() : null;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't do this, null coalesce doesn't work with null, so the array_key_exists is needed.

@soyuka
Copy link
Member

soyuka commented Jun 11, 2018

Weird circleci issue, may you force push to trigger the build?

@bendavies
Copy link
Contributor Author

all good @soyuka

@dunglas dunglas merged commit a1b4924 into api-platform:2.2 Jun 11, 2018
@dunglas
Copy link
Member

dunglas commented Jun 11, 2018

Thanks @bendavies!

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

Successfully merging this pull request may close these issues.

3 participants