Skip to content
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

RelatedCollectionLinkNormalizer performance improved #6536

Conversation

pmattmann
Copy link
Member

@pmattmann pmattmann commented Dec 15, 2024

Function getRelatedCollectionHref is called several times for the same relation

  • try-catch removed
  • cache per relation if no URI can be calculated for it anyway (instead of trying again and again)

Old:

  • all activities
    image
  • activities of one camp
    image

New:

  • all activities
    image
  • activities of one camp
    image

New2:

  • all activities
    image
  • activities of one camp
    image

#6461

@pmattmann pmattmann requested a review from a team December 15, 2024 18:59
Copy link
Member

@carlobeltrame carlobeltrame left a comment

Choose a reason for hiding this comment

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

Is the array return really faster than the exception? If so, then facepalm PHP...
But did you test whether the performance is also improved similarly with only the caching part?

@pmattmann
Copy link
Member Author

Is the array return really faster than the exception? If so, then facepalm PHP... But did you test whether the performance is also improved similarly with only the caching part?

As far as I know, it is faster in most languages if you don't need exceptions for the control flow.
You can find a few measurements for PHP here:
https://php.watch/articles/php-exception-performance#results

@pmattmann pmattmann requested review from usu and BacLuc December 19, 2024 06:04
Copy link
Contributor

@BacLuc BacLuc left a comment

Choose a reason for hiding this comment

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

When starting to write performance optimized code, there is always the tradeof between readability (here the inconsistency with the php way of writing code and that we use exceptions in the other places) and the gained performance.

A way for this would be:

  1. Benchmark this endpoint (measurement done)
  2. Show, that this is the crucial part were the performance can be gained, e.g. with a profiler. (Maybe there is a simpler place where we can gain more)
  3. optimize the performance here
  4. Benchmark again and show that it improved
  5. Write a test that enforces this maybe uncommon style
  6. Look how it behaves in production.

But i would say this is ok here and we look how the request time behaves when we have it running with real users against the real database.

But maybe there is more to gain with other configurations of the frankenphp container, as can be seen here:

@pmattmann
Copy link
Member Author

When starting to write performance optimized code, there is always the tradeof between readability (here the inconsistency with the php way of writing code and that we use exceptions in the other places) and the gained performance.

A way for this would be:

  1. Benchmark this endpoint (measurement done)
  2. Show, that this is the crucial part were the performance can be gained, e.g. with a profiler. (Maybe there is a simpler place where we can gain more)
  3. optimize the performance here
  4. Benchmark again and show that it improved
  5. Write a test that enforces this maybe uncommon style
  6. Look how it behaves in production.

But i would say this is ok here and we look how the request time behaves when we have it running with real users against the real database.

But maybe there is more to gain with other configurations of the frankenphp container, as can be seen here:

I completely agree with the procedure you described!
That's exactly what I did.

Here is the initial measurement (XDebug Profiler) of localhost:3000/api/activities.jsonhal
image

RelatedCollectionLinkNormalizer::getRelatedCollectionHref() is clearly a time consuming operation.

Here are the results after implementing the cache:

image

image

@pmattmann
Copy link
Member Author

@BacLuc
If you agree with my last adjustments, please merge.
thx

@pmattmann pmattmann added this pull request to the merge queue Dec 25, 2024
Merged via the queue into ecamp:devel with commit 2758830 Dec 25, 2024
35 checks passed
@pmattmann pmattmann deleted the feature/related-collection-link-normalizer-performance branch December 25, 2024 15:26
@BacLuc BacLuc mentioned this pull request Jan 2, 2025
@BacLuc BacLuc mentioned this pull request Jan 14, 2025
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