-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Improve N+1 detection for simple hydration methods #45848
Merged
johnswanson
merged 2 commits into
master
from
jds/improved-n+1-detection-for-simple-hydration
Jul 24, 2024
Merged
Improve N+1 detection for simple hydration methods #45848
johnswanson
merged 2 commits into
master
from
jds/improved-n+1-detection-for-simple-hydration
Jul 24, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We have N+1 detection for simple hydration methods that checks to see whether the hydration method made any DB queries at all. This makes sense, because a simple hydration method is called on each instance individually - if we make database calls *each time*, we're in trouble. Where this runs into issues is with cached data, where the simple hydration method is populating the cache - e.g. `:can_read` or `:can_write` dereferencing `api/*current-user-permissions-set*`. Currently we specifically exclude those hydration methods from the check. But after running into this again for a different simple hydration method, I think there's a more robust approach: - first, run the hydration method without counting DB queries - then, run it again and make sure we don't make any additional DB queries. There is a potential downside here: if someone wrote a simple hydration method with side effects, they'd potentially be very confused (in development - this doesn't run in prod!) when those side effects happened twice for a single hydration. But I *think* that's fairly unlikely.
johnswanson
added a commit
that referenced
this pull request
Jul 19, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
johnswanson
added a commit
that referenced
this pull request
Jul 19, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
johnswanson
added a commit
that referenced
this pull request
Jul 19, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
escherize
approved these changes
Jul 22, 2024
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.
Looks good. Is this getting tested by some existing test? Might be worth adding one, though I can see where it would be tricky.
escherize
reviewed
Jul 23, 2024
[toucan2.tools.hydrate :as t2.hydrate])) | ||
|
||
(methodical/defmethod t2.hydrate/simple-hydrate | ||
[:default ::some-silly-key-that-should-never-actually-be-hydrated!!!] |
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.
😂
escherize
approved these changes
Jul 23, 2024
johnswanson
added a commit
that referenced
this pull request
Jul 25, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
johnswanson
added a commit
that referenced
this pull request
Jul 26, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
johnswanson
added a commit
that referenced
this pull request
Jul 29, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
johnswanson
added a commit
that referenced
this pull request
Jul 29, 2024
I forgot to commit this before. Tests seem to be failing due to [this](#45848)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We have N+1 detection for simple hydration methods that checks to see whether the hydration method made any DB queries at all. This makes sense, because a simple hydration method is called on each instance individually - if we make database calls each time, we're in trouble.
Where this runs into issues is with cached data, where the simple hydration method is populating the cache - e.g.
:can_read
or:can_write
dereferencingapi/*current-user-permissions-set*
. Currently we specifically exclude those hydration methods from the check.But after running into this again for a different simple hydration method, I think there's a more robust approach:
first, run the hydration method without counting DB queries
then, run it again and make sure we don't make any additional DB queries.
There is a potential downside here: if someone wrote a simple hydration method with side effects, they'd potentially be very confused (in development - this doesn't run in prod!) when those side effects happened twice for a single hydration. But I think that's fairly unlikely.