Skip to content

fix normalizer cache key generation #1894

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
Apr 26, 2018
Merged

fix normalizer cache key generation #1894

merged 1 commit into from
Apr 26, 2018

Conversation

bendavies
Copy link
Contributor

@bendavies bendavies commented Apr 26, 2018

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

Cache keys were being regenerated even if they were already present. this disabled caching when serializing resources which have already been serialized.

https://blackfire.io/profiles/compare/e4646b66-8700-4ac9-b36e-9de700dc5b03/graph

@@ -213,7 +216,7 @@ private function getRelationIri($rel): string
*
* @return bool|string
*/
private function getHalCacheKey(string $format = null, array $context)
private function getCacheKey(string $format = null, array $context)
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 revert those changes? Reusing the name of a method (even private) defined in a parent class is misleading and trigger warning in some analysis tools (PHP EA inspection IIRC)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@codecov
Copy link

codecov bot commented Apr 26, 2018

Codecov Report

Merging #1894 into 2.2 will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##                2.2    #1894      +/-   ##
============================================
+ Coverage     96.47%   96.47%   +<.01%     
- Complexity     2600     2602       +2     
============================================
  Files           191      191              
  Lines          6526     6534       +8     
============================================
+ Hits           6296     6304       +8     
  Misses          230      230
Impacted Files Coverage Δ Complexity Δ
src/JsonApi/Serializer/ItemNormalizer.php 93.85% <100%> (+0.28%) 53 <0> (+2) ⬆️
src/Hal/Serializer/ItemNormalizer.php 98.66% <100%> (+0.05%) 31 <0> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ca162c1...d03461e. Read the comment docs.

if (false !== $context['cache_key'] && isset($this->componentsCache[$context['cache_key']])) {
return $this->componentsCache[$context['cache_key']];
$class = \get_class($object);
$cacheKey = $class.'-'.$context['cache_key'];
Copy link
Contributor

Choose a reason for hiding this comment

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

$class seems used only one time

@@ -221,8 +223,11 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
*/
private function getComponents($object, string $format = null, array $context)
{
if (isset($this->componentsCache[$context['cache_key']])) {
return $this->componentsCache[$context['cache_key']];
$class = \get_class($object);
Copy link
Contributor

Choose a reason for hiding this comment

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

$class seems used only one time

@@ -99,8 +102,10 @@ protected function getAttributes($object, $format = null, array $context)
*/
private function getComponents($object, string $format = null, array $context)
{
if (false !== $context['cache_key'] && isset($this->componentsCache[$context['cache_key']])) {
return $this->componentsCache[$context['cache_key']];
$cacheKey = \get_class($object).'-'.$context['cache_key'];
Copy link
Member

Choose a reason for hiding this comment

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

We know the object class in the normalize method right? Could we pass the information to this function to avoid calling get_class? Might be a micro-optimization though. (same below)

Copy link
Member

Choose a reason for hiding this comment

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

\get_class is transformed in opcode, it's almost costless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For info I've copied the method used in core symfony, which is to add the class here

@bendavies
Copy link
Contributor Author

we good here?

@dunglas dunglas merged commit 0b35725 into api-platform:2.2 Apr 26, 2018
@dunglas
Copy link
Member

dunglas commented Apr 26, 2018

Thanks @bendavies. All your work regarding performance is very appreciated!

teohhanhui pushed a commit to teohhanhui/api-platform-core that referenced this pull request May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants