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

Add caching for resolve #3549

Merged
merged 7 commits into from
Jun 26, 2024
Merged

Add caching for resolve #3549

merged 7 commits into from
Jun 26, 2024

Conversation

patrick91
Copy link
Member

@patrick91 patrick91 commented Jun 24, 2024

Closes #3544

This PR adds cache to the resolve method to make sure we don't do the same work multiple times.

Copy link
Contributor

sourcery-ai bot commented Jun 24, 2024

Reviewer's Guide by Sourcery

This pull request addresses issue #3544 by replacing the @property decorator with @cached_property for the .type method in the strawberry/arguments.py file. This change aims to improve performance by caching the result of the .type method after its first computation.

File-Level Changes

Files Changes
strawberry/arguments.py Replaced @Property with @cached_property for the .type method to cache the result and improve performance.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @patrick91 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

strawberry/arguments.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jun 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.57%. Comparing base (095463d) to head (4e72915).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3549   +/-   ##
=======================================
  Coverage   96.57%   96.57%           
=======================================
  Files         524      524           
  Lines       33629    33630    +1     
  Branches     5578     5578           
=======================================
+ Hits        32478    32479    +1     
  Misses        915      915           
  Partials      236      236           

Copy link

codspeed-hq bot commented Jun 24, 2024

CodSpeed Performance Report

Merging #3549 will improve performances by ×7

Comparing fix/generic-input-perf (4e72915) with main (095463d)

Summary

⚡ 1 improvements
✅ 12 untouched benchmarks

Benchmarks breakdown

Benchmark main fix/generic-input-perf Change
test_execute_generic_input 3,094.8 ms 439.4 ms ×7

strawberry/arguments.py Outdated Show resolved Hide resolved
@patrick91
Copy link
Member Author

patrick91 commented Jun 24, 2024

reminder to make a pre-release and test on strawberry django & sqlalchemy (thanks @skilkis)

@erikwrede
Copy link
Member

erikwrede commented Jun 24, 2024

image

We might be able to add additional caching here in the GraphQL core converter

@patrick91
Copy link
Member Author

@erikwrede yeah, but I'd like to check if we are doing too much useless work first :D

@patrick91 patrick91 force-pushed the fix/generic-input-perf branch from 75db9ca to 1c3ccf9 Compare June 25, 2024 16:46
@patrick91 patrick91 force-pushed the fix/generic-input-perf branch from 74cffa4 to fff5021 Compare June 25, 2024 17:19
@patrick91 patrick91 force-pushed the fix/generic-input-perf branch from b806091 to a2cb19b Compare June 25, 2024 17:35
@@ -101,19 +101,17 @@ def annotation(self) -> Union[object, str]:
def annotation(self, value: Union[object, str]) -> None:
self.raw_annotation = value

self.__resolved_cached__ = None

def evaluate(self) -> type:
Copy link
Member Author

Choose a reason for hiding this comment

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

this is used in a couple of places directly, so we might want to reintroduce the cache, but I need to double check why we use it directly

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we only use it here:

evaled_annotation = annotation.evaluate()

@skilkis do you think it is worth caching the result here too? 😊

@patrick91
Copy link
Member Author

/pre-release

@botberry
Copy link
Member

Pre-release

👋

Releasing commit [5224421] to PyPi as pre-release! 📦

@botberry
Copy link
Member

Pre-release

👋

Pre-release 0.235.1.dev.1719337273 [5224421] has been released on PyPi! 🚀
You can try it by doing:

poetry add strawberry-graphql==0.235.1.dev.1719337273

@patrick91
Copy link
Member Author

just tested this on Strawberry Django and all the tests passed 😊

@botberry
Copy link
Member

botberry commented Jun 25, 2024

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release improves the performance when returning a lot of data, especially
when using generic inputs (where we got a 7x speedup in our benchmark!).

Here's the tweet text:

🆕 Release (next) is out! Thanks to @patrick91 for the PR 👏

This release improves the performance when returning a lot of data, especially
when using generic inputs (where we got a 7x speedup in our benchmark!).

Get it here 👉 https://strawberry.rocks/release/(next)

`resolve` is called every time we get the field type, which, turns out,
is called a lot of times, even when creating the dataclass, maybe
there's some opportunity for improvements here, but we would need
to call `resolve` anyway, so it's probably not worth changing how this
works
return self.__resolve_cache__

def _resolve(self) -> Union[StrawberryType, type]:
# TODO: I wonder if this resolve should be creating types?
Copy link
Member Author

Choose a reason for hiding this comment

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

Leaving this here for the future 😊

`resolve` is called every time we get the field type, which, turns out,
is called a lot of times, even when creating the dataclass, maybe
there's some opportunity for improvements here, but we would need
to call `resolve` anyway, so it's probably not worth changing how this
works

@patrick91 patrick91 requested review from erikwrede and skilkis June 25, 2024 19:08
@patrick91 patrick91 changed the title Use cached property for .type Add caching for resolve Jun 25, 2024
@patrick91 patrick91 merged commit ea6a5c4 into main Jun 26, 2024
63 of 64 checks passed
@patrick91 patrick91 deleted the fix/generic-input-perf branch June 26, 2024 13:43
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.

Slow performance for queries that return many items
5 participants