-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
Conversation
@@ -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'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; | ||
|
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep will do
There was a problem hiding this comment.
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.
Weird circleci issue, may you force push to trigger the build? |
all good @soyuka |
Thanks @bendavies! |
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.